mybatis: foreach實現in集合查詢(mybatis 3.5.7)
阿新 • • 發佈:2022-02-27
一,示例程式碼:
1,OrderController.java
List<Cart> cartList = cartService.getAllCartByCartId(userId,cartIds,configBeanValue.goodsImagesHost,configBeanValue.attrHost);
2,CartServiceImpl.java
@Override public List<Cart> getAllCartByCartId(Long userId,Long[] cartIds,String urlHost,String urlHostAttr) {//List<Cart> listCart = cartMapper.selectAllCartByUserId(userId); List<Cart> listCart = cartMapper.selectAllCartByCartId(userId,cartIds); ...
3,CartMapper.java
List<Cart> selectAllCartByCartId(@Param("userId")Long userId, @Param("cartIds")Long[] cartIds);
4,CartMapper.xml
<select id="selectAllCartByCartId" resultType="com.yj.storeback.pojo.Cart"> select c.* from cart as c,goods as g where c.userId=#{userId} and c.goodsId=g.goodsId and g.isSale=1 and c.cartId IN <foreach collection="cartIds" item="id" index="index" open="(" close=")"separator=","> #{id} </foreach> order by c.cartId desc </select>
說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest
對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/
或:https://gitee.com/liuhongdi
說明:作者:劉巨集締 郵箱: [email protected]
二,測試效果
檢視sql查詢的日誌:
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7cd7fbe6] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@1874055310 wrapping com.mysql.cj.jdbc.ConnectionImpl@6052b4a0] will not be managed by Spring ==> Preparing: select c.* from cart as c,goods as g where c.userId=? and c.goodsId=g.goodsId and g.isSale=1 and c.cartId IN ( ? , ? ) order by c.cartId desc ==> Parameters: 1(Long), 25(Long), 24(Long) <== Columns: cartId, isDirect, userId, goodsId, skuId, goodsName, skuAttrNames, skuAttrIds, price, amount, addTime, isCheck, packType, addFrom <== Row: 25, 0, 1, 1, 22, 元気森林蘇打水氣泡水無糖飲料元氣水, 480ml, 58, 44.00, 4, 2022-02-25 17:15:12, 1, 2, wap <== Row: 24, 0, 1, 2, 1, 張裕 第九代特選級解百納7, dd_22, 2_4, 8.00, 4, 2022-02-24 19:01:29, 1, 2, wap <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7cd7fbe6]
前端效果: