Sql映射文件
MyBatis真正的力量是在映射語句中。這里是奇跡發生的地方。對於所有的力量,SQL映射的XML文件是相當的簡單。當然如果你將它們和對等功能的JDBC代碼來比較,你會發現映射文件節省了大約95%的代 碼量。MyBatis的構建就是聚焦於SQL的,使其遠離於普通的方式。
SQL映射文件有很少的幾個頂級元素(按照它們應該被定義的順序):
>mapper:映射文件的根元素節點,只有一個屬性namespace命名空間,用於區分不同的mapper,全局唯一 ,namespace綁定的DAO接口全名稱,即面向接口編程。這里的mapper就相當於接口的實現類。
- cache - 配置給定命名空間的緩存。
- cache-ref – 從其他命名空間引用緩存配置。
- resultMap – 最復雜,也是最有力量的元素,用來描述如何從數據庫結果集中來加載你的對象。
- parameterMap – 已經被廢棄了!老式風格的參數映射。內聯參數是首選,這個元素可能在將來被移除。這里不會記錄。
- sql – 可以重用的SQL塊,也可以被其他語句引用。
- insert – 映射插入語句
- update – 映射更新語句
- delete – 映射刪除語句
- select – 映射查詢語句
一:使用select完成但條件查詢
使用工具idea和mysql數據庫
創建實體類
public class student { private int stuId; private String stuName; private grade getGrade; private int stuAge; public grade getGetGrade() { return getGrade; } public void setGetGrade(grade getGrade) { this.getGrade = getGrade; } public int getStuAge() { return stuAge; } public student(int id,String name){ } public student(){} public void setStuAge(int stuAge) { this.stuAge = stuAge; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } }
使用select完成條件查詢
一:首先配置mapper使用resultType
<!--模糊查詢 使用resultType返回結果集--> <select id="getAllStudentByLike" parameterType="String" resultType="stu"> select * from student where stuName like CONCAT('%',#{stuName},'%')
</select>
測試類
public void Test() throws IOException { studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class); List<student> list = dao.getAllStudentByLike("z"); for (student item:list) { System.out.println("----------"+item.getStuName()); } }
另外parameterType支持的復雜類型除了javaBean之外,還包括Map類型
即修改Mapper
<!--模糊查詢--> <select id="getAllStudentByLike" parameterType="Map" resultType="stu"> select * from student where stuName like CONCAT('%',#{stuName},'%') </select>
然后再測試類里創建一個 HashMap集合直接作為方法參數即可
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class); Map<String,String> userMap = new HashMap<String, String>(); userMap.put("stuName","z"); List<student> list = dao.getAllStudentByLike(userMap); for (student item:list) { System.out.println("----------"+item.getStuName()); }
不過map集合的key值必須和類中的字段名相同。
二:使用resultMap完成兩表查詢
比如學生表里關聯班級表的主鍵id,如果使用resultType只能展示其id但在實際中往往關注的是班級名稱,所有需要使用resultMap映射自定義結果。
<resultMap id="studentMap" type="entity.student">
<id property="stuId" column="stuId"></id> <result property="stuName" column="stuName"></result> <result property="gradeName" column="gradeName"> </resultMap>
//sql語句
select * from student,grade
resultType直接表示 返回 類型 ,包括基礎類型和復雜數據類型
resultMap則是對外部resultMap的引用,對應resultMap的id 表示返回結果映射到 哪一個resultMap。:他的應用場景是:數據庫字段信息與對象屬性不一致或者需要做復雜的聯合查詢以便自由控制映射結果 。
另外在 MyBatis的select元素中,resultType和resultMap本質上是一樣的,都是Map數據結構。但是 二者不能同時 存在。
三:使用resultMap的自動映射級別
MyBatis中分為三個映射級別
>NONE:禁止自動匹配
>PARTIAL:(默認):自動匹配所有屬性有內部嵌套(association,collection)的除外
>FULL:自動匹配所有
在大配置里設置autoMappingBehavior
<settings> <!--設置resultMap的自動映射級別為Full(自動匹配所有)--> <setting name="autoMappingBehavior" value="FULL" /> <!--FULL要大寫··--> </settings>
設置autoMappingBehavior的值為FULL時就不需要配置resultMap下的節點,他會根據數據庫自動匹配
四:使用update完成修改
<update id="update"> update student set stuName=#{0} where stuId=#{1} </update>
這里使用占位符比較簡單的一種作為參數,在測試類中就直接填參就行了
五:使用映射復雜類型的屬性association
前面的result只能映射到javaBean的某個“簡單類型”屬性,基礎數據類型和包裝類等/
但要映射復雜類型的屬性時需要用到assocoation 復雜類xing:即一個javaBean里有另一個javaBean,但是association僅處理一對一的關聯關系
private int stuId; private String stuName; private grade getGrade; private int stuAge;
。。。。。省略封裝
<resultMap id="studentMap" type="entity.student"> <!-- <id property="stuId" column="stuId"></id> <result property="stuName" column="stuName"></result>--> <!--關聯另一個 屬性--> <association property="getGrade" javaType="grade"> <!-- <id property="gradeId" javaType="Integer" column="gradeId"></id> <result property="gradeName" javaType="String" column="gradeName"></result>--> </association> </resultMap>
<!--這里使用了自動匹配-->
<select id="getAllStudent" resultMap="studentMap"> SELECT * FROM student,grade WHERE student.stuGrade=grade.gradeId </select>
測試類里直接調用即可
六:前面說到association僅處理一對一的管理關系
如果要處理一對多的關系,則需要使用collection,它與 association元素差不多,但它映射的屬性是一個集合列表,即javaBean內部嵌套一個復雜數據類型屬性。
javaBean
private int gradeId; private String gradeName; private List<student> gatStudent;
<resultMap id="gradeMap" type="grade"> <!--<id property="gradeId" column="gradeId"></id> <result property="gradeName" column="gradeName"></result>--> <collection property="gatStudent" ofType="stu"> <!-- <id property="stuId" column="stuId"></id> <result property="stuName" column="stuName"></result>--> </collection> </resultMap>
<!--查詢對應年級的student-->
<select id="getAll" resultMap="gradeMap">
select * from student,grade where stuGrade = gradeId and gradeId=1
</select>
完: