iBatis中的动态查询

iBatis中的动态查询还是比较好用的

如果想深入学习,可以参考 Manning.iBATIS.in.Action.Jan.2007

下面给出几个例子和dtd定义:

<select id="selectDispatchedKey" parameterClass="KeyAndKeyFlowInfo" resultMap="KeyAndKeyFlowResult">
   select distinct
    KEY_ID,
    USER_ID,
    INITIATOR,
    INIT_DATE,
    INITIATOR_EMAIL,
    SGS_KEY.BRANCH_NAME,
    APPROVER,
    APPROVER_EMAIL,
    APPROVE_DATE
   from
    SGS_KEY,
    SGS_KEY_FLOW
   where SGS_KEY.APPLY_FLOW_ID=SGS_KEY_FLOW.KEY_FLOW_ID
   <dynamic prepend="and" open="(" close=")">   
    <isNotNull property="ki.userId" prepend="and" removeFirstPrepend="false">
     USER_ID = #ki.userId#
    </isNotNull>
    <isNotNull property="kfi.initiator" prepend="and">
     INITIATOR = #kfi.initiator#
    </isNotNull>
    <isNotNull property="kfi.initDate" prepend="and">
     INIT_DATE = #kfi.initDate#
    </isNotNull>
   </dynamic>
</select>

 

<update id="updateKeyFlow" parameterClass="KeyFlowInfo">
   update SGS_KEY_FLOW set
   <dynamic>
     KEY_FLOW_ID =#keyFlowId#
    <isNotNull property="branchName" prepend=",">
     BRANCH_NAME = #branchName#
    </isNotNull>
    <isNotEqual property="operation" prepend="," compareProperty="operation" compareValue="0">
     OPERATION = #operation#
    </isNotEqual>
    <isNotNull property="initiator" prepend=",">
     INITIATOR = #initiator#
    </isNotNull>
    <isNotNull property="initiatorEmail" prepend=",">
     INITIATOR_EMAIL = #initiatorEmail#
    </isNotNull>
    <isNotNull property="initDate" prepend=",">
     INIT_DATE = #initDate#
    </isNotNull>
    <isNotNull property="approver" prepend=",">
     APPROVER = #approver#
    </isNotNull>
    <isNotNull property="approverEmail" prepend=",">
     APPROVER_EMAIL = #approverEmail#
    </isNotNull>
    <isNotNull property="approveDate" prepend=",">
     APPROVE_DATE = #approveDate#
    </isNotNull>
    <isNotEqual property="keyNum" prepend="," compareProperty="keyNum" compareValue="0">
     KEY_NUM = #keyNum#
    </isNotEqual>
    <isNotNull property="applyReason" prepend=",">
     APPLY_REASON = #applyReason#
    </isNotNull>
    <isNotNull property="rejectReason" prepend=",">
     REJECT_REASON = #rejectReason#
    </isNotNull>                        
   </dynamic>
   where
    KEY_FLOW_ID =#keyFlowId#
</update>

 

属性关键字

含义

<isEqual>

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

<isNotEqual>

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

<isGreaterThan>

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

<isGreaterEqual>

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

<isLessEqual>

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

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

ADOLESCENT = ‘TRUE’

</isLessEqual>

<isPropertyAvailable>

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

<isNotPropertyAvailable>

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

<isNull>

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

<isNotNull>

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

<isEmpty>

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

<isNotEmpty>

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

<isNotEmpty prepend=”AND” property=”firstName” >

FIRST_NAME=#firstName#

</isNotEmpty>

<isParameterPresent>

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

<isNotParameterPresent>

Checks to see if the parameter object is not present (null). Example Usage:

<isNotParameterPresent prepend=”AND”>

EMPLOYEE_TYPE = ‘DEFAULT’

</isNotParameterPresent>

你可能感兴趣的:(Date,ibatis,user,null,email,branch)