Welcome to Yumao′s Blog.
iBatis到MyBatis的動態標簽變化
, 2016年08月18日 , Java Language , 评论 在〈iBatis到MyBatis的動態標簽變化〉中留言功能已關閉 ,

相比在Ibatis中我們都會接觸過非常好用的SQL標簽

在SQL標簽内有各種各樣好用的方法,例如

1
<isEqual>, <isNotEqual>, <isGreaterThan>, <isGreaterEqual>,<isLessEqual>...

但是在項目遷移到Mybatis之後

系統將提示你的這邊標簽不可用

查找官方文檔后,得出以下對應結構

 

isNull/isNotNull 標簽

1
2
3
4
5
6
7
8
9
10
11
12
13
<isNull prepend="AND" property="order.id" >
    ACCOUNT.ACCOUNT_ID = ORDER.ACCOUNT_ID(+)
</isNull>
<isNotNull prepend="AND" property="FirstName" open="(" close=")">
    ( ACC_FIRST_NAME = #FirstName#
    <isNotNull prepend="OR" property="LastName">
        ACC_LAST_NAME = #LastName#
    </isNotNull>
    )
</isNotNull>
<isNotNull prepend="AND" property="EmailAddress">
    ACC_EMAIL like #EmailAddress#
</isNotNull>

對應MyBatis語句

1
2
3
4
5
6
7
8
9
10
11
12
13
<if test="order.id == null ">
    AND  ACCOUNT.ACCOUNT_ID = ORDER.ACCOUNT_ID(+)
</if>
<if test="FirstName != null ">
    ( AND ( ACC_FIRST_NAME = #{FirstName}
    <if test="LastName != null ">
        OR  ACC_LAST_NAME = #{LastName}
    </if>
    )
    )</if>
<if test="EmailAddress != null ">
    AND  ACC_EMAIL like #{EmailAddress}
</if>

isGreaterThan 標簽

1
2
3
<isGreaterThan prepend="AND" property="Id" compareValue="0">
    ACC_ID = #Id#
</isGreaterThan>

對應MyBatis語句

1
2
3
<if test="Id &gt; 0">
    AND  ACC_ID = #{Id}
</if>

isEqual/isNotEqual 標簽

1
2
3
4
5
6
<isEqual prepend="AND" property="status" compareValue="Y">
    MARRIED = ‘TRUE'
</isEqual>
<isNotEqual prepend="AND" property="status" compareValue="N">
    MARRIED = ‘FALSE'
</isNotEqual>

對應MyBatis語句

1
2
3
4
5
6
<if test="status == Y">
    AND  MARRIED = ‘TRUE'
</if>
<if test="status &lt;&gt; N">
    AND  MARRIED = ‘FALSE'
</if>

isEqual/isNotEqual 標簽

1
2
3
4
5
6
<isEqual prepend="AND" property="status" compareValue="Y">
    MARRIED = ‘TRUE'
</isEqual>
<isNotEqual prepend="AND" property="status" compareValue="N">
    MARRIED = ‘FALSE'
</isNotEqual>

對應MyBatis語句

1
2
3
4
5
6
<if test="status == Y">
    AND  MARRIED = ‘TRUE'
</if>
<if test="status &lt;&gt; N">
    AND  MARRIED = ‘FALSE'
</if>

isGreaterThan/isGreaterEqual/isLessEqual 標簽

1
2
3
4
5
6
7
8
9
<isGreaterThan prepend="AND" property="age" compareValue="18">
    ADOLESCENT = ‘FALSE'
</isGreaterThan>
<isGreaterEqual prepend="AND" compareProperty="shoeSize" compareValue="12">
    BIGFOOT = ‘TRUE'
</isGreaterEqual>
<isLessEqual prepend="AND" property="age" compareValue="18">
    ADOLESCENT = ‘TRUE'
</isLessEqual>

對應MyBatis語句

1
2
3
4
5
6
7
8
9
<if test="age &gt; 18">
    AND  ADOLESCENT = ‘FALSE'
</if>
<if test="shoeSize &gt;= 12">
    AND  BIGFOOT = ‘TRUE'
</if>
<if test="age &lt; 18">
    AND  ADOLESCENT = ‘TRUE'
</if>

isPropertyAvailable/isNotPropertyAvailable 標簽

1
2
3
4
5
6
<isPropertyAvailable property="id" >
    ACCOUNT_ID=#id#
</isPropertyAvailable>
<isNotPropertyAvailable property="age" >
    STATUS='New'
</isNotPropertyAvailable>

對應MyBatis語句

1
2
3
4
5
6
<if test="id != null ">
    ACCOUNT_ID=#{id}
</if>
<if test="age == null ">
    STATUS='New'
</if>

isNotEmpty 標簽

1
2
3
4
5
6
<isNotEmpty property="firstName" >
    LIMIT 0, 20
</isNotEmpty>
<isNotEmpty prepend="AND" property="firstName" >
    FIRST_NAME LIKE '%$FirstName$%'
</isNotEmpty>

對應MyBatis語句

1
2
3
4
5
6
<if test=" firstName != null and firstName != '' ">
    LIMIT 0, 20
</if>
<if test=" firstName != null and firstName != '' ">
    AND  FIRST_NAME LIKE '%$FirstName$%'
</if>
关键字:, ,

评论已关闭