首页 > 技术文章 > Mybatis解决sql中like通配符模糊匹配

Mr-O-O 2019-06-30 11:50 原文

偶尔用一次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

推荐阅读