23htg 发表于 2016-3-31 09:39:59

关于INTEGER 类型在Mybatis中使用if的注意

                      Mybatis中是使用<if>用来编写动态语句是很方便的一个操作,不过在最近的项目开发中当使用的一个变量为inteter类型的时候,用<if>来编写发现有点问题,源码如下面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<update id="updateWeightintervalInfo" parameterType="com.exiao.platform.core.logistics.data.WeightInterval">
      update tm_weightinterval SET
      <if test="wgtintervalCode!=null and wgtintervalCode !=''">
            wgtintervalcode=#{wgtintervalCode},
      </if>
      <if test="intervalName!=null and intervalName!=''">
            intervalname=#{intervalName},
      </if>
      <if test="transportMode!=null and transportMode!=''">
            transportmode=#{transportMode},
      </if>
      <if test="vendorCode !=null and vendorCode !=''">
            vendorcode=#{vendorCode},
      </if>
      <if test="departurecityCode!=null and departurecityCode!=''">
            departurecitycode=#{departurecityCode},
      </if>
      <if test="minWeight !=null and minWeight !=''">
            minweight=#{minWeight},
      </if>
      <if test="maxWeight != null and maxWeight !=''">
            maxweight=#{maxWeight},
      </if>
      <if test="firstWeight !=null and firstWeight !=''">
            firstweight=#{firstWeight},
      </if>
      <if test="addedWeight != null and addedWeight!=''">
            addedweight=#{addedWeight},
      </if>
      <if test="sortSeq!=null and sortSeq!=''">
            sortseq=#{sortSeq},
      </if>
      <if test="enabled!=null and enabled!=''">
            enabled=#{enabled},
      </if>
      update_time=#{updateTime},
      updator=#{updator}
      WHERE wgtintervalid=#{id}
    </update>




其中enabled是interger类型,其中用0表示无效,1表示有效。如果传值为0的时候,按照if的逻辑则不判断为false不执行当中的语句。经过查找资料,只需要把代码中的enabled!=‘’去掉则可以正常使用。
                   

页: [1]
查看完整版本: 关于INTEGER 类型在Mybatis中使用if的注意