Mybatis循环处理 like
Mybatis循环处理 like用户表user_idpreference_tagnick123456java,python余欢喜书籍表plan_idtag10042java,c++,python10043ruby,python,c10044android,ios需求:根据用户喜好的
·
Mybatis循环处理 like
用户表
user_id preference_tag nick 123456 java,python 余欢喜 书籍表
plan_id tag 10042 java,c++,python 10043 ruby,python,c 10044 android,ios
需求:根据用户喜好的标签,查找出与之对应的书籍
如上图应该返回
plan_id tag 10042 java,c++,python 10043 ruby,python,c 解决思路,将用户表中的preference_tag查询出来,放入一个List集合中,然后在书籍表中循环like查询,并去掉重复的数据即可
SELECT plan_id FROM push_plan WHERE 1=0
UNION DISTINCT
<if test="tags != null">
<foreach item="item" index="index" collection="tags">
SELECT plan_id FROM push_plan
WHERE tag LIKE CONCAT('%',#{item},'%')
UNION DISTINCT
</foreach>
</if>
SELECT plan_id FROM push_plan WHERE 1=0
主要利用了mybatis的foreach方法,同样也可以用in,但是代码需要改变下如:
<where>
<if test="followingPlanIds != null">
plan_id in
<foreach item="item" index="index" collection="followingPlanIds"
open="(" separator="," close=")">#{item}
</foreach>
</if>
</where>
注意到下面的代码含有 open=”(” separator=”,” close=”)”,就是因为 in(xxx,xxx,xxx)需要
更多推荐
已为社区贡献1条内容
所有评论(0)