AdSense

網頁

2021/2/25

iBatis dynamic SQL <isNotNull>

iBatis動態SQL <isNotNull>用法。

例如撈取EMPLOYEE資料時依參數類別Employee屬性nameemail是否有值決定產生的where條件。

<select id="getEmployeeList" 
        parameterClass="com.abc.demo.model.Employee" 
        resultClass="com.abc.demo.model.Employee" >
    select * from EMPLOYEE
        <dynamic prepend="where">
            <isNotNull prepend="and" property="name">
                NAME = #name#
            </isNotNull>
            <isNotNull prepend="and" property="email">
                EMAIL like '%'||#email#||'%' 
            </isNotNull>
        </dynamic>
    order by AGE
</select>

例如更新EMPLOYEE資料時依參數類別Employee屬性age是否有值決定產生的update sql的更新欄位。

<update id="updateEmployeeById" 
        parameterClass="com.abc.demo.model.Employee">
    update EMPLOYEE set
        NAME = #name#,
        <dynamic>
            <isNotNull property="age">
                AGE = #age#,
            </isNotNull>
        </dynamic>
        EMAIL = #email#
    where ID = #id#
</update>


沒有留言:

AdSense