场景:
文本框输入一些关键字、需对这些关键字都做模糊匹配
解决:
1、字符串分割(string-->stringList)
public List<String> stringToList(String str){
log.info("============str =========,{}",str);
List<String> list = new ArrayList<>();
StringBuilder res = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i)==','||str.charAt(i)=='、'||str.charAt(i)==' '){
list.add(res.toString());
res=new StringBuilder();
continue;
}
if(i==str.length()-1){
res.append(str.charAt(i));
list.add(res.toString());
}
res.append(str.charAt(i));
}
List<String> resList = list.stream().distinct().filter((x)->!StringUtils.isBlank(x)).collect(Collectors.toList());
log.info("============resList =========,{}",resList);
return resList;
}2、模糊查询
入参变为List后再做模糊匹配(伪代码)
<if test="list != null">
AND
<foreach collection="list" item="item" open="(" separator="or" close=")">
u.user_name like CONCAT('%', #{item}, '%')
</foreach>
</if>
该文章介绍了如何在Java中处理关键字的模糊匹配。首先,通过字符串分割函数将输入文本转换为List,然后过滤出非空项。接着,使用List流处理进行去重。最后,展示了在参数为List时如何构建SQL模糊查询条件。

被折叠的 条评论
为什么被折叠?



