MyBatis04:SpringBoot整合MyBatis——多表關聯|延遲加載|MyBatisX插件|SQL注解

目錄

一、多表關聯

1、數據庫表結構

2、javaBean類

3、mapper接口

4、sqlMapper文件

5、測試

二、延遲加載

1、解決什么問題

2、嵌套查詢

3、延遲加載

三、逆向工程MybatisX插件

1、下載插件

1. 通過 JetBrains 插件市場下載(推薦)

2. 手動下載(備用)

四、SQL注解(這個方法是MybatisX的方法,不使用sqlmapper文件映射,直接通過注解在java文件里面寫sql語句,沒有實現sql語句與java分離的初衷)


一、多表關聯

1、數據庫表結構

create table king(id int primary key auto_increment,name char(32)
);
?
create table queen(id int primary key auto_increment,name char(32),k_id int 
);
?
create table consort(id int primary key auto_increment,name char(32),k_id int 
);
?
insert into king(name) values ('拉瑪十世'),('乾隆');
insert into queen(name,k_id) values ('蘇提達',1),('富察氏',2);
insert into consort(name,k_id) values ('詩妮娜1號',1),('詩妮娜2號',1),('令妃',2),('香妃',2);select * from queen;        
select * from consort;      

2、javaBean類

@Data
public class King {private Integer id;private String name;//一對一映射private Queen queen;//王后對象//一對多映射private List<Consort> list;//妃子集合
}

3、mapper接口

@Mapper
public interface KingMapper {public List<King> getKings();
}

4、sqlMapper文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hl.mybatis03.mapper.KingMapper"><!--     public List<King> getKings();--><select id="getKings" resultMap="map1">select king.*,queen.id as qid,queen.name as qname,queen.k_id,c.id as cid,c.name as cname,c.k_id as ck_idfrom kingjoin queen on king.id = queen.k_idjoin consort c on king.id = c.k_id</select><!--type="預期的最終返回值類型"  autoMapping="true"開啟結果集自動映射--><resultMap id="map1" type="King" autoMapping="true"><id property="id" column="id"></id><!--手動結果集映射-->
<!--        <result property="name" column="name"></result>--><!--一對一映射--><association property="queen" javaType="Queen"><!--給Queen類的屬性賦值--><id property="id" column="qid"></id><result property="name" column="qname"></result><result property="kId" column="k_id"></result></association><!--一對多映射 autoMapping="true"自動映射  columnPrefix="c" 針對列名添加前綴--><collection property="list" ofType="Consort" autoMapping="true" columnPrefix="c"><id property="id" column="id"></id>
<!--            <id property="id" column="cid"></id>-->
<!--            <result property="name" column="cname"></result>-->
<!--            <result property="kId" column="ck_id"></result>--></collection></resultMap>
</mapper>

5、測試

二、延遲加載

多張表相關聯情況下

1、解決什么問題

延遲加載主要用于解決嵌套查詢的效率問題。

只針對嵌套查詢。

2、嵌套查詢

一個查詢調用另一個查詢。通過王后查國王,再通過國王查妃子

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hl.mybatis03.mapper.QueenMapper"><!--public List<Queen> getQueens();--><!--查詢王后信息--><select id="getQueens" resultMap="map1">select * from queen</select><!--  fetchType="eager" 數據的抓取策略:立即加載 延遲加載--><resultMap id="map1" type="Queen" autoMapping="true"><id property="id" column="id"></id><association property="king" javaType="King"fetchType="lazy"select="findKing" column="k_id"></association><collection property="list" ofType="Consort"fetchType="lazy"select="selectConsort" column="k_id"></collection></resultMap><!--根據王后表中的國王id,找到國王信息--><select id="findKing" resultType="King">select * from king where id = #{id}</select><!--根據國王id,找到妃子集合--><select id="selectConsort" resultType="Consort">select * from consort where k_id = #{kId}</select>
</mapper>

3、延遲加載

當我們不需要另一個查詢時,該查詢先不執行。

當我們需要另一個查詢的數據時,再執行該查詢。

需要??:當我們訪問這個關聯屬性時,進行查詢;不訪問關聯屬性時,不執行查詢。

select="sql語句唯一標識" 嵌套查詢

