MyBatis處理映射關系

在Mybatis實現數據處理過程中,字段名符合數據庫的規則,屬性一般為駝峰規則,因此字段名和屬性名通常不一致,此時可以通過以下兩種方式對數據庫字段進行映射處理:

  • 為字段起別名,保證和實體類中的屬性名一致
  • 在滿足駝峰轉換規則時
    在MyBatis的核心配置文件中設置一個全局配置信息mapUnderscoreToCamelCase,可以在查詢表中數據時,自動將數據庫字段名轉換為駝峰
  • 通過resultMap進行字段映射處理

1 利用字段別名映射

List<Emp> listEmp();
    <!--通過字段別名解決字段名和屬性名的映射問題--><select id="listEmp" resultType="Emp">select id, user_name as userName, pass_word as passWord, sex, dept_id as deptId from t_emp</select>

2 利用駝峰轉換規則映射

在Mybatis核心配置文件中,設置全局的駝峰轉換,此時框架會根據駝峰規則自動將數據庫字段和實體屬性進行映射。

  • 核心配置
<settings><!-- 將表中的下劃線字段自動映射為駝峰命名的屬性字段 --><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
  • 數據查詢
<!--通過駝峰配置,此時Mybatis框架會自動將字段和屬性進行映射,如user_name映射到userName屬性上。-->
<select id="listEmp" resultType="Emp">select id, user_name, pass_word, sex, dept_id from t_emp
</select>

此處resultType直接使用實體類別名,因為存在如下的配置:

<typeAliases><!--typeAlias:設置某個具體的類型的別名屬性:type:需要設置別名的類型的全類名alias:設置此類型的別名若不設置此屬性,該類型擁有默認的別名,即類名且不區分大小寫; 若設置此屬性,此時該類型的別名只能使用alias所設置的值--><!--<typeAlias type="com.giser.mybatis.bean.User"></typeAlias>--><!--<typeAlias type="com.giser.mybatis.bean.User" alias="abc"></typeAlias>--><!--以包為單位,設置改包下所有的類型都擁有默認的別名,即類名且不區分大小寫--><package name="com.giser.pojo"/>
</typeAliases>

3 利用resultMap映射

 <!--resultMap設置自定義映射關系id: 自定義映射的唯一標識type: 映射的實體類類型子標簽:id: 設置主鍵的映射關系result:設置普通字段的映射關系屬性:property: 設置映射關系中實體類的屬性column: 設置映射關系中表字段--><resultMap id="empMap" type="Emp"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /><result property="deptId" column="dept_id" /></resultMap><!--使用ResultMap解決字段名和屬性名的映射問題--><select id="listEmp" resultMap="empMap">select * from t_emp</select>

3.1 多對一映射

3.1.1 級聯映射

存在如下實體:

public class Dept {private Integer id;private String deptName;private String deptNo;// getter setter
}public class EmpDept {private Integer id;private String userName;private String passWord;private String sex;private Integer deptId;private Dept dept;// getter setter
}
List<EmpDept> listEmpDeptById(@Param("eid")Integer eid);
<select id="listEmpDeptById" resultMap="empDeptMap">select * from t_emp left join t_dept on t_emp.dept_id = t_dept.id where t_emp.id = #{eid}
</select>

此時需要級聯映射,如下:

<!--通過級聯屬性賦值
-->
<resultMap id="empDeptMap" type="EmpDept"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /><result property="deptId" column="dept_id" /><result property="dept.id" column="id" /><result property="dept.deptName" column="dept_name" /><result property="dept.deptNo" column="dept_no" />
</resultMap>
3.1.2 使用association實現多對一映射

如員工和部門的關系就是多對一,多個員工可能在同一個部門。此時可以通過上述的級聯屬性賦值方式進行映射,還可以通過association處理映射關系。

