mybatisplus中的xml如何添加like条件,进行模糊查询

like会和%结合使用,like '%xxx%'匹配包含xxx的内容,'%xxx'匹配一xxx结尾的内容,'xxx%'匹配以xxx开头的内容。

如果在xml中写code like ${dto.code},会被解析code like xxx,如果不是数字,肯定是会直接报错的,没有加单引号。

如果使用code like '${dto.code}',解析为code like 'xxx',错是不会报了,但是查询结果和code = xxx是一样的。

所以,根据自己的需求,在两边加上%,如code like '%${dto.code}'

当然,由于${}存在sql注入的风险,所以上述看似直观的方法,在敏感信息处,不推荐使用。可以使用bind标签。

<if test="itemName!= null and itemName!= ''">
   <bind name="itemName" value="'%'+itemName+'%'"/>
         t1.item_name like #{itemName}
</if>

还可以使用concat函数进行拼接

<if test="itemName!= null and itemName!= ''">
    t1.item_name like concat('%',#{itemName},'%')
</if>

猜你喜欢

转载自blog.csdn.net/qq_41885819/article/details/116271141