使用 Mybatis 的时候通过 Mybatis Generator 生成了Mapper, 通过 like
查询的时候发现在与用户交互中,用户输入 %
能查询到全部信息,
所以需要一种方法来过滤掉用户输入的通配符.
- 目前只找到了这一种方式, 转译用户输入的通配符为普通符号
public class LikeEscape {
public static String filterAddPrefix(String prefixWildcard, String content) {
String filterContent = filter(content);
if (StringUtils.isBlank(filterContent)) {
return content;
}
return prefixWildcard + content;
}
public static String filterAddSuffix(String content, String suffixWildcard) {
String filterContent = filter(content);
if (StringUtils.isBlank(filterContent)) {
return content;
}
return filterContent + suffixWildcard;
}
public static String filter(String content) {
if (StringUtils.isBlank(content)) {
return content;
}
content = content.replace("/", "//");
content = content.replace("'", "''");
content = content.replace("[", "/[");
content = content.replace("]", "/]");
content = content.replace("%", "/%");
content = content.replace("&", "/&");
content = content.replace("_", "/_");
content = content.replace("(", "/(");
content = content.replace(")", "/)");
return content;
}
}
来自: https://www.cnblogs.com/mingfeng002/p/10078876.html