Mybatis使用in传入List的三种方法

1.非xml方式,使用注解传in,要使用

@Select("<script>" +
            "SELECT count(DISTINCT member_id) as memberCount\n" +
            "from member_analysis\n" +
            "WHERE agent_id in <foreach item='item' index='index' collection='memberIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
Integer getCountWithAgentId(@Param("memberIds") List<String> memberIds);

其中的foreach的collection直接写成@param中的值即可。

2.在入参前进行字符串封装,拼成(,,,,)传值

@Select("SELECT count(DISTINCT member_id) as memberCount from member_analysis where access_pat_id in (#{memberIds})")
    Integer getCountWithAgentId(@Param("memberIds")String memberIds);

使用方法返回

 public static String indexForm(List<String> s){
        String content="";
        int i=0;
        for (String ss:s){
            i++;
            if (i==s.size()){
                content+=ss;
            }else {
                content+=ss+",";
            }
        }
        return content;
    }

或者使用

StringUtils.join(memberIds.toArray(),",")

3.正常流程的xml方法

    <select id="findByIds" resultMap="BaseResultMap">
        select * from dictionaries
        <where>
            dictionaries.key  in 
            <foreach  item="item" index="index" collection="memberTypes" open="(" separator="," close=" )">
                 #{item}
        	</foreach>
        </where>
    </select>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值