首页 > 技术文章 > ibatis 字段类型为int时如何避免默认值得干扰

ylhssn 2016-06-03 14:38 原文

在xml文件中配置查询语句时,通常都是采用以下方法:

<select id="getByExample" resultMap="PgWtResult" parameterClass="com.daos.newSix.entity.PgWt" >
   select id, seqNo, entrustNo, tradeType
   from pg_wt
   <dynamic prepend="where">
      <isNotNull prepend="and" property="seqno" >
        seqNo = #seqno:VARCHAR#
      </isNotNull>
      <isNotNull prepend="and" property="tradetype" >-->
           tradeType = #tradetype:INTEGER#    
      </isNotNull>
      <isNotNull prepend="and" property="tradetypeother" >
        tradeTypeOther = #tradetypeother:VARCHAR#
      </isNotNull>
</dynamic>
</select>

此处关于int类型字段只采用了非空判断,很弱有木有~

当你new PgWt() 时,tradetype=0~,但是在咱们做getByExample查询时,又不以tradetype做查询条件时,sql中却拼接了

“ and tradetype=0”这查询条件~

要想避免这种情况,可以按照以下写法进行修改

 

1 <isPropertyAvailable property="tradetype">
2     <isNotNull property="tradetype">
3     <!--  isGreaterThan判断参数是否大于compareValue,isGreaterEquals是大于等于 -->
4     <isGreaterEqual prepend=" and " property="id" compareValue="0">
5         tradeType = #tradetype:INTEGER#
6     </isGreaterEqual >
7     </isNotNull>
8 </isPropertyAvailable>        

 

除此之外,还可以使用其他二元条件元素,介绍如下: 

二元条件元素 

二元条件元素将一个属性值和一个静态值或另一个属性值比较,如果条件为“真”,元素体的内容将被包括在查询SQL语句中。

二元条件元素的属性: 

         prepend - 可被覆盖的SQL语句组成部分,添加在语句的前面(可选)property - 被比较的属性(必选)
         compareProperty - 另一个用于和前者比较的属性(必选或选择compareValue)
         compareValue - 用于比较的值(必选或选择compareProperty)

二元条件元系的属性:

<isEqual>

比较属性值和静态值或另一个属性值是否相等。

<isNotEqual>

比较属性值和静态值或另一个属性值是否不相等。

<isGreaterThan>

比较属性值是否大于静态值或另一个属性值。

<isGreaterEqual>

比较属性值是否大于等于静态值或另一个属性值。

<isLessThan>

比较属性值是否小于静态值或另一个属性值。

<isLessEqual>

比较属性值是否小于等于静态值或另一个属性值。 例子: <isLessEqual prepend=”AND” property=”age” compareValue=”18”> ADOLESCENT = ‘TRUE’ </isLessEqual>

 

 

 

 

  

推荐一个:ibatis动态生成标签

推荐阅读