参考:mybatis中LIKE模糊查询的几种写法以及注意点_NasubiZW-CSDN博客_mybatis模糊查询like语句怎么写
1.${...}
"<if test=\"title !=null and title !='' and title !='null'\"> " +
" and s.title like '%${title,jdbcType=VARCHAR}%'" +
"</if> "
报错:Parameter 'VARCHAR' not found. Available parameters are [XXX]
~[mybatis-plus3.4.0]
不写 jdbcType=VARCHAR 就没事
"<if test=\"title !=null and title !='' and title !='null'\"> " +
" and s.title like '%${title}%'" +
"</if> "
但是注意 '%${title}%' 单引号别写错了,如果你的错了看看是不是单引号弄错了。
可能会引起sql的注入,平时尽量避免使用${...}
这句话鄙人还未验证过,大家都说是,那就是吧。
2.#{...}
如果写成这样
like '%'#{title,jdbcType=VARCHAR}'%'
单引号引%就会:sql injection violation, syntax error: syntax error, error in :'itle like '%'?'%') TOTAL'
但是写成双引号引 % 的话,在注解式开发mapper接口下又会有冲突。
@Select("SELECT 。。。" )这里有了双引号,你只能换单引号或者转义(我8会)?
List ....
所以 like "%" #{title,jdbcType=VARCHAR} "%" 好像并不适用所有情况。
3.CONCAT()函数连接参数
"<if test=\"title !=null and title !='' and title !='null'\"> " +
" and s.title like concat('%',#{title,jdbcType=VARCHAR},'%')" +
"</if> "
这个就不说了,虽然复杂点,但是真香!
关于sql注入、#和$的区别、jdbcType=XXX这几点知识,新手有必要补补(还有我自己)