天天看點

Ibatis <dynamic> 标簽 update 的相關問題總結

1.為了使用<dynamic >中 <isNotNull>和其他地方出現不必要的bug  我們在定義map 或者 TO時 将所有的類型都定義為原生态的類型,比如定義INTEGER。

2.在寫語句時很容易引入很難察覺的錯誤 比如“ : ” ,和“ : ” .這兩個字元在.java 檔案裡面會差別非常明顯,但是在xml中,肉眼是看不出來的。是以盡量統一,将标點用半角英文。

3.所有出現字段的地方,盡可能精确的制定類型,雖然有些地方看起來寫或者不寫都可以正常運作,這樣做可以減少出錯的幾率,減少後顧之憂。

4.貼出一段做範例。

TOMap:

[html]  view plain  copy

  1. <resultMap id="userTO" class="UserTO">  
  2.    <!--設定需要的資料表項與UserTO成員變量之間的對應關系  -->  
  3.    <result column="ID" jdbcType="int" javaType="java.lang.Integer" property="id"/>  
  4.    <result column="USERNAME" jdbcType="varchar" javaType="java.lang.String" property="username" />  
  5.    <result column="PASSWORD" jdbcType="varchar" javaType="java.lang.String" property="password"/>  
  6.    <result column="GENDER" jdbcType="Integer" javaType="java.lang.Integer" property="gender" />  
  7.    <result column="DATE_OF_BIRTH" jdbcType="date" javaType="java.util.Date" property="dateOfBirth"/>  
  8.    <result column="CREATED_DATE" jdbcType="datetime" javaType="java.util.Date" property="createdDate" />  
  9.    <result column="IMAGE_ID" jdbcType="Integer" javaType="java.lang.Integer" property="imageId"/>  
  10.  </resultMap>  

SQL:

[html]  view plain  copy

  1. <update id="updateUserinfoById" parameterClass="UserTO " >  
  2.     update USER  
  3.     <dynamic prepend="set">  
  4.       <isNotNull prepend="," property="password">PASSWORD = #password: VARCHAR#</isNotNull>  
  5.       <isNotNull prepend="," property="gender">GENDER = #gender: INTEGER#</isNotNull>   
  6.       <isNotNull prepend="," property="dateOfBirth">DATE_OF_BIRTH = #dateOfBirth: TIMESTAMP#</isNotNull>   
  7.       <isNotNull prepend="," property="imageId">IMAGE_ID = #imageId: INTEGER#</isNotNull>  
  8.       <isNotNull prepend="," property="status">STATUS = #status: INTEGER#</isNotNull>   
  9.     </dynamic>   
  10.     where ID=#id: INTEGER#  
  11.   </update>  

TO:

[html]  view plain  copy

  1. public class UserTO implements Constant {  
  2.     private Integer id;  
  3.     private String username;  
  4.     private String password;  
  5.     private Integer gender;  
  6.     private Integer imageId;  
  7.     private Date dateOfBirth;  
  8.     private Date createdDate;  

如果可以,可以做兩套TO,一個作為參數,另一個TO作為傳回值。這裡就不舉例了。

繼續閱讀