MyBatis --- 映射关系一对一对多多对多,懒加载机制
Posted Lverson_tester
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis --- 映射关系一对一对多多对多,懒加载机制相关的知识,希望对你有一定的参考价值。
映射(多、一)对一的关联关系
1)若只想得到关联对象的id属性,不用关联数据表
2)若希望得到关联对象的其他属性,要关联其数据表
举例:
员工与部门的映射关系为:多对一
1.创建表
员工表
确定其外键是部门表的 id
DROP TABLE IF EXISTS emp; CREATE TABLE emp( id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, emp_name VARCHAR(255) DEFAULT NULL, gender CHAR(1) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, dept_id INT(22) DEFAULT NULL, FOREIGN KEY (dept_id) REFERENCES dept(id) )
部门表
CREATE TABLE dept ( id INT(11) PRIMARY KEY AUTO_INCREMENT, dept_name VARCHAR(255) )
2.创建相应的实体类和Mapper接口
查询的方法有三!
方法一:
- 写关联的 sql 语句
将两个表通过部门表的id关联,查找员工表id为1的员工信息
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name FROM emp e,dept d WHERE e.dept_id = d.id AND e.id = 1
- 在sql映射文件中写映射sql语句【联合查询:级联属性封装结果集】
使用resultMap映射,对于【对一】关系可以不用 association
<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap"> <!-- Employee --> <id column="id" property="id"/> <result column="emp_name" property="empName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <!-- Department --> <result column="id" property="dept.id"/> <result column="dept_name" property="dept.deptName"/> </resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap"> SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name FROM emp e,dept d WHERE e.dept_id = d.id AND e.id = #{id} </select>
- 编写测试用例
private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml"); @Test public void test() { TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class); TblEmployee employee = bean.getEmployeeById(1); System.out.println(employee); }
方法二:使用association来定义关联对象的规则,【比较正规的,推荐的方式】
<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap"> <!-- Employee --> <id column="id" property="id"/> <result column="emp_name" property="empName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <!-- association可以指定联合的javaBean对象 property="dept":指定哪个属性是联合的对象 javaType:指定这个属性对象的类型【不能省略】 --> <association property="dept" javaType="com.neuedu.mybatis.entity.TblDepartment"> <id column="id" property="id"/> <result column="dept_name" property="deptName"/> </association> </resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap"> SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name FROM emp e,dept d WHERE e.dept_id = d.id AND e.id = #{id} </select>
方法三:上述结果相当于使用嵌套结果集的形式,还可以使用 association 进行分步查询:
使用association进行分步查询:
1.先按照员工id查询员工信息
2.根据查询员工信息中d_id值取部门表查出部门信息
3.部门设置到员工中:
<!-- 通过id与resultMap相连 --> <select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment"> select d.id,d.dept_name from dept d where d.id = #{id} </select> <!-- 通过id被查找employee的select找到 --> <resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap"> <id column="id" property="id"/> <result column="emp_name" property="empName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <!-- property:是employee中的属性, select:dept的select语句的id column:查找到的部门的id,传到dept的select中 --> <association property="dept" select="selectDept" column="dept_id"/> </resultMap> <select id="getEmployeeById" resultMap="getEmployeeByIdMap"> SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id from emp e where e.id = #{id} </select>
<mapper namespace="com.neuedu.mybatis.mapper.TblDepartmentMapper"> <select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment"> select d.id,d.dept_name from dept d where d.id = #{id} </select> </mapper>
之前的 association 中的select是写 id,现在写 DepartmentMapper 的全类名+ id
<resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
<id column="id" property="id"/>
<result column="emp_name" property="empName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- property:是employee中的属性,
select:select语句的id
column:查找到的部门的id,传到查找dept的select中
-->
<association property="dept" select="com.neuedu.mybatis.mapper.TblDepartmentMapper.selectDept" column="dept_id"/>
</resultMap>
<select id="getEmployeeById" resultMap="getEmployeeByIdMap">
SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
from emp e
where e.id = #{id}
</select>
懒加载机制【按需加载】:
我们只需要在分步查询的基础之上,在mybatis的全局配置文件中加入两个属性:
<!-- 开启懒加载机制 ,默认值为false--> <setting name="lazyLoadingEnabled" value="true"/> <!-- 开启的话,每个属性都会直接全部加载出来;禁用的话,只会按需加载出来 --> <setting name="aggressiveLazyLoading" value="false"/>
private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml"); @Test public void test() { TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class); TblEmployee employee = bean.getEmployeeById(1); System.out.println(employee.getEmpName()); }
private List<TblEmployee> list; public List<TblEmployee> getList() { return list; } public void setList(List<TblEmployee> list) { this.list = list; }
建立DepartmentMapper接口文件,并添加如下方法:
public TblDepartment selectDeptAndEmp(Integer id);
2.sql映射文件中的内容为:【collection:嵌套结果集的方式:使用collection标签定义关联的集合类型元素的封装规则】
<resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap"> <id column="id" property="id"/> <result column="dept_name" property="deptName"/> <!-- property:Department中的属性名,ofType集合中存放的类型 --> <collection property="list" ofType="com.neuedu.mybatis.entity.TblEmployee"> <id column="eid" property="id"/> <result column="emp_name" property="empName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> </collection> </resultMap> <select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap"> select d.id,d.dept_name,e.id eid,e.emp_name,e.gender,e.email,e.dept_id from dept d LEFT JOIN emp e ON d.id = e.dept_id WHERE d.id = #{id} </select>
3.测试方法为:
@Test public void TestselectDeptAndEmp(){ TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class); TblDepartment dept = bean.selectDeptAndEmp(1); List<TblEmployee> list = dept.getList(); for (TblEmployee tblEmployee : list) { System.out.println(tblEmployee); } }
第二种:使用分步查询结果集
1.在DepartmentMapper接口文件中添加方法,如下所示:
public TblDepartment selectDeptAndEmp(Integer id);
2.再从EmployeeMapper接口中添加一个方法,如下所示:
public List<TblEmployee> getEmps(Integer deptId);
3.并在响应的sql映射文件中添加相应的sql语句
<select id="getEmps" resultType="com.neuedu.mybatis.entity.TblEmployee"> SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id FROM emp e WHERE e.dept_id = #{id} </select>
4.在DepartmentMapper映射文件中:
<resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap"> <id column="id" property="id"/> <result column="dept_name" property="deptName"/> <!-- property:Department中的属性名,ofType集合中存放的类型 --> <collection property="list" select="com.neuedu.mybatis.mapper.TblEmployeeMapper.getEmps" column="id"/> </resultMap> <select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap"> SELECT d.id,d.dept_name FROM dept d WHERE d.id = #{id} </select>
5.测试类:
@Test public void TestselectDeptAndEmp(){ TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class); TblDepartment dept = bean.selectDeptAndEmp(1); List<TblEmployee> list = dept.getList(); for (TblEmployee tblEmployee : list) { System.out.println(tblEmployee); } }
以上是关于MyBatis --- 映射关系一对一对多多对多,懒加载机制的主要内容,如果未能解决你的问题,请参考以下文章