← 回到文章列表

iBatis到MyBatis的动态标签变化

相比在Ibatis中我们都会接触过非常好用的SQL标签 在SQL标签内有各种各样好用的方法,例如

相比在Ibatis中我们都会接触过非常好用的SQL标签

在SQL标签内有各种各样好用的方法,例如

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

但是在项目迁移到Mybatis之后

系统将提示你的这边标签不可用

查找官方文档后,得出以下对应结构

isNull/isNotNull 标签

            <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语句

        <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 标签

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

对应MyBatis语句

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

isEqual/isNotEqual 标签

            <isEqual prepend="AND" property="status" compareValue="Y">
                MARRIED = ‘TRUE'
            </isEqual>
            <isNotEqual prepend="AND" property="status" compareValue="N">
                MARRIED = ‘FALSE'
            </isNotEqual>

对应MyBatis语句

        <if test="status == Y">
            AND  MARRIED = ‘TRUE'
        </if>
        <if test="status &lt;&gt; N">
            AND  MARRIED = ‘FALSE'
        </if>

isEqual/isNotEqual 标签

            <isEqual prepend="AND" property="status" compareValue="Y">
                MARRIED = ‘TRUE'
            </isEqual>
            <isNotEqual prepend="AND" property="status" compareValue="N">
                MARRIED = ‘FALSE'
            </isNotEqual>

对应MyBatis语句

        <if test="status == Y">
            AND  MARRIED = ‘TRUE'
        </if>
        <if test="status &lt;&gt; N">
            AND  MARRIED = ‘FALSE'
        </if>

isGreaterThan/isGreaterEqual/isLessEqual 标签

            <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语句

        <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 标签

            <isPropertyAvailable property="id" >
                ACCOUNT_ID=#id#
            </isPropertyAvailable>
            <isNotPropertyAvailable property="age" >
                STATUS='New'
            </isNotPropertyAvailable>

对应MyBatis语句

        <if test="id != null ">
            ACCOUNT_ID=#{id}
        </if>
        <if test="age == null ">
            STATUS='New'
        </if>

isNotEmpty 标签

            <isNotEmpty property="firstName" >
                LIMIT 0, 20
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="firstName" >
                FIRST_NAME LIKE '%$FirstName$%'
            </isNotEmpty>

对应MyBatis语句

        <if test=" firstName != null and firstName != '' ">
            LIMIT 0, 20
        </if>
        <if test=" firstName != null and firstName != '' ">
            AND  FIRST_NAME LIKE '%$FirstName$%'
        </if>

在这里找找

输入关键词,找一篇过去的记录。