ibatis中動態SQL_update
阿新 • • 發佈:2019-02-20
如何知道上一篇的動態INSERT的話,動態UPDATE就很簡單了,直接上XML
注意:如果<isNotEmpty>是寫在一行的話,並且是第一個的話
<isNotEmpty property="realName" prepend=","><![CDATA[ REALNAME = #realName# ]]></isNotEmpty>
內容注意中間加個空格,不然會出現 update role setname=xxx 直接報錯
Xml程式碼-
<update id="updateRole" parameterClass="com.mmblue.model.Role"
- <![CDATA[
- UPDATE role
- SET
- gmt_modified = to_date(#gmtModified#,'yyyy-mm-dd hh24:mi:ss'),
- modifier = #modifier#
- ]]>
- <dynamic>
- <isNotEmpty property="isDeleted" prepend=",">
-
<![CDATA[
- is_deleted = #isDeleted#
- ]]>
- </isNotEmpty>
- <isNotEmpty property="roleCode" prepend=",">
- <![CDATA[
- role_code = #roleCode#
- ]]>
-
</isNotEmpty>
- <isNotEmpty property="roleName" prepend=",">
- <![CDATA[
- role_name = #roleName#
- ]]>
- </isNotEmpty>
- <isNotEmpty property="deptId" prepend=",">
- <![CDATA[
- dept_id = #deptId#
- ]]>
- </isNotEmpty>
- </dynamic>
- <![CDATA[
- WHERE id = #id#
- ]]>
- </update>
2.再來個全部遍歷的
Xml程式碼- <update id="updateUser" parameterClass="com.mmblue.domain.User">
- UPDATE MYIVR_TUSER
- <dynamic prepend="SET ">
- <isNotEmpty property="realName" prepend=","><![CDATA[ REALNAME = #realName# ]]></isNotEmpty>
- <isNotEmpty property="password" prepend=","><![CDATA[ PASSWORD = #password# ]]></isNotEmpty>
- <isNotEmpty property="projectId" prepend=","><![CDATA[ PROJECT_ID = #projectId# ]]></isNotEmpty>
- <isNotEmpty property="isEable" prepend=","><![CDATA[ ISENABLE = #isEable# ]]></isNotEmpty>
- <isNotEmpty property="createTime" prepend=","><![CDATA[ CREATETIME = #createTime# ]]></isNotEmpty>
- <isNotEmpty property="name" prepend=","><![CDATA[ USERNAME = #name# ]]></isNotEmpty>
- <isNotEmpty property="id" prepend=","><![CDATA[ USERID = #id# ]]></isNotEmpty>
- <isNotEmpty property="isFirstLogin" prepend=","><![CDATA[ ISFIRSTLOGIN = #isFirstLogin# ]]></isNotEmpty>
- </dynamic>
- <dynamic prepend="WHERE">
- USERID = #id#
- </dynamic>
- </update>