偶爾用一次like進行模糊查詢,
除了%,竟忘了還有別的通配符。。
被人提了bug。
處理字符串中的通配符-----前面放一個轉義符
public static String escapeStr(String str) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '%' || str.charAt(i) == '_') {
temp += "\\" + str.charAt(i);
} else {
temp += str.charAt(i);
}
}
return temp;
}
sql 中 也要作以下處理
<if test="param.areaname!=null"> and areaname like '%'||#{param.areaname}||'%' escape '\'</if>
原文是轉的,卻不見原文的原文地址。
https://blog.csdn.net/w522301629/article/details/82379514