Mybatis 传递List参数 in查询的使用

当查询的参数为一个时

findIds(List<Long> ids)

当参数类型为List或Array时,查询语句collection必须为list或array。

 select * from student where id in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> 
                        #{item}
    </foreach>

select * from student where id in
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")"> 
                        #{item}
    </foreach>

属性描述

collection: 表示迭代集合的名称,可以使用@Param注解指定,如下图所示 该参数为必选
item: 表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map:则代表key-value的value,该参数为必选
open:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
close:表示该语句以什么结束,最常用的是右括弧’)’,注意:mybatis会将该字符拼接到整体的sql语 句之后,该参数为可选项
separator: mybatis会在每次迭代后给sql语句append上separator属性指定的字符,该参数为可选项
index: 在list、Set和数组中,index表示当前迭代的位置,在map中,index代指是元素的key,该参数是可选项。

当参数为多个时

findIds(List<Long>ids,String name)

可以在业务逻辑层使用Map,以Map的形式传参,这样可以指定collection属性名称

Map<String,Object> params = new HashMap<String,Object>(2);
params.put("communityId",activiteManage.getCommunityId());
params.put("buildingIds",buildingIds)
mapper.selectBuildingListById(params)

mapper层

List<ActiviteManage> selectBuildingListById(Map<String, Object> params);

mapper.xml

 <select id="selectBuildingListById" resultType="com.wr.remote.estate.activity.ActiviteManage" parameterType="list" resultMap="ActiviteManageResult">
        SELECT
            building_id,
            community_id,
            building_name
        FROM
            xccj_community.bus_building
        WHERE
            del_flag = 0
          AND community_id = #{communityId} and building_id in

        <foreach item="item" index="index" collection="buildingIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值