escape 的说明
Escape wildcard characters
The LIKE keyword allows for string searches. The '_' wild card character is used to match exactly one character, while '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL. Examples:
SELECT name FROM emp
WHERE id LIKE '%/_%' ESCAPE '/';
SELECT name FROM emp
WHERE id LIKE '%\%%' ESCAPE '\';
通配符
http://www.w3school.com.cn/sql/sql_wildcards.asp
% 替换为 /%
_ 替换为 /_
作为escapeChar的 / 本身也需要替换为 // (搜索关键字里有/,替换为 // ,表示不要把这里的 / 当作转义符)
JPA like 函数参数escapeChar
Predicate javax.persistence.criteria.CriteriaBuilder.like(Expression<String> x, String pattern, char escapeChar)
最终用法:
cb.like(root.get("name"),"%" +name.replaceAll("/","//").replaceAll("_","/_").replaceAll("%","/%")+"%",'/')
(mysql 默认使用 ‘\' 作为转义符,实例见 https://blog.csdn.net/wuzhong8809/article/details/87973686)