mybatis使用associaton进行分步查询

本文介绍了在MyBatis中如何通过association标签实现分步查询,以Employee和Department类为例,展示了如何查询员工及其关联的部门信息。同时,解释了如何在全局配置文件中开启懒加载,以避免不必要的查询。当懒加载启用后,只有在真正需要部门信息时才会执行相应的查询操作,提高了查询效率。
摘要由CSDN通过智能技术生成

Employee类

public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private Department dept;
    // 省略setter、getter、toString方法
}

Department类

public class Department {
	
	private Integer id;
	private String departmentName;
	private List<Employee> emps;
}

再来看EmployeeMapper.xml中的相关语句

	<!-- 使用association进行分步查询:
		1、先按照员工id查询员工信息
		2、根据查询员工信息中的d_id值去部门表查出部门信息
		3、部门设置到员工中;
	 -->
	 
	 <!--  id  last_name  email   gender    d_id   -->
	 <resultMap type="com.mybatis.bean.Employee" id="MyEmpByStep">
	 	<id column="id" property="id"/>
	 	<result column="last_name" property="lastName"/>
	 	<result column="email" property="email"/>
	 	<result column="gender" property="gender"/>
	 	<!-- association定义关联对象的封装规则
	 		select:表明当前属性是调用select指定的方法查出的结果
	 		column:指定将哪一列的值传给这个方法
	 		流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
	 	 -->
 		<association property="dept" 
	 		select="com.mybatis.dao.DepartmentMapper.getDeptById"
	 		column="d_id">
 		</association>
	 </resultMap>
	 <!--  public Employee getEmpByIdStep(Integer id);-->
	 <select id="getEmpByIdStep" resultMap="MyEmpByStep">
	 	select * from tbl_employee where id=#{id}
	 </select>

DepartmentMapper.xml中的相关语句

<!--public Department getDeptById(Integer id);  -->
	<select id="getDeptById" resultType="com.mybatis.bean.Department">
		select id,dept_name departmentName from tbl_dept where id=#{id}
	</select>

通过association实现了分步查询,在一定程度上简化了sql语句,另外association还指支持延迟加载(懒加载),目前的情况是当我们执行了getEmpByIdStep语句,也一定执行DepartmentMapper.xml中的getDeptById语句,但如果并不需要部门表中的信息呢?

			Employee employee = mapper.getEmpByIdStep(3);
			System.out.println(employee);

查询id为3的员工的信息,此时我们并不需要部门表的信息,那可以用懒加载的方式进行。

需要在mybatis全局配置文件mybatis-config.xml中开启

	<settings>
		<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>

对于这两个属性,我们来看一下mybatis官方文档中的介绍
在这里插入图片描述

当这样设置后,当我们再次运行

	Employee employee = mapper.getEmpByIdStep(3);
			System.out.println(employee);

通过控制台的打印sql语句可以发现,并未执行查询部门的sql语句

Employee employee = mapper.getEmpByIdStep(3);
System.out.println(employee.getDept());

当这样调用时,就会调用查询部门的语句,实现了按需加载。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建 Employee 实体类和 Department 实体类,分别对应员工表和部门表的结构。 Employee.java ``` public class Employee { private int id; private String name; private int age; private int departmentId; // getter/setter 省略 } ``` Department.java ``` public class Department { private int id; private String name; // getter/setter 省略 } ``` 2. 创建 EmployeeMapper 接口和 DepartmentMapper 接口,用于定义查询方法。 EmployeeMapper.java ``` public interface EmployeeMapper { // 根据员工 id 查询员工信息 Employee selectEmployeeById(int id); // 根据部门 id 查询部门信息 Department selectDepartmentById(int id); } ``` DepartmentMapper.java ``` public interface DepartmentMapper { // 根据部门 id 查询部门信息 Department selectDepartmentById(int id); } ``` 3. 创建 EmployeeMapper.xml 和 DepartmentMapper.xml 文件,用于实现查询方法。 EmployeeMapper.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.example.mapper.EmployeeMapper"> <select id="selectEmployeeById" parameterType="int" resultType="com.example.entity.Employee"> SELECT * FROM employee WHERE id = #{id} </select> <select id="selectDepartmentById" parameterType="int" resultType="com.example.entity.Department"> SELECT * FROM department WHERE id = #{id} </select> </mapper> ``` DepartmentMapper.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.example.mapper.DepartmentMapper"> <select id="selectDepartmentById" parameterType="int" resultType="com.example.entity.Department"> SELECT * FROM department WHERE id = #{id} </select> </mapper> ``` 4. 在配置文件中配置 mapper。 ``` <configuration> <mappers> <mapper resource="com/example/mapper/EmployeeMapper.xml"/> <mapper resource="com/example/mapper/DepartmentMapper.xml"/> </mappers> </configuration> ``` 5. 在业务代码中使用分步查询。 ``` @Autowired private EmployeeMapper employeeMapper; @Autowired private DepartmentMapper departmentMapper; public Employee getEmployeeById(int id) { Employee employee = employeeMapper.selectEmployeeById(id); Department department = departmentMapper.selectDepartmentById(employee.getDepartmentId()); employee.setDepartment(department); return employee; } ``` 以上是使用 MyBatis 实现分步查询的步骤和代码示例,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值