<!--通過association處理多對一映射關系
-->
<resultMap id="empDeptMapTwo" type="EmpDept"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /><result property="deptId" column="dept_id" /><!--association處理多對一的映射關系property: 需要處理多對一映射關系的實體屬性名javaType: 實體屬性的類型--><association property="dept" javaType="Dept"><id property="id" column="id" /><result property="deptName" column="dept_name" /><result property="deptNo" column="dept_no" /></association>
</resultMap><select id="listEmpDeptById" resultMap="empDeptMapTwo">select * from t_emp left join t_dept on t_emp.dept_id = t_dept.id where t_emp.id = #{eid}
</select>
3.1.3 使用association分步查詢實現多對一映射
List<EmpDept> listEmpDeptByStep(@Param("eid") Integer eid);
<!--多對一映射關系處理方式二:通過分步查詢
-->
<resultMap id="empDeptMapStep" type="EmpDept"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /><result property="deptId" column="dept_id" /><!--select :設置分步查詢的Sql的唯一標識(namespace.SQLId或mapper接口的全類名.方法名)column :設置分步查詢的條件--><association property="dept"
select="com.giser.mapper.DeptMapper.selectEmpAndDeptByStepTwo"column="dept_id"></association>
</resultMap><select id="listEmpDeptByStep" resultMap="empDeptMapStep">select * from t_emp left join t_dept on t_emp.dept_id = t_dept.id where t_emp.id = #{eid}
</select>
/*** 分步查詢第二步* @param deptId* @return*/
Dept selectEmpAndDeptByStepTwo(@Param("deptId")Integer deptId);
<select id="selectEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where id = #{deptId}
</select>
  • 延遲加載
    分步查詢的優點:可以實現延遲加載,但是必須在核心配置文件中設置全局配置信息:
    lazyLoadingEnabled:延遲加載的全局開關。當開啟時,所有關聯對象都會延遲加載
    aggressiveLazyLoading:當開啟時,任何方法的調用都會加載該對象的所有屬性。 否則,每個屬性會按需加載
    此時就可以實現按需加載,獲取的數據是什么,就只會執行相應的sql。此時可通過association和collection中的fetchType屬性設置當前的分步查詢是否使用延遲加載,fetchType=“lazy(延遲加載)|eager(立即加載)”。
    在mybatis-config.xml中,開啟懶加載配置
    <settings><!-- 開啟延遲加載 --><setting name="lazyLoadingEnabled" value="true" /></settings>

在映射關聯屬性時,設置fetchType=“lazy”

    <!--多對一映射關系處理方式二:通過分步查詢--><resultMap id="empDeptMapStep" type="EmpDept"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /><result property="deptId" column="dept_id" /><!--select :設置分布查詢的Sql的唯一標識(namespace.SQLId或mapper接口的全類名.方法名)column :設置分布查詢的條件fetchType: 延遲加載 ,只有當全局配置lazyLoadingEnabled設置為true時,fetchType=lazy才會延遲加載,否則都是立即加載fetchType="eager|lazy" eager:立即加載; lazy:延遲加載 且此處設置的優先級高于全局配置--><association property="dept"select="com.giser.mapper.DeptMapper.selectEmpAndDeptByStepTwo"column="dept_id"fetchType="lazy"></association></resultMap>

3.2 一對多映射

3.2.1 使用collection實現一對多映射

存在如下實體:

public class DeptEmp {private Integer id;private String deptName;private String deptNo;private List<Emp> emps;// getter setter
}
public class Emp {private Integer id;private String userName;private String passWord;private String sex;private Integer deptId;// getter setter
}
/*** 一對多關系查詢* @param deptId* @return*/
DeptEmp selectDeptAndEmpByDeptId(@Param("deptId")Integer deptId);
<resultMap id="deptAndEmpResultMap" type="DeptEmp"><id property="id" column="id" /><result property="deptName" column="dept_name" /><result property="deptNo" column="dept_no" /><!--collection : 處理一對多關系映射property: 集合屬性字段名稱ofType : 表示該集合中存儲的數據類型--><collection property="emps" ofType="Emp"><id property="id" column="id" /><result property="userName" column="user_name" /><result property="passWord" column="pass_word" /><result property="sex" column="sex" /></collection>
</resultMap><select id="selectDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">select * from t_dept left join t_emp on t_dept.id = t_emp.dept_id where t_dept.id = #{deptId}
</select>
3.2.2 使用分步查詢實現一對多映射
DeptEmp selectDeptAndEmpByStepOne(@Param("deptId")Integer deptId);
<resultMap id="selectDeptAndEmpByStepResultMap" type="DeptEmp"><id property="id" column="id" /><result property="deptName" column="dept_name" /><result property="deptNo" column="dept_no" /><collection property="emps"
select="com.giser.mapper.EmpMapper.selectDeptAndEmpByStepTwo"column="id"></collection>
</resultMap><select id="selectDeptAndEmpByStepOne" resultMap="selectDeptAndEmpByStepResultMap">select * from t_dept where id = #{deptId}
</select>
Emp selectDeptAndEmpByStepTwo(@Param("deptId")Integer deptId);
<select id="selectDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp where dept_id = #{deptId}
</select>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/211824.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/211824.shtml
英文地址,請注明出處:http://en.pswp.cn/news/211824.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

