1. 程式人生 > >mybatis的查詢語句-foreach的使用

mybatis的查詢語句-foreach的使用

前言

傳入的引數時map集合,但其中有 list 集合

service

  Map<String, Object> searchMap = new HashMap<>();
  searchMap.put("borrowId", borrowId);
  List<String> stateList = new ArrayList<>();
  stateList.add(BorrowModel.STATE_PASS);
  stateList.add(BorrowModel.STATE_REPAY_FAIL);
  searchMap.put
("stateList", stateList); Borrow borrow = clBorrowMapper.findByIdAndState(searchMap);

mapper

Borrow findByIdAndState(Map<String, Object> paramMap);

xml

<select id="findByIdAndState" resultMap="BaseResultMap" parameterType="java.util.HashMap">
        select
        id,user_id,order_no,remark
        from
cl_borrow <trim prefix="where" prefixOverrides="and|or"> <if test="borrowId !='' and borrowId !=null"> and id = #{borrowId,jdbcType=BIGINT} </if> <if test="stateList !=null"> and state in <foreach item
="item" index="index" collection="stateList" open="(" separator="," close=")"> #{item} </foreach> </if> </trim> </select>