MyBatis中常用標簽的總結,簡單給出自己的總結
MyBatis映射文件中的標簽使用介紹
1.<select>:用於編寫查詢語句用的標簽
- id:表示當前<select>標簽的唯一標識
- parameterType:指定查詢限制條件的輸入類型,一般使用#{}實現的是向prepareStatement中的預處理語句中設置參數值
- resultType:指定查詢返回結果的輸出類型,如果返回的結果是一個實體類,必須要求實體類的屬性和表的字段名稱相同
- resultMap:也是一個輸出類型,配合<resultMap>標簽使用
- flushCache:設置查詢的時候是否清空緩存,默認為false
- useCache:將查詢結果放入緩存中,默認為true
- timeout:設置查詢返回結果的最大響應時間
- fetchSize:每次批量返回的結果行數。默認不設置
- statementType:STATEMENT、PREPARED或CALLABLE的一種,這會讓MyBatis使用選擇Statement、PreparedStatement或CallableStatement。默認值:PREPARED
- resultSetType:設置游標FORWARD_ONLY、SCROLL_SENSITIVE、SCROLL_INSENSITIVE中的一種。認不設置
2.<resultMap>:用於解決實體類中屬性和表字段名不相同的問題
- id:表示當前<resultMap>標簽的唯一標識
- result:定義表字段和實體類屬性的對應關系
- property:記錄實體類的屬性
- column:記錄表的字段名稱
3.<mapper>:每個映射文件的根標簽,重點關注<mapper>標簽中namespace屬性
4.<sql>:可以重用的SQL語句,可以被其他語句引用
<sql id="userColumns">id,username,password</sql> <select id="selectUsers" paramertType="int" resultType="hashmap"> select <include refid="userColumns"/> from some_table </select>
5.<insert>:用於編寫插入語句用的標簽
<insert id=”addMyUser” parameterType=”com.gxa.pojo.MyUser”> insert into MyUser (username, userpass) values (#{username}, #{userpass}) </insert>
6.<update>:用於編寫更新語句用的標簽
<update id=”updateMyUser” parameterType=”com.gxa.pojo.MyUser”> Update MyUser set username=#{userName} where userId=#{userId} </update>
7.<delete>:用於編寫刪除語句用的標簽
<delete id=”delMyUser” parameterType=”java.lang.Integer”> delete from myuser where userId = #{id} </delete>
8.<cache>:配置給定命名空間緩存
9.<cache-ref>:從其他命名空間引用緩存配置
10.MyBatis中用於實現動態SQL的元素主要有
- <if>
- <choose>(when,otherwise)
- <trim>
- <where>
- <set>
- <foreach>