鍍金池/ 問答/Java/ Mybatis非空判斷沒有生效

Mybatis非空判斷沒有生效

使用mybatis updateByPrimaryKeySelective方法來更新語句,但是null值也傳過去了。

Mapper.xml

  <update id="updateByPrimaryKeySelective">
    update zzbizorder_#{tableIndex}
    <set>
      <if test="#{record.clickid != null}">
        clickid = #{record.clickid,jdbcType=BIGINT},
      </if>
      <if test="#{record.xxxx != null}">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="#{record.xxxx != null}">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="#{record.xxxx != null}">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="#{record.xxxx != null}">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
    </set>

Mapper.class

public interface OrderEntityMapper {

    int insertSelective(@Param("tableIndex") Integer tableIndex, @Param("record") OrderEntity record);

    int updateByPrimaryKeySelective(@Param("tableIndex") Integer tableIndex, @Param("record") OrderEntity record);

}

程序打印的SQL語句:

JDBC Connection [com.mysql.jdbc.JDBC4Connection@377008df] will not be managed by Spring
==>  Preparing: update order_? SET clickid = ?, xxxx = ?, xxxx = ?, xxxx = ?, xxxx = ?, where id = ? 
==> Parameters: 0(Integer), null, null, null, null
<==    Updates: 1

clickid為null,但是也set了,請(qǐng)問這是什么原因呢。

回答
編輯回答
萌吟
  
  把xml配置改成這樣就可以了
  <update id="updateByPrimaryKeySelective">
    update zzbizorder_#{tableIndex}
    <set>
      <if test="record.clickid != null">
        clickid = #{record.clickid,jdbcType=BIGINT},
      </if>
      <if test="record.xxxx != null">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="record.xxxx != null">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="record.xxxx != null">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
      <if test="record.xxxx != null">
        clickid = #{record.xxxx,jdbcType=BIGINT},
      </if>
    </set>
    
    
    
2018年2月19日 19:49