0
点赞
收藏
分享

微信扫一扫

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


场景

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 条评论