lv11 嵌入式開發 IIC(下) 20

目錄 1 Exynos4412下IIC控制器介紹 1.1 總覽 1.2 特征 1.3 工作框圖 1.4 其他內容介紹 1.5 四種工作模式寄存器流程 2 IIC寄存器詳解 2.1 概述 2.2 控制寄存器 2.3 狀態寄存器 2.4 地址寄存器 2.5 數據寄存器 2.6 其他寄存器 3 MPU06050 3.1 簡介 3.2 MPU6050主…

HJ103 Redraiment的走法

題目&#xff1a; HJ103 Redraiment的走法 題解&#xff1a; dfs 暴力搜索 枚舉數組元素&#xff0c;作為起點如果后續節點大于當前節點&#xff0c;繼續向后搜索記錄每個起點的結果&#xff0c;求出最大值 public int getLongestSub(int[] arr) {int max 0;for (int i 0…

data_loader返回的每個batch的數據大小是怎么計算得到的?

data_loader是一個通用的術語&#xff0c;用于表示數據加載器或數據批次生成器。它是在機器學習和深度學習中常用的一個概念。 一、data loader 數據加載器&#xff08;data loader&#xff09;是一個用于加載和處理數據集的工具&#xff0c;它可以將數據集劃分為小批次&#…

提示(Prompt)工程中提示詞的開發優化基礎概念學習總結

本文對學習過程進行總結&#xff0c;僅對基本思路進行說明&#xff0c;結果在不同的模型上會有差異。 提示與提示工程 提示&#xff1a;指的是向大語言模型輸入的特定短語或文本&#xff0c;用于引導模型產生特定的輸出&#xff0c;以便模型能夠生成符合用戶需求的回應。 提示…

內存學習——堆(heap)

目錄 一、概念二、自定義malloc函數三、Debug運行四、heap_4簡單分析4.1 heap管理鏈表結構體4.2 堆初始化4.3 malloc使用4.4 free使用 一、概念 內存分為堆和棧兩部分&#xff1a; 棧&#xff08;Stack&#xff09;是一種后進先出&#xff08;LIFO&#xff09;的數據結構&…

AVFormatContext封裝層:理論與實戰

文章目錄 前言一、封裝格式簡介1、FFmpeg 中的封裝格式2、查看 FFmpeg 支持的封裝格式 二、API 介紹三、 實戰 1&#xff1a;解封裝1、原理講解2、示例源碼 13、運行結果 14、示例源碼 25、運行結果 2 四、 實戰 2&#xff1a;轉封裝1、原理講解2、示例源碼3、運行結果 前言 A…

文章解讀與仿真程序復現思路——電力系統自動化EI\CSCD\北大核心《考慮電力-交通交互的配電網故障下電動汽車充電演化特性》

這個標題涉及到電力系統、交通系統和電動汽車充電的復雜主題。讓我們逐步解讀&#xff1a; 考慮電力-交通交互的配電網故障&#xff1a; 電力-交通交互&#xff1a; 指的是電力系統和交通系統之間相互影響、相互關聯的關系。這可能涉及到電力需求對交通流量的影響&#xff0c;反…

回溯算法之N皇后

一 什么是回溯算法 回溯算法&#xff08;Backtracking Algorithm&#xff09;是一種用于解決組合優化問題的算法&#xff0c;它通過逐步構建候選解并進行驗證&#xff0c;以尋找所有滿足特定條件的解。回溯算法通常應用于在給定約束條件下枚舉所有可能解的問題&#xff0c;如…

Git—文件添加查看刪除修改

目錄 1.添加文件—場景一 2.查看.git文件 3.添加文件—場景三 4.修改文件 5.版本回退 6.撤銷修改 7.刪除文件 1.添加文件—場景一 在包含.git的目錄下新建?個ReadMe文件&#xff0c;我們可以使用 git add 命令可以將文件添加到暫存 區&#xff1a; ●添加一個或多個文…

Matlab數學建模算法之小波神經網絡詳解

