目錄
一、多表關聯
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 中直接安裝:
-
打開 IDEA,進入
File
→Settings
→Plugins
。 -
搜索
MybatisX
,點擊Install
安裝。 -
重啟 IDEA 生效。
-
-
JetBrains 插件市場地址: MybatisX on JetBrains Marketplace
2. 手動下載(備用)
-
如果無法通過 IDEA 直接安裝,可以從 JetBrains 插件市場下載
.jar
文件:-
訪問上述鏈接,點擊
Download
獲取最新版本的.jar
。 -
在 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注解)