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='member
文章共754字 · 阅读需要大约3分钟
一键AI生成摘要,助你高效阅读
问答
·
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>
更多推荐
已为社区贡献3条内容
所有评论(0)