mybatis中的xml中拼接sql中参数与字符串的方法

是她丫

关注

阅读 74

2023-03-11


场景

mybatis中接口方法对应的xml文件中的方法中,需要使用模糊搜索,

查询以参数开头的记录。

错误的sql拼接:

<if test="locationVO != null and locationVO.selected != null">
and location.goods_location_number like #{locationVO.selected}+'%'
</if>

这样拼接的sql语句为:

and location.goods_location_number like 'A'+'%';

实现

正确实现拼接的写法为:

<if test="locationVO != null and locationVO.selected != null">
and location.goods_location_number like CONCAT(#{locationVO.selected},'%')
</if>

CONCAT是数据库中拼接字符串的方法。

精彩评论(0)

0 0 举报