spring-data-jpa fuzzy query special character escape

When using special characters such as _% and other fuzzy queries, often the result of the query is not what we want _ will match any character% will match any number of characters. To use _% for fuzzy queries, it must be escaped

  For example, the following sql  

select * from orders where name like "%abc_%"

Will match any data whose name contains abc. If you need to match abc_, you need to escape the underscore_

select * from orders where name like "%abc/_%" ESCAPE '/'

escpae'/' means to escape the characters after the'/'

Multiple special character query

select * from order where name like "%acb///_/%%" ESCAPE '/'

Will match data containing acb/_%. Note that the escape character / must also be escaped, otherwise it will be used as a logo to escape the following characters

How to write spring-data-jpa

cb.like(root.get("name"),"%" +name.replaceAll("/","//").replaceAll("_","/_").replaceAll("%","/%")+"%",'/')

 

Guess you like

Origin blog.csdn.net/name_is_wl/article/details/82145188