前面學習的都是一些配置,mybatis的精華也就集中在SQL的映射文件上,相比實現相同功能的jdbc代碼,節約了95%的代碼量。
<!-- 配置給定命名空間的緩存 -->
<cache></cache>
<!-- 從其他命名空間引用緩存配置 -->
<cache-ref namespace="" />
<!-- 描述如何將db中查詢的結果集加載成對象 -->
<resultMap type="" id="">
<constructor>
<idArg />
<arg />
<arg />
<arg />
</constructor>
</resultMap>
<!-- 定義可重用的sql語句 -->
<sql id=""></sql>
<!-- 映射dml語句 -->
<insert id=""></insert>
<select id=""></select>
<update id=""></update>
<delete id=""></delete>
一、select可以可以說是使用最多的元素,使用也很簡單
- <select id="getUserById" parameterType="int" resultType="User">
- select * from tbl_user where id = #{id}
- </select>
< select>元素中的一些屬性(紅色為用的較多的屬性)
id | 在命名空間中唯一的標識符,可以被用來引用這條語句 |
parameterType | 將會傳入這條語句的參數類的完全限定名或別名。 |
resultType | 從這條語句中返回的期望類型的類的完全限定名或別名。 意集合情形,那應該是集合可以包含的類型, 而不能是集合本身。使用 resultType或 resultMap, 但不能同時使用。(可以是基本類型int等, 復合類型User,集合類型map,list等) |
resultMap | 命名引用外部的resultMap。返回map是MyBat is 最具力量的特性,對其有一個很好的理解的話, 多復雜映射的情形就能被解決了。 使用 resultMap 或resultType,但不能同時使用。 |
flushCache | 將其設置為 true,不論語句什么時候被帶哦用, 都會導致緩存被清空。默認值:false |
useCache | 將其設置為 true,將會導致本條語句的結果被緩存。 默認值:true |
timeout | 這個設置驅動程序等待數據庫返回請求結果,并拋出 異常時間的最大等待值。默認不設置(驅動自行處理) |
fetchSize | 這是暗示驅動程序每次批量返回的結果行數。 默認不設置(驅動自行處理) |
statementType | STATEMENT,PREPARED 或 CALLABLE 的一種。 這會讓 MyBat is使用選擇使用 Statement,PreparedStatement或 CallableStatement。 默認值:PREPARED。 |
resultSetType | FORWARD_ONLY, SCROLL_SENSITIVE, SCROLL_INSENSITIVE中的一種。 默認是不設置(驅動自行處理) |
| |
| |
二、insert插入數據庫,進行插入操作時主要是要拿到插入數據自增的主鍵
id | 在命名空間中唯一的標識符,可以被用來引用這條語句。 |
parameterType
| 將會傳入這條語句的參數類的完全限定名或別名 |
flushCache | 將其設置為 true,不論語句什么時候被帶哦用,都會導致緩存被清空。默認值:false。 |
timeout | 這個設置驅動程序等待數據庫返回請求結果,并拋出異常時間的最大等待值。 默認不設置(驅動自行處理)。 |
statementType | STATEMENT,PREPARED 或 CALLABLE 的一種。這會讓 MyBat is使用選擇使用 Statement,PreparedStatement 或 CallableStatement。默認值:PREPARED。 |
useGeneratedKeys | (僅對 insert 有 用 ) 這 會 告 訴 MyBat is 使用 JDBC 的getGeneratedKeys 方法來取出由數據(比如:像 MySQL 和 SQL Server 這樣的數據庫管理系統的自動遞增字段)內部生成的主鍵。默認值:false。 |
keyProperty | (僅對insert有用) 標記一個屬性, MyBat is會通過getGeneratedKeys或者通過 insert 語句的selectKey 子元素設置它的值。默認:不設置。 |
| |
在進行insert,update,delete之后session要進行commit操作,不然數據庫不會更新到數據庫
insert的Demo
另一種獲取主鍵的方法
- <insert id="addUserLastId" parameterType="User">
- <selectKey resultType="int" order="AFTER" keyProperty="id">
- SELECT LAST_INSERT_ID() AS id
- </selectKey>
- insert into tbl_user(name,age) values(#{name},#{age})
- </insert>
- <insert id="addUser" parameterType="User" keyProperty="id" useGeneratedKeys="true" >
- insert into tbl_user(name,age) values(#{name},#{age})
- </insert>
- @Test
- public void testInsert() throws IOException {
- UserMapper mapper = session.getMapper(UserMapper.class) ;
- User user = new User() ;
- user.setName("zhangss") ;
- user.setAge(22) ;
- int i = mapper.addUser(user) ;
- session.commit() ;
- session.close() ;
- System.out.println("id:"+i+"--"+user.getId());
- }
update的Demo
- <update id="updateUser" parameterType="User">
- update tbl_user set name = #{name} ,age = #{age} where id = #{id}
- </update>
- @Test
- public void testUpdate(){
- UserMapper mapper = session.getMapper(UserMapper.class) ;
- User user = new User() ;
- user.setId(27) ;
- user.setName("zhang27") ;
- user.setAge(227) ;
- int i = mapper.updateUser(user) ;
- session.commit() ;
- session.close() ;
- System.out.println("id:"+i+"--"+user.getId());
- }
三、sql定義可重用的sql語句
- <sql id="selectItem">id,name,age</sql>
- <select id="getUserById" parameterType="int" resultType="User">
-
-
-
-
- select <include refid="selectItem"/> from tbl_user where id = #{id}
- </select>
四、Parameter
#{department, mode=OUT, jdbcType=CURSOR, javaType=ResultSet,resultMap=departmentResultMap}
五、resultMap所做的工作就是將從數據庫中獲取的ResultSet結果集放入指定的對象中,避免大量的setter getter代碼,實現自動裝配的功能,實現結果的映射
1、簡單映射
- <select id=”selectUsers” parameterType=”int” resultType=”hashmap”>
- select id, username, hashedPassword from some_table where id = #{id}
- </select>
所有列被自動映射到 HashMap 的鍵上,key為列名,value為數據庫中的數據
- <select id=”selectUsers” parameterType=”int” resultType=”com.someapp.model.User”>
- select id, username, hashedPassword from some_table where id = #{id}
- </select>
將所有從數據庫中取得數據自動裝配到JavaBean中,如果列名與屬性名相同,則無需作任務的干預即可完成裝配。
如果數據庫中的列名與javabean的屬性名稱不同可以在查詢的時候取別名,如
- <select id=”selectUsers” parameterType=”int” resultType=”User”>
- select user_id as “id”, user_name as “userName”, hashed_password as “hashedPassword” from some_table where id = #{id}
- </select>
也可以使用resultMap進行映射的指定
- <resultMap id="userResultMap" type="User">
- <id property="id" column="user_id" />
- <result property="username" column="username"/>
- <result property="password" column="password"/>
- </resultMap>
-
- <select id=”selectUsers” parameterType=”int” resultMap=”userResultMap”>
- select user_id, user_name, hashed_password from some_table where id = #{id}
- </select>
2、resultMap高級映射