fetchType="eager" 立即加載

fetchType="lazy" 延遲加載,懶(延遲)加載要求所對應的類(javabean)以及相關類實現序列化接口Serializable?

@Data
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
public class Queen implements Serializable {private Integer id;private String name;private Integer kId;//國王id
?//一對一private King king;//間接  一對多private List<Consort> list;
}
@Mapper
public interface QueenMapper {public List<Queen> getQueens();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hl.mybatis03.mapper.QueenMapper"><!--public List<Queen> getQueens();--><!--查詢王后信息--><select id="getQueens" resultMap="map1">select * from queen</select><!--  fetchType="eager" 數據的抓取策略:eager立即加載 lazy延遲加載 : 使用數據時查詢(比如return返回時json序列化,或者debug模式看數據內容)--><resultMap id="map1" type="Queen" autoMapping="true"><id property="id" column="id"></id><association property="king" javaType="King"fetchType="lazy"select="findKing" column="k_id"></association><collection property="list" ofType="Consort"fetchType="lazy"select="selectConsort" column="k_id"></collection></resultMap><!--根據王后表中的國王id,找到國王信息--><select id="findKing" resultType="King">select * from king where id = #{id}</select>
?<!--根據國王id,找到妃子集合--><select id="selectConsort" resultType="Consort">select * from consort where k_id = #{kId}</select>
</mapper>

三、逆向工程MybatisX插件

1、下載插件

下載和安裝 MybatisX 的地址和方法:

1. 通過 JetBrains 插件市場下載(推薦)

  • 在 IntelliJ IDEA 中直接安裝

    1. 打開 IDEA,進入 FileSettingsPlugins

    2. 搜索 MybatisX,點擊 Install 安裝。

    3. 重啟 IDEA 生效。

  • JetBrains 插件市場地址: MybatisX on JetBrains Marketplace

2. 手動下載(備用)

  • 如果無法通過 IDEA 直接安裝,可以從 JetBrains 插件市場下載 .jar 文件:

    1. 訪問上述鏈接,點擊 Download 獲取最新版本的 .jar

    2. 在 IDEA 的 Plugins 界面選擇 Install Plugin from Disk,上傳下載的 .jar 文件。

四、SQL注解(這個方法是MybatisX的方法,不使用sqlmapper文件映射,直接通過注解在java文件里面寫sql語句,沒有實現sql語句與java分離的初衷)

@Insert
@Options
?
@Update
@Delete
@Select
?
@Results
@Result
@One
@Many

package com.hl.mybatis03.mapper;import com.hl.mybatis03.pojo.Consort;
import com.hl.mybatis03.pojo.King;
import org.apache.ibatis.annotations.*;import java.util.List;
@Mapper
public interface ConsortMapper {@Select("select * from consort")public List<Consort> listAll();@Insert("insert into consort(name,k_id) values (#{name},#{kId})")//返回自增主鍵@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")public int save(Consort consort);@Update("update consort set name=#{name},k_id=#{kId} where id=#{id}")public int update(@Param("id") Integer id, @Param("name") String name, @Param("kId") Integer kId);@Delete("delete from consort where id=#{id}")public int delete(Integer id);//關聯妃子和王國@Select("select * from consort")@Results(value = {@Result(id = true,property = "id",column = "id"),@Result(property = "name",column = "name"),@Result(property = "king",javaType = King.class, column = "k_id",one=@One(select = "com.hl.mybatis03.mapper.ConsortMapper.getKingById"))})public List<Consort> list();@Select("select * from king where id=#{id}")public King getKingById(Integer id);}

作業

創建 用戶類、角色類

用戶表(id,username,phone,role_id)

zhangsan

lisi

角色表(id,name)

超級管理員

普通管理員

財務人員

市場人員

1)通過用戶,查詢當前用戶信息和相關的角色信息

一對一

方式一:兩張表join連接查詢,相關數據(xml)

方式二:嵌套查詢(xml、sql注解)

方式三:嵌套查詢(sql注解)

2)通過角色,查詢角色名和想的所有用戶列表

一對多

方式一:兩張表join連接查詢,相關數據(xml)

方式二:嵌套查詢(xml、sql注解)

方式三:嵌套查詢(sql注解)

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

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

相關文章

PySpark、Plotly全球重大地震數據挖掘交互式分析及動態可視化研究

全文鏈接&#xff1a;https://tecdat.cn/?p42455 分析師&#xff1a;Yapeng Zhao 在數字化防災減災的時代背景下&#xff0c;地震數據的深度解析成為公共安全領域的關鍵議題。作為數據科學工作者&#xff0c;我們始終致力于通過技術整合提升災害數據的應用價值&#xff08;點擊…

【Veristand】Veristand環境安裝教程-Linux RT / Windows

首先聲明&#xff0c;此教程是針對Simulink編譯模型并導入Veristand中編寫的&#xff0c;同時需要注意的是老用戶編譯可能用的是Veristand Model Framework&#xff0c;那個是歷史版本&#xff0c;且NI不會再維護&#xff0c;新版本編譯支持為VeriStand Model Generation Suppo…

MVC與MVP設計模式對比詳解

MVC&#xff08;Model-View-Controller&#xff09;和MVP&#xff08;Model-View-Presenter&#xff09;是兩種廣泛使用的分層架構模式&#xff0c;核心目標是解耦業務邏輯、數據和界面&#xff0c;提升代碼可維護性和可測試性。以下是它們的對比詳解&#xff1a; MVC 模式&…

Node.js 項目調試指南

Node.js 項目調試指南 &#x1f9ed; 一、調試工具和方式總覽 方式難度場景說明console.log 調試★簡單問題定位最常見&#xff0c;但效率低debug 模塊★★模塊化輸出日志支持命名空間的調試日志VSCode 斷點調試★★★跟蹤函數調用、變量狀態推薦使用node inspect / ndb★★★…

Spring Boot 啟動流程及配置類解析原理

Spring Boot 是一個基于 Spring 框架的開源框架&#xff0c;旨在簡化 Spring 應用的配置和部署。通過提供約定優于配置的原則&#xff0c;Spring Boot 大大降低了 Java 企業級應用的開發復雜度。本文將詳細介紹 Spring Boot 的啟動流程及其配置類的解析原理&#xff0c;幫助開發…

Flask 核心概念速覽:路由、請求、響應與藍圖

一、路由參數與請求方法 Flask 路由允許定義多種參數類型,并通過 methods 屬性限制請求方法。 1. 路由參數類型: 除了默認的 string,Flask 還支持: int: 匹配整數,自動轉換為 Python int 類型。非數字輸入會返回 404。 float: 匹配浮點數,自動轉換為 Python float 類型…

Beckhoff(倍福)PLC 順控程序轉換條件解讀

一、請求機器人上件步 二、程序代碼解釋:1、程序代碼1&#xff1a; 1、程序代碼&#xff1a; fbVar_TonDelay2(IN : (bInPartPresentLeft AND bInPartPresentRight), PT : T#100MS);(* Go to the next step *) stVar_SEQ.bTOK : stVar_SEQ.bRELEASE AND(stGV_SYS_ELEMENTS.ar…

KITTI數據集(計算機視覺和自動駕駛領域)

KITTI&#xff08;Karlsruhe Institute of Technology and Toyota Technological Institute at Chicago&#xff09;數據集是計算機視覺和自動駕駛領域中最廣泛使用的基準數據集之一。它由德國卡爾斯魯厄理工學院和美國芝加哥豐田技術研究所聯合發布&#xff0c;旨在推動自動駕…

echarts在uniapp中使用安卓真機運行時無法顯示的問題

文章目錄 一、實現效果二、話不多說&#xff0c;上源碼 前言&#xff1a;我們在uniapp中開發的時候&#xff0c;開發的時候很正常&#xff0c;echarts的圖形在h5頁面上也是很正常的&#xff0c;但是當我們打包成安卓app或者使用安卓真機運行的時候&#xff0c;圖形根本就沒有渲…

C#使用MindFusion.Diagramming框架繪制流程圖(1):基礎類型

MindFusion.Diagramming框架 在C#中使用MindFusion.Diagramming.dll庫來創建圖表和圖形界面&#xff0c;可以讓你構建出豐富和交互式的圖形應用。MindFusion.Diagramming 是一個強大的庫&#xff0c;支持創建各種類型的圖表&#xff0c;例如流程圖、網絡圖、組織結構圖等。 M…

