日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis映射文件(二)

發布時間:2025/3/20 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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。

<resultMap type="com.itheima.domain.Employee" id="MySimpleEmp"><!--指定主鍵列的封裝規則id:定義主鍵底層有優化;column:指定哪一列property:指定對應的javaBean屬性--><id column="id" property="id"/><!-- 定義普通列封裝規則 --><result column="last_name" property="lastName"/><!-- 其他不指定的列會自動封裝:我們只要寫resultMap就把全部的映射規則都寫上。 --><result column="email" property="email"/><result column="gender" property="gender"/></resultMap><!-- resultMap:自定義結果集映射規則; --><!-- public Employee getEmpById(Integer id); --><select id="getEmpById" ?resultMap="MySimpleEmp">select * from tbl_employee where id=#{id}</select>

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去部門表查出部門信息

  • 部門設置到員工中

<resultMap type="com.itheima.domain.Employee" id="MyEmpByStep"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="email" property="email"/><result column="gender" property="gender"/><!-- association定義關聯對象的封裝規則select:表明當前屬性是調用select指定的方法查出的結果column:指定將哪一列的值傳給這個方法流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,并封裝給property指定的屬性--><association property="dept" select="com.itheima.domain.DepartmentMapper.getDeptById"column="d_id"></association>
  • 開啟延遲加載和屬性按需加載需要進行如下配置:

<!--lazyLoadingEnabled:是否開啟延遲加載功能aggressiveLazyLoading:對任意延遲屬性的調用會使帶有延遲加載屬性的對象完整加載 --><settings> <setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/> </settings>

(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映射文件(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。