MyBatis映射文件(二)
目錄
一、select元素
1.參數介紹
2.resultType
(1)返回實體對象
(2)返回List集合
(3)返回Map集合,object>
(4)返回Map<主鍵類型,實體類>集合
3.setting設置自動映射
4.自定義resultMap映射
5.association
(1)聯合查詢
(2)association-嵌套結果集
(3)association-分步查詢&延遲加載
(4)Collection-集合類型&嵌套結果集
(5)Collection-分布查詢&延遲加載
(6)discriminator鑒別器
一、select元素
1.參數介紹
select元素用來定義查詢語句
? id:唯一標識符
?? ? ? ? ? ? 用來引用這條語句,需要和接口的方法名一致
? parametertype:參數類型
? ? ? ? ? ? ? ? ? ? ? ? 可以不傳,MyBatis會根據TypeHandler自動推斷
? resultType:返回值類型
? ? ? ? ? ? ? ? ? ? ? ? 別名或全類名,如果返回的是集合,定義集合中元素的類型。不能和resultMap同時使用。
2.resultType
(1)返回實體對象
<!-- public Employee getEmpByMap(Map<String, Object> map); --><select id="getEmpByMap" resultType="com.itheima.domain.Employee">select * from ${tableName} where id=${id} and last_name=#{lastName}</select>(2)返回List集合
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); --><!--resultType:如果返回的是一個集合,要寫集合中元素的類型 --><select id="getEmpsByLastNameLike" resultType="com.itheima.domain.Employee">select * from tbl_employee where last_name like #{lastName}</select>(3)返回Map<String,Object>集合
? 鍵值分別對應列名和對應的值
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); --><select id="getEmpByIdReturnMap" resultType="map">select * from tbl_employee where id=#{id}</select>(4)返回Map<主鍵類型,實體類>集合
? 鍵值分別對應主鍵名和對應的實體類
<!-- public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); ? --><select id="getEmpByLastNameLikeReturnMap"resultType="com.itheima.domain.Employee">select * from tbl_employee where last_name like #{lastName}</select>? @MapKey注解:可以指定封裝這個map的時候使用哪個屬性作為主鍵
@MapKey("lastName")public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);3.setting設置自動映射
-
autoMappingBehavior默認是PARTIAL,開啟自動映射的功能。唯一的要求是列名和javaBean屬性名一致
-
如果autoMappingBehavior設置為null則會取消自動映射
-
數據庫字段命名規范,POJO屬性符合駝峰命名法,如A_COLUMN===aColumn,我們可以開啟自動駝峰命名規則映射功能,mapUnderscoreToCamelCase=true。
4.自定義resultMap映射
(1)屬性
-
type:自定義規則的Java類型
-
id:唯一標識,用于標識一個result map
-
autoMapping:如果設置這個屬性,MyBatis將會為這個ResultMap開啟或者關閉自動映射。這個屬性會覆蓋全局的屬性 autoMappingBehavior。默認值為:unset。
5.association
場景:
? 查詢Employee的同時查詢員工對應的部門
(1)聯合查詢
? 級聯屬性封裝結果集
<resultMap type="com.itheima.domain.Employee" id="MyDifEmp"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!-- dept為Employee的 Department dept成員變量名Department有id和departmentName屬性--><result column="did" property="dept.id"/><result column="dept_name" property="dept.departmentName"/></resultMap>(2)association-嵌套結果集
<resultMap type="com.itheima.domain.Employee" id="MyDifEmp2"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><!-- association可以指定聯合的javaBean對象property="dept":指定哪個屬性是聯合的對象javaType:指定這個屬性對象的類型[不能省略]--><association property="dept" javaType="com.itheima.domain.Department"><id column="did" property="id"/><result column="dept_name" property="departmentName"/></association></resultMap>(3)association-分步查詢&延遲加載
-
先按照員工id查詢員工信息
-
根據查詢出來的d_id去部門表查出部門信息
-
部門設置到員工中
-
開啟延遲加載和屬性按需加載需要進行如下配置:
(4)Collection-集合類型&嵌套結果集
<!--嵌套結果集的方式,使用collection標簽定義關聯的集合類型的屬性封裝規則 --><resultMap type="com.itheima.domain.Department" id="MyDept"><id column="did" property="id"/><result column="dept_name" property="departmentName"/><!-- collection定義關聯集合類型的屬性的封裝規則 ofType:指定集合里面元素的類型--><collection property="emps" ofType="com.itheima.domain.Employee"><!-- 定義這個集合中元素的封裝規則 --><id column="eid" property="id"/><result column="last_name" property="lastName"/><result column="email" property="email"/><result column="gender" property="gender"/></collection></resultMap>(5)Collection-分布查詢&延遲加載
<resultMap type="com.itheima.domain.Department" id="MyDeptStep"><id column="id" property="id"/><id column="dept_name" property="departmentName"/><collection property="emps" select="com.itheima.domain.EmployeeMapperPlus.getEmpsByDeptId"column="{id}" fetchType="lazy"></collection><!--多列的值傳遞過去:將多列的值封裝map傳遞;column="{key1=column1,key2=column2}"fetchType="lazy":表示使用延遲加載;- lazy:延遲- eager:立即--> </resultMap>(6)discriminator鑒別器
<!-- <discriminator javaType=""></discriminator>鑒別器:mybatis可以使用discriminator判斷某列的值,然后根據某列的值改變封裝行為封裝Employee:如果查出的是女生:就把部門信息查詢出來,否則不查詢;如果是男生,把last_name這一列的值賦值給email;--><resultMap type="com.itheima.domain.Employee" id="MyEmpDis"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="email" property="email"/><result column="gender" property="gender"/><!--column:指定判定的列名javaType:列值對應的java類型 --><discriminator javaType="string" column="gender"><!--女生 resultType:指定封裝的結果類型;不能缺少。/resultMap--><case value="0" resultType="com.itheima.domain.Employee"><association property="dept" select="com.itheima.domain.DepartmentMapper.getDeptById"column="d_id"></association></case><!--男生 ;如果是男生,把last_name這一列的值賦值給email; --><case value="1" resultType="com.itheima.domain.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="last_name" property="email"/><result column="gender" property="gender"/></case></discriminator></resultMap>?
總結
以上是生活随笔為你收集整理的MyBatis映射文件(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis映射文件(一)
- 下一篇: MyBatis Generator:代码