MyBatis模糊查询特殊字符无效问题

在使用LIKE关键字进行模糊查询时,“%”、“_”和“[]”单独出现时,会被认为是通配符。可以通过以下2种方式解决。

一. 在的mer文件中,在like语句后面加上ESCAPE,告诉数据库转义字符为"/"

 

二. 使用内置函数来进行模糊搜索(locate()等)

使用locate()

select `name` from `user` where locate('key', `condition`)>0

找到返回的结果都大于0,没有查找到返回0;

使用instr()

select`name` from `user` where instr(`condition`, ‘keyword’ )>0

唯一不同的是查询内容的位置不同

使用position()

select`name` from `user` where position(‘keyword’ IN `condition`)

使用find_in_set()

find_in_set(str,strlist),strlist必须要是以逗号分隔的字符串

胜象大百科