以前一直用hibernate,现在换mybatis,结果这里竟然折腾了个把小时,试了网络上有好多种解决方法,都不靠谱
最后终于解决了,亲测有效
我的数据库用的是mysql,mapper.xml有两种配置解决方法
1.如果字段是字符串,可以直接写成
<if test="usedName != null and usedName!=''">
and used_name like "%"#{usedName}"%"
</if>
但是字段类型是int或者其他数字类型,写成这样数据库查询就会报错。
2.通用解决方法是写成下面这样,注意使用’$’,不是’#’
<if test="usedName != null and usedName !=''">
and usedName like CONCAT('%','${usedName}','%')
</if>