mybatis的查询语句-foreach的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cherry_xiu/article/details/82116842

前言

传入的参数时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>

猜你喜欢

转载自blog.csdn.net/cherry_xiu/article/details/82116842