mybatis【关联关系,延迟加载】

本文详细探讨了MyBatis中的关联关系,包括1对1和一对多的映射配置、对象关系以及测试案例。同时,还介绍了延迟加载的概念,如何开启全局延迟加载,并展示了1对1和一对多延迟加载的映射文件和测试实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关联关系

在关联型数据库中,表与表之间很少是独立与其他表妹关系的。所以在实际开发过程中我们会碰到很多复杂的关联关系。

1 对 1 关系

我们有一张员工表(t_emp),一张部门表(t_dept)。员工表中的一条记录对应于部门表中有且仅有一条记录。这就是一对一的关联关系
在这里插入图片描述

映射文件

<?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="com.sxt.dao.EmpDao">
	
	<resultMap type="Emp" id="baseResultMap">
		<!-- 主表里面的信息 -->
		<id column="empId" property="empId"/>
		<result column="empName" property="empName"/>
		<result column="sex" property="sex"/>
		<!-- 配置11的关联关系 property 对应的就是Emp中部门成员变量的名称-->
		<association property="dept" javaType="Dept">
			<id column="deptId" property="deptId"/>
			<result column="deptName" property="deptName"/>
			<result column="deptDesc" property="deptDesc"/>
		</association>
	</resultMap>
	
	<select id="query" resultMap="baseResultMap">
		select 
			t1.empId
			,t1.empName
			,t1.sex
			,t2.deptId
			,t2.deptName
			,t2.deptDesc
		from t_emp t1 
			LEFT JOIN
				t_dept t2
					ON t1.deptId = t2.deptId
	</select>
	
</mapper>

员工类

package com.sxt.pojo;

public class Emp {

	private Integer empId;
	private String empName;
	private String sex;
	//private Integer deptId;
	
	//员工和部门的关联关系是1VS1
	private Dept dept;

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	@Override
	public String toString() {
		return "Emp [empId=" + empId + ", empName=" + empName + ", sex=" + sex + ", dept=" + dept + "]";
	}	
	
}

部门类

package com.sxt.pojo;

public class Dept {

	private Integer deptId;
	private String deptName;
	private String deptDesc;
	
	//部门和员工的关系是 1 VS N 一对多
	public Integer getDeptId() {
		return deptId;
	}
	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getDeptDesc() {
		return deptDesc;
	}
	public void setDeptDesc(String deptDesc) {
		this.deptDesc = deptDesc;
	}
	@Override
	public String toString() {
		return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptDesc=" + deptDesc + "]";
	}
	
}

接口

package com.sxt.dao;

import java.util.List;

import com.sxt.pojo.Emp;

public interface EmpDao {
	public List<Emp> query();
}

测试

package com.sxt.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sxt.dao.EmpDao;
import com.sxt.pojo.Emp;

public class TestDemo {

	private SqlSession session;
	@Before
	public void before() throws IOException{
		InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		session = factory.openSession();
	}
	@Test
	public void test1() throws IOException {
		EmpDao dao = session.getMapper(EmpDao.class);
		List<Emp> list = dao.query();
		for (Emp emp : list) {
			System.out.println(emp);
		}
		
	}
	
	@After
	public void after(){
		session.commit();
		session.close();
	}
}

在这里插入图片描述

一对多

在这里插入图片描述
查询出所有的部门信息及该部门下所有员工的信息

映射文件

<?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="com.sxt.dao.DeptDao">
	
	<resultMap type="Dept" id="baseResultMap">
		<!-- 主表里面的信息 -->
		<id column="deptId" property="deptId"/>
		<result column="deptName" property="deptName"/>
		<result column="deptDesc" property="deptDesc"/>
		
		<!-- 配置1对多的关联关系 ofType就是集合中泛型的类型-->
		<collection property="emps" ofType="Emp">
			<id column="empId" property="empId"/>
			<result column="empName" property="empName"/>
			<result column="sex" property="sex"/>
		</collection>
		
	</resultMap>
	
	<select id="query" resultMap="baseResultMap">
		select 
			 t1.deptId
			,t1.deptName
			,t1.deptDesc
			,t2.empId
			,t2.empName
			,t2.sex
		from t_dept t1 
			LEFT JOIN
				t_emp t2
					ON t1.deptId = t2.deptId
	</select>
	
</mapper>

对象与对象一对多

Emp类

package com.sxt.pojo;

public class Emp {

