在MyBatis Plus中,使用like查询特殊字符_,,%时会出现以下情况:
1、查询下划线_,sql语句会变为"%_%",会导致返回所有结果。在MySQL中下划线“_”表示匹配单个字符,所以结合后“%_%”就表示匹配所有字符了。
2、查询百分号%,sql语句为“%%%”,也会导致返回所有结果。
3、查询反斜杠,sql语句是“%\%”,反斜杠表示转义,导致不会查询到包含字段中有的行。
解决方法
解决方案就是对这三个特殊字符进行转义,示例public static String escapeSqlSpecialChars(String str){
public static String escapeSqlSpecialChars(String str)
{
if(StringUtils.isNotBlank(str))
{
str= str.replaceAll("\\", "\\\\");
str= str.replaceAll("_", "\\_");
str= str.replaceAll("%", "\\%");
}
return str;
}
参考https://blog.csdn.net/weixin_39911007/article/details/111884096