LangChain【6】之輸出解析器:結構化LLM響應的關鍵工具

文章目錄 一 LangChain輸出解析器概述1.1 什么是輸出解析器&#xff1f;1.2 主要功能與工作原理1.3 常用解析器類型 二 主要輸出解析器類型2.1 Pydantic/Json輸出解析器2.2 結構化輸出解析器2.3 列表解析器2.4 日期解析器2.5 Json輸出解析器2.6 xml輸出解析器 三 高級使用技巧3…

Spring Boot項目中JSON解析庫的深度解析與應用實踐

在現代Web開發中&#xff0c;JSON&#xff08;JavaScript Object Notation&#xff09;作為輕量級的數據交換格式&#xff0c;已成為前后端通信的核心橋梁。Spring Boot作為Java生態中最流行的微服務框架&#xff0c;提供了對多種JSON庫的無縫集成支持。本文將深入探討Spring B…

OPenCV CUDA模塊光流------高效地執行光流估計的類BroxOpticalFlow

操作系統&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 編程語言&#xff1a;C11 算法描述 cv::cuda::BroxOpticalFlow 是 OpenCV CUDA 模塊中實現Brox光流算法的類。該類用于在 GPU 上高效地計算兩幀圖像之間的稠密光流&#xff08;Dens…

視覺分析在人員行為屬性檢測中的應用

基于視覺分析的人員行為屬性檢測方案 一、背景與需求分析 在工業生產、建筑施工、公共安全等領域&#xff0c;人員行為屬性的合規性檢測是保障安全生產的關鍵環節。例如&#xff0c;工地工人未佩戴安全帽、廚房人員未佩戴手套、作業現場人員使用手機等行為&#xff0c;均可能…

Linux--進程的程序替換

問題導入&#xff1a; 前面我們知道了&#xff0c;fork之后&#xff0c;子進程會繼承父進程的代碼和“數據”&#xff08;寫實拷貝&#xff09;。 那么如果我們需要子進程完全去完成一個自己的程序怎么辦呢&#xff1f; 進程的程序替換來完成這個功能&#xff01; 1.替換原理…

場景題-3

如何實現一個消息隊列 拆解分析主流的幾種消息隊列 1、基本架構 生產者Producer、消費者Consumer、Broker&#xff1a;生產者發送消息&#xff0c;消費者接受消息&#xff0c;Broker是服務端&#xff0c;處理消息的存儲、備份、刪除和消費關系的維護。 主題和分區&#xff…

vue2 項目中 npm run dev 運行98% after emitting CopyPlugin 卡死

今天在運行項目時&#xff0c;發現如下問題&#xff1a; 開始以為是node_modules依賴的問題&#xff0c;于是重新 npm install&#xff0c;重啟項目后還是未解決。 在網上找了一圈發現有人說是 require引入圖片地址沒有寫。在我的項目中排查沒有這個問題&#xff0c;最后發現某…

73 LV的使用(XFS文件系統)

四 LV的使用 先創建一個LV01 lvcreate -L 10G lv01 datavg Logical volume "lv01" created. 將創建出來的LV01進行格式化 mkfs.xxx LV的名稱(絕對路徑) 絕對路徑的組成:/dev/你創建VG的名字/LV的名字 mkfs.xfs /dev/datavg/lv01 掛載你的LV…

mybatis中判斷等于字符串的條件怎么寫

mybatis中判斷等于字符串的條件怎么寫_mybatis 等于字符串-CSDN博客 因為mybatis映射文件&#xff0c;是使用的ognl表達式&#xff0c;ognl是java語言&#xff08;強類型語言&#xff09;&#xff0c;會把‘X’解析成字符&#xff0c;而不是字符串。 所以比較字符串相等使用是…

C語言實現絕對值差值函數

在C語言中&#xff0c;可以編寫一個函數來計算兩個數的差值的絕對值。以下是一個簡單的實現&#xff1a; #include <stdio.h> #include <stdlib.h> // 用于abs函數&#xff08;如果需要&#xff09; // 方法1&#xff1a;使用標準庫函數 int absoluteDifference1…