1.為了使用<dynamic >中 <isNotNull>和其他地方出現不必要的bug 我們在定義map 或者 TO時 将所有的類型都定義為原生态的類型,比如定義INTEGER。
2.在寫語句時很容易引入很難察覺的錯誤 比如“ : ” ,和“ : ” .這兩個字元在.java 檔案裡面會差別非常明顯,但是在xml中,肉眼是看不出來的。是以盡量統一,将标點用半角英文。
3.所有出現字段的地方,盡可能精确的制定類型,雖然有些地方看起來寫或者不寫都可以正常運作,這樣做可以減少出錯的幾率,減少後顧之憂。
4.貼出一段做範例。
TOMap:
[html] view plain copy
- <resultMap id="userTO" class="UserTO">
- <!--設定需要的資料表項與UserTO成員變量之間的對應關系 -->
- <result column="ID" jdbcType="int" javaType="java.lang.Integer" property="id"/>
- <result column="USERNAME" jdbcType="varchar" javaType="java.lang.String" property="username" />
- <result column="PASSWORD" jdbcType="varchar" javaType="java.lang.String" property="password"/>
- <result column="GENDER" jdbcType="Integer" javaType="java.lang.Integer" property="gender" />
- <result column="DATE_OF_BIRTH" jdbcType="date" javaType="java.util.Date" property="dateOfBirth"/>
- <result column="CREATED_DATE" jdbcType="datetime" javaType="java.util.Date" property="createdDate" />
- <result column="IMAGE_ID" jdbcType="Integer" javaType="java.lang.Integer" property="imageId"/>
- </resultMap>
SQL:
[html] view plain copy
- <update id="updateUserinfoById" parameterClass="UserTO " >
- update USER
- <dynamic prepend="set">
- <isNotNull prepend="," property="password">PASSWORD = #password: VARCHAR#</isNotNull>
- <isNotNull prepend="," property="gender">GENDER = #gender: INTEGER#</isNotNull>
- <isNotNull prepend="," property="dateOfBirth">DATE_OF_BIRTH = #dateOfBirth: TIMESTAMP#</isNotNull>
- <isNotNull prepend="," property="imageId">IMAGE_ID = #imageId: INTEGER#</isNotNull>
- <isNotNull prepend="," property="status">STATUS = #status: INTEGER#</isNotNull>
- </dynamic>
- where ID=#id: INTEGER#
- </update>
TO:
[html] view plain copy
- public class UserTO implements Constant {
- private Integer id;
- private String username;
- private String password;
- private Integer gender;
- private Integer imageId;
- private Date dateOfBirth;
- private Date createdDate;
如果可以,可以做兩套TO,一個作為參數,另一個TO作為傳回值。這裡就不舉例了。