背景:针对文章,会有tag标签,这时可多选tag,只要包含任何一个tag都会被查出来
目录
一、初始化TagList
1:建议使用...,将传过来的id字符串进行分割,支持多个,但如果没有会为[null],需要排除
2:建议转为list进行处理,防止逗号分割情况出现隐形bug
3:代码
public static List<String> getListFromString(String... str){
List<String> list=new ArrayList<>();
if(str.length==1 && StringUtils.isEmpty(str[0])){
return list;
}
for (String s : str) {
String[] split = s.split(",");
for (String item : split) {
if(StringUtils.isNotBlank(item) && !item.equals(",")){
list.add(item);
}
}
}
return list;
}
二、mybatis处理
1:需使用foreach标签,一定要控制or情况
2:sql本质为or的拼接,将list每一个值读出来,进行like模糊查询,or
3:控制外层一个and
4:循环时,需要使用index下标判定,如果为最后一次循环,不加or
<if test="tagIdList != null and tagIdList.size() > 0">
and
<foreach item="tagId" index="index" collection="tagIdList" open="" separator="" close="">
tag_ids like CONCAT( '%',#{tagId},'%')
<if test="index!=(tagIdList.size-1)">or</if>
</foreach>
</if>