ibatis.net学习笔记(五) 带条件查询语法

这里拿一个新闻表来做介绍

  <resultMaps>
    <resultMap id="FullResultMap" class="News">
        <result property="NewsID" column="NewsID" type="System.Int32" dbType="SqlDbType.Int"/>
        <result property="CateID" column="CateID" type="System.Int32" dbType="SqlDbType.Int"/>
        <result property="Title" column="Title" type="System.String" dbType="SqlDbType.NVarChar"/>
        <result property="Summary" column="Summary" type="System.String" dbType="SqlDbType.NVarChar"/>
        <result property="Description" column="Description" type="System.String" dbType="SqlDbType.NText"/>
        <result property="IsTop" column="IsTop" type="System.Boolean" dbType="SqlDbType.Bit"/>
        <result property="IsShow" column="IsShow" type="System.Boolean" dbType="SqlDbType.Bit"/>
        <result property="Visitation" column="Visitation" type="System.Int32" dbType="SqlDbType.Int"/>
        <result property="PublishTime" column="PublishTime" type="System.DateTime" dbType="SqlDbType.DateTime"/>
        <result property="UpdateTime" column="UpdateTime" type="System.DateTime" dbType="SqlDbType.DateTime"/>
        <result property="CreateUser" column="CreateUser" type="System.Int32" dbType="SqlDbType.Int"/>
        <result property="UpdateUser" column="UpdateUser" type="System.Int32" dbType="SqlDbType.Int"/>
    </resultMap>
  </resultMaps>

1  如果 CateID >0 执行查询 select * from news where CateID=xxx

   <select id="SelectAllRecord"  parameterClass="Hashtable" resultClass="int">
      SELECT count(*) from News
      <dynamic prepend="where"> --条件成立时前面要加的字符串 prepend ="字符串" 如果下面条件成立 追加where
        <isGreaterThan    property="CateID"  compareValue="0">  -- 如果是属性关键字是比较条件时,字符串存放要比较的值compareValue="字符串"   如果>0 追加  CateID = #CateID#
          CateID = #CateID#
        </isGreaterThan>
      </dynamic>
    </select>

其他参数列表

<isEqual>

如果参数相等于值则查询条件有效。

<isNotEqual>

如果参数不等于值则查询条件有效。

<isGreaterThan>

如果参数大于值则查询条件有效。

<isGreaterEqual>

如果参数等于值则查询条件有效。

<isLessEqual>

如果参数小于值则查询条件有效。如下所示:

<isLessEqual prepend = ”AND” property = ”Visitation” compareValue = ”18” >

IsShow= ‘TRUE’

</isLessEqual>

<isPropertyAvailable>

如果参数有使用则查询条件有效。

<isNotPropertyAvailable>

如果参数没有使用则查询条件有效。

<isNull>

如果参数为NULL则查询条件有效。

<isNotNull>

如果参数不为NULL则查询条件有效。

<isEmpty>

如果参数为空则查询条件有效。

<isNotEmpty>

如果参数不为空则查询条件有效。参数的数据类型为Collection、String 时参数不为NULL或“”。如下所示:

<isNotEmpty prepend=”AND” property=”Visitation” >  select * from news where xxxx and  Visitation=#Visitation#

    Visitation=#Visitation#

</isNotEmpty>

<isParameterPresent>

如果参数类不为NULL则查询条件有效。

<isNotParameterPresent>

<isNotParameterPresent prepend=”AND”>

    EMPLOYEE_TYPE = ‘DEFAULT’

</isNotParameterPresent>

原文地址:https://www.cnblogs.com/jcgh/p/2628457.html