mybatis的xml文件因为注释影响sql内容的问题

默认mybtis的xml会删除多余的“and”,并且条件不成立也不会生成where标签。

示例:

<select id="getZisdateYesCountsVo" resultType="com.cw.bean.vo.ZisdateYesCountsVo">
      SELECT
          *
      FROM 
          myTable t
      <where>
          <if test="name2 != '' and name2 != null">
              t.NAME2 = #{name2}
          </if>
      </where>
</select>

但如果在where标签中添加注释,可能会被mybaits认为是where中的一个条件

<select id="getZisdateYesCountsVo" resultType="com.cw.bean.vo.ZisdateYesCountsVo">
      SELECT
          *
      FROM 
          myTable t
      <where>
          -- 如果有注释的情况下,会误以为不为空,可能会出现不删除多余的where和and
          <if test="name2 != '' and name2 != null">
              t.NAME2 = #{name2}
          </if>
      </where>
</select>

解决办法:

1. 可以放到if标签内

2. where中有必须有的条件比如1=1

<if test="name2 != '' and name2 != null">
    -- 注释放到标签内不会影响
    t.NAME2 = #{name2}
</if>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值