1. 程式人生 > >ibatis中動態SQL_update

ibatis中動態SQL_update

如何知道上一篇的動態INSERT的話,動態UPDATE就很簡單了,直接上XML

注意:如果<isNotEmpty>是寫在一行的話,並且是第一個的話

<isNotEmpty property="realName" prepend=","><![CDATA[ REALNAME = #realName# ]]></isNotEmpty>

內容注意中間加個空格,不然會出現 update role setname=xxx 直接報錯

Xml程式碼  收藏程式碼
  1. <update id="updateRole" parameterClass="com.mmblue.model.Role"
    >  
  2.     <![CDATA[ 
  3.         UPDATE role 
  4.         SET 
  5.             gmt_modified = to_date(#gmtModified#,'yyyy-mm-dd hh24:mi:ss'), 
  6.             modifier = #modifier# 
  7.     ]]>  
  8.         <dynamic>  
  9.             <isNotEmpty property="isDeleted" prepend=",">  
  10.                 <![CDATA[
     
  11.                     is_deleted = #isDeleted# 
  12.                 ]]>  
  13.             </isNotEmpty>  
  14.             <isNotEmpty property="roleCode" prepend=",">  
  15.                 <![CDATA[  
  16.                     role_code = #roleCode# 
  17.                 ]]>  
  18.             </isNotEmpty>
      
  19.             <isNotEmpty property="roleName" prepend=",">  
  20.                 <![CDATA[  
  21.                     role_name = #roleName# 
  22.                 ]]>  
  23.             </isNotEmpty>  
  24.             <isNotEmpty property="deptId" prepend=",">  
  25.                 <![CDATA[  
  26.                     dept_id = #deptId# 
  27.                 ]]>  
  28.             </isNotEmpty>  
  29.         </dynamic>  
  30.     <![CDATA[ 
  31.         WHERE id = #id# 
  32.     ]]>  
  33. </update>  

2.再來個全部遍歷的

Xml程式碼  收藏程式碼
  1. <update id="updateUser" parameterClass="com.mmblue.domain.User">  
  2.     UPDATE MYIVR_TUSER  
  3.     <dynamic prepend="SET ">  
  4.                 <isNotEmpty property="realName" prepend=","><![CDATA[ REALNAME = #realName# ]]></isNotEmpty>  
  5.                 <isNotEmpty property="password" prepend=","><![CDATA[ PASSWORD = #password# ]]></isNotEmpty>  
  6.                 <isNotEmpty property="projectId" prepend=","><![CDATA[ PROJECT_ID = #projectId# ]]></isNotEmpty>  
  7.                 <isNotEmpty property="isEable" prepend=","><![CDATA[ ISENABLE = #isEable# ]]></isNotEmpty>  
  8.                 <isNotEmpty property="createTime" prepend=","><![CDATA[ CREATETIME = #createTime# ]]></isNotEmpty>  
  9.                 <isNotEmpty property="name" prepend=","><![CDATA[ USERNAME = #name# ]]></isNotEmpty>  
  10.                 <isNotEmpty property="id" prepend=","><![CDATA[ USERID = #id# ]]></isNotEmpty>  
  11.                 <isNotEmpty property="isFirstLogin" prepend=","><![CDATA[ ISFIRSTLOGIN = #isFirstLogin# ]]></isNotEmpty>  
  12.     </dynamic>  
  13.     <dynamic prepend="WHERE">  
  14.         USERID = #id#  
  15.     </dynamic>  
  16. </update>