Mybatis使用in传入List的三种方法
1.非xml方式,使用注解传in,要使用
@Select("<script>" + "SELECT count(DISTINCT member_id) as memberCount " + "from member_analysis " + "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>
下一篇:
数组排序算法之快速排序