	private Integer empId;
	private String empName;
	private String sex;
	//private Integer deptId;
	

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Emp [empId=" + empId + ", empName=" + empName + ", sex=" + sex + "]";
	}
	
}

Dept类

package com.sxt.pojo;

import java.util.List;

public class Dept {

	private Integer deptId;
	private String deptName;
	private String deptDesc;
	
	//部门和员工的关系是 1 VS N 一对多
	private List<Emp> emps;
	
	public Integer getDeptId() {
		return deptId;
	}
	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getDeptDesc() {
		return deptDesc;
	}
	public void setDeptDesc(String deptDesc) {
		this.deptDesc = deptDesc;
	}
	public List<Emp> getEmps() {
		return emps;
	}
	public void setEmps(List<Emp> emps) {
		this.emps = emps;
	}
	@Override
	public String toString() {
		return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptDesc=" + deptDesc + ", emps=" + emps + "]";
	}
	
}

接口

package com.sxt.dao;

import java.util.List;
import com.sxt.pojo.Dept;

public interface DeptDao {
	public List<Dept> query();
}

测试

package com.sxt.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sxt.dao.DeptDao;
import com.sxt.pojo.Dept;

public class TestDemo {

	private SqlSession session;
	@Before
	public void before() throws IOException{
		InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		session = factory.openSession();
	}
	@Test
	public void test1() throws IOException {
		DeptDao dao = session.getMapper(DeptDao.class);
		List<Dept> list = dao.query();
		for (Dept dept : list) {
			System.out.println(dept);
			System.out.println(dept.getEmps());
		}
		
	}
	
	@After
	public void after(){
		session.commit();
		session.close();
	}
}

一个部门多个员工
在这里插入图片描述

延迟加载

   延迟查询的一对一和一对多查询的延续

   在默认的一对一和一对多中,一条SQL就能查询到所有数据,但是,有的数据有时候一时半会用不上,例如查询员工,捎带获取员工的部门数据,但是部门数据使用的频率很低,这种时候就可以使用延迟查询,首先获取到所有的员工数据,然后在需要的时候再去获取部门数据。当需要使用数据的时候才去加载即是延迟加载

开启延迟加载

全局配置文件中配置
在这里插入图片描述

一对一的延迟加载

<settings>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>

在这里插入图片描述

映射文件

EmpDao.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="com.sxt.dao.EmpDao">
	
	<resultMap type="Emp" id="baseResultMap">
		<!-- 主表里面的信息 -->
		<id column="empId" property="empId"/>
		<result column="empName" property="empName"/>
		<result column="sex" property="sex"/>
		<!-- 配置11的关联关系 property 对应的就是Emp中部门成员变量的名称-->
		<association property="dept" javaType="Dept"
			column="deptId" select="queryDeptById">
			<!-- 把deptId作为条件查询 queryDeptById-->
			<id column="deptId" property="deptId"/>
			<result column="deptName" property="deptName"/>
			<result column="deptDesc" property="deptDesc"/>
		</association>
	</resultMap>
	
	<select id="queryDeptById" parameterType="int" resultType="Dept">
		select * from t_dept where deptId = #{deptId}
	</select>
	
	<select id="query" resultMap="baseResultMap">
		select 
			t1.empId
			,t1.empName
			,t1.sex
			,t1.deptId
		from t_emp t1 
	</select>
	
</mapper>

测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

一对多

配置文件

开启延迟加载
在这里插入图片描述

映射文件

<?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="com.sxt.dao.DeptDao">
	
	<resultMap type="Dept" id="baseResultMap">
		<!-- 主表里面的信息 -->
		<id column="deptId" property="deptId"/>
		<result column="deptName" property="deptName"/>
		<result column="deptDesc" property="deptDesc"/>
		
		<!-- 配置1对多的关联关系 ofType就是集合中泛型的类型-->
		<collection property="emps" ofType="Emp" column="deptId" select="queryEmpByDeptId">
			<id column="empId" property="empId"/>
			<result column="empName" property="empName"/>
			<result column="sex" property="sex"/>
		</collection>
		
	</resultMap>
	
	<select id="queryEmpByDeptId" parameterType="int" resultType="Emp">
		select * from t_emp where deptId = #{deptId}
	</select>
	
	<select id="query" resultMap="baseResultMap">
		select 
			 t1.deptId
			,t1.deptName
			,t1.deptDesc
		from t_dept t1 
	</select>
	
</mapper>

要把tostring方法里的员工信息去掉
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值