&#x1f517; 運行環境&#xff1a;Matlab &#x1f6a9; 撰寫作者&#xff1a;左手の明天 &#x1f947; 精選專欄&#xff1a;《python》 &#x1f525; 推薦專欄&#xff1a;《算法研究》 &#x1f510;#### 防偽水印——左手の明天 ####&#x1f510; &#x1f497; 大家…

vue的屬性

key 預期&#xff1a;number | string | boolean (2.4.2 新增) | symbol (2.5.12 新增) key 的特殊 attribute 主要用在 Vue 的虛擬 DOM 算法&#xff0c;在新舊 nodes 對比時辨識 VNodes。如果不使用 key&#xff0c;Vue 會使用一種最大限度減少動態元素并且盡可能的嘗試就地…

2022藍橋杯c組求和

題目名字 求和 題目鏈接 題意 輸入的每個數都要兩兩相乘&#xff0c;然后再加起來&#xff0c;求最后總和&#xff1b; 思路 每個數乘這個數的前綴和即可 算法一&#xff1a;前綴和 實現步驟 先把前綴和寫出來再寫for循環每個數都乘以自己的前綴和&#xff1b; 實現步驟 直接…

存儲成本降71%,怪獸充電歷史庫遷移OceanBase

怪獸充電作為共享充電寶第一股&#xff0c;業務增長迅速&#xff0c;以至于業務架構不停地增加組件。在驗證 OceanBase 可以簡化架構并帶來更大的業務價值后&#xff0c;首次嘗試在歷史庫中使用 OceanBase 替代 MySQL&#xff0c;存儲成本降低 71%。本文為怪獸充電運維架構部王…

Docker 入門

Docker 入門 基礎 不同操作系統下其安裝包、運行環境是都不相同的&#xff01;如果是手動安裝&#xff0c;必須手動解決安裝包不同、環境不同的、配置不同的問題 而使用Docker&#xff0c;這些完全不用考慮。就是因為Docker會自動搜索并下載MySQL。注意&#xff1a;這里下載…

【C++】輸入輸出流 ⑥ ( cout 標準輸出流對象 | cout 常用 api 簡介 | cout.put(char c) 函數 )

文章目錄 一、cout 標準輸出流對象1、cout 標準輸出流對象簡介2、cout 常用 api 簡介 二、cout.put(char c) 函數1、cout.put(char c) 函數 簡介2、代碼示例 - cout.put(char c) 函數 一、cout 標準輸出流對象 1、cout 標準輸出流對象簡介 cout 是 標準輸出流 對象 , 是 ostrea…

端口被占用 --- 解決方案

問題描述 加速服務啟動失敗&#xff0c;443端口被magentproc(1576)占用。請關掉占用443端口的程序或者嘗試使用系統代理模式。 問題解決 按下 win R 打開 輸入cmd 輸入命令 netstat -ano | findstr 443 找到 0.0.0.0:443 對應的端口 (1576) 按下 ctrl shift esc, 打開任務管…

綜述 2023-IEEE-TCBB:生物序列聚類方法比較

Wei, Ze-Gang, et al. "Comparison of methods for biological sequence clustering." IEEE/ACM Transactions on Computational Biology and Bioinformatics (2023). https://ieeexplore.ieee.org/document/10066180 被引次數&#xff1a;1&#xff1b;研究背景&am…

力扣題:數字與字符串間轉換-12.13

力扣題-12.13 [力扣刷題攻略] Re&#xff1a;從零開始的力扣刷題生活 力扣題1&#xff1a;442. 數組中重復的數據 解題思想&#xff1a;直接相除即可 class Solution(object):def optimalDivision(self, nums):""":type nums: List[int]:rtype: str"&qu…

Transformer 簡介

Transformer 是 Google 在 2017 年底發表的論文 Attention Is All You Need 中所提出的 seq2seq 模型。Transformer 模型的核心是 Self-Attention 機制&#xff0c;能夠處理輸入序列中的每個元素&#xff0c;并能計算其與序列中其他元素的交互關系的方法&#xff0c;從而能夠更…

再見了Future,圖解JDK21虛擬線程的結構化并發

Java為我們提供了許多啟動線程和管理線程的方法。在本文中&#xff0c;我們將介紹一些在Java中進行并發編程的選項。我們將介紹結構化并發的概念&#xff0c;然后討論Java 21中一組預覽類——它使將任務拆分為子任務、收集結果并對其進行操作變得非常容易&#xff0c;而且不會不…