Mybatis的多表关联查询以及延迟加载(LazyLoad)

 Mybatis既然封装了JDBC,那么它一定能做到Mysql中的连接查询。

一、Mysql的连接查询

 简单回顾一下Mysql的连接查询:分为两种,一种是内连接,内连接又分为等值连接,非等值连接,自连接;另外一种是外

 连接,外连接分为左连接(left join)和右连接(right join),两种连接都是表达多个表之间的关系。二者在代码上依靠是否

 有left或者right来进行区分。

 等值连接基本语句:

 SQL92语法:(优点是语法简单)

Select a.ename, d.dname from emp e, dept d where e.deptno = d.deptno;

SQL99语法:(优点是on后面还可以接where关键字,进行多重判断,逻辑清晰)

Select e.ename, d.dname from emp e join dept d on e.deptno = d.deptno;

很可惜以上两种表达,在Mybatis不会用到,因为后续开发中,在高并发的条件下我们为了降低数据库的压力,会使用延迟加

载设置,而延迟加载是建立在单表查询之上的。

二、Mybatis中的多表关联查询:

 

1、工程截图:

2、bean层:

一共有两个类,一个country国家,一个minister部长,关系是:一个国家有多个部长。

country类成员变量:使用SET集合,表示部长无序不重复

private Integer cid;
private String cname;
//一个国家多个部长,设置一个集合
private Set<Minister> ministers;

minister类:

private String mname;
private Integer mid;

3 Mysql数据库:

提供Mysql代码:

drop table if EXISTS minister;
create table minister(
		mid INT(12) PRIMARY key auto_increment,
		mname VARCHAR(32),;
		countryId INT(12)
)
drop table if EXISTS country;
create table country(
	cid INT(12) PRIMARY KEY auto_increment,
	cname VARCHAR(32)
)

insert minister(mname,countryId) VALUES("aa",1);
insert minister(mname,countryId) VALUES("bb",1);
insert minister(mname,countryId) VALUES("cc",1);
insert minister(mname,countryId) VALUES("dd",3);
insert minister(mname,countryId) VALUES("ee",2);

insert country(cname) VALUES("USA");
insert country(cname) VALUES("England");
insert country(cname) VALUES("China");

4 Dao层

  ICountryDao.java:只有一个方法,注意方法名字要和mapper文件中的select的id相同(详情可见动态代理)

public interface ICountryDao {
   country selectCountryById(int cid);
}

  mapper.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.ICountryDao">
	<resultMap type="country" id="CountryMapper">
		<id column="cid" property="cid" />
		<result column="cname" property="cname" />
		<collection property="ministers"
					ofType="Minister"
					select="selectMinisterByCountry"
					column="cid"
					 />
	</resultMap>
	<select id="selectMinisterByCountry" resultType="Minister">
		select mid,mname from minister where countryId=#{xx}
	</select>

	<select id="selectCountryById" resultMap="CountryMapper">
		select cid,cname from country where cid=#{xxx}
	</select>
</mapper>

我们需要理解他的执行顺序逻辑:

首先:根据ICountry中的方法寻找到selectCountryById的标签,并执行他的sql语句,把返回来的数据封装为id为CountryMap的

resultMap标签。这里的sql语句只是对country进行了查询,获取表中cid和id参数相等的数据,和minister并没有关系。

然后:找到CountryMap标签,根据id和result进行封装。因为country中的minister是一个set集合,因此使用collection进行封装,

前面说了之前执行的sql语句并没有对minister表进行查询。

之后:chazha查找collection中的select对应id标签,进行判断的参数是collection中的column

三、Mybatis中延迟查询:(lazyload)

3.1 概述

在Mybatis中执行查询语句有两种加载情况

1 直接加载:直接执行select的sql语句

2 延迟加载:

首先需要清楚查询语句分为主查询和关联查询,在第二点中的代码中:<select id="selectCountryById"    resultMap="CountryMapper">就是主查询,主查询是无法延迟.但是关联查询可以进行设置

延迟查询分为:分为深度延迟和侵入式延迟.下面分别介绍

3.2 深度延迟

   关联查询不会直接进行,只有当你使用了管理查询对应的数据时,才会执行对应关联插叙的sql语句.

   代码:只需要在myBatis.xml文件中进行setting配置:

<settings>
<!--延迟加载总开关  -->
    <setting name="lazyLoadingEnabled" value="true"/>
</settings>

  测试1:我们在35\36行处添加断点.getcname是主查询的操作,getminister是关联查询的操作

  可以看见,程序执行完34行,在控制台日志中,只执行了一个sql语句,也就是主查询的sql语句,关联查询没有执行.

  测试2:

  

 可以看见,当我们执行到关联查询的操作getminister后,控制台出现了第二句sql语句,这就是延迟加载.

3. 3  侵入式延迟

侵入式就是关联查询的详情侵入到了主查询的详情中,他会在调用主查询相关操作的时候,执行关联查询语句.

代码;注意必须要先把开关打开,这是前提条件。

	<settings>
	    <!--延迟加载总开关  -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!--侵入式延迟加载  -->
		<setting name="aggressiveLazyLoading" value="true"/>
	</settings>

测试1: 

可以看到,它只进行了主查询语句.

测试二:

他在调用主查询相关操作的时候,才执行了关联查询语句.

 

                                                                                                                                                                  ---以上

下面的空白去不掉,逼死强迫症...................

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MyBatis进行多表关联查询时,可以通过使用注解的方式或者在application.yml文件中进行配置来实现。当使用注解的方式时,可以在对应的Mapper接口中使用@Results和@Result注解来定义查询结果映射关系。同时,还需要在对应的SQL语句中使用JOIN语句将多个表关联起来进行查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatis如何实现多表联查](https://blog.csdn.net/blanceage/article/details/125476614)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [MyBatis多表关联查询](https://blog.csdn.net/IceTiger_/article/details/119566992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Mybatis多表关联查询的实现(DEMO)](https://download.csdn.net/download/weixin_38514872/12785395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值