【MyBatis框架】关联映射
关系映射
1. 关联映射概述
在关系型数据库中,多表之间存在着三种关联关系,分别为一对一,一对多和多对多,如图
- 一对一的关系:就是在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a。
- 一对多的关系:就是一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a。
- 多对多的关系:在A类中定义B类类型的集合,在B类中定义A类类型的集合。
2. 环境搭建
创建t_emp表
t_dept表
实体类Dept
package com.atguigu.mybatis.pojo;
import java.util.List;
public class Dept {
private Integer deptId;
private String deptName;
private List<Emp> emps;
public Dept() {
}
public Dept(Integer deptId, String deptName) {
this.deptId = deptId;
this.deptName = deptName;
}
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 List<Emp> getEmps() {
return emps;
}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Dept{" +
"deptId=" + deptId +
", deptName='" + deptName + ''' +
", emps=" + emps +
'}';
}
}
实体类Emp
package com.atguigu.mybatis.pojo;
public class Emp {
private Integer empId;
private String empName;
private Integer age;
private String gender;
private Dept dept;
public Emp() {
}
public Emp(Integer empId, String empName, Integer age, String gender) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.gender = gender;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Emp{" +
"empId=" + empId +
", empName='" + empName + ''' +
", age=" + age +
", gender='" + gender + ''' +
", dept=" + dept +
'}';
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
3.处理字段名和属性名不一致的情况
SQL语句
接口:
public interface EmpMapper {
Emp getEmpById(@Param("empId") Integer empId);
}
测试方法:
public void test(){
SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
SqlSession sqlSession = sqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp empById = mapper.getEmpById(1);
System.out.println(empById.toString());
}
执行测试方法后会得到如下结果:
可以看到,我们的SQl语句并没有问题,但为什么查询出的结果会有NUll出现呢,这就是因为我们的数据库中的字段名于Emp
实体类的属性名不一致,因此出现了无法对应的情况。
解决办法:
1.可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};
再次执行尝试:
2.可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,
此属性可以在查询表中数据时,自动将_类型的字段名,即下划线转换为驼峰
举个栗子:
例如:字段名user_id
,设置了mapUnderscoreToCamelCase
,此时字段名就会转换为userId
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
4. 处理一对一映射
调节数据库字段与实体类的属性对应需要标签resultMap
,如上文那个简单的查询例子就可以这样写:
<resultMap id="empResultMap" type="Emp">
<id property="empId" column="emp_id"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="gender" column="gender"></result>
</resultMap>
<select id="getEmpById" resultMap="empResultMap">
select * from t_emp where emp_id = #{empId};
</select>
属性:
-
id:表示自定义映射的唯一标识
-
type:查询的数据要映射的实体类的类型
子标签:
-
id:设置主键的映射关系
-
result:设置普通字段的映射关系
-
association :设置多对一的映射关系
-
collection:设置一对多的映射关系
-
属性:
-
property:设置映射关系中实体类中的属性名
-
column:设置映射关系中表中的字段名
-
5. 处理多对一映射
5.1 级联方式处理
<resultMap id="empAndDeptResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!--部门中的字段dept_id与Emp实体类中的属性dept中的deptId相对应 -->
<!--部门中的字段dept_name与Emp实体类中的属性dept中的deptName相对应 -->
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap>
<select id="getEmpAndDeptById" resultMap="empAndDeptResultMap">
SELECT t_emp.*,t_dept.* FROM t_emp
LEFT JOIN t_dept
ON t_emp.dept_id=t_dept.dept_id
where t_emp.emp_id=#{empId}
</select>
接口:
测试方法:
public void test(){
SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
SqlSession sqlSession = sqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp empAndDeptById = mapper.getEmpAndDeptById(1);
System.out.println(empAndDeptById);
}
查询结果:
5.2 使用association处理映射关系
<resultMap id="empAndDeptResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<association property="dept" javaType="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
5.3 分步查询
第一步:查询员工信息
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.getDeptByStep" column="dept_id">
</association>
</resultMap>
<select id="getEmpAndDeptByStep" resultMap="empAndDeptByStepResultMap">
select * from t_emp where emp_id=#{empId};
</select>
注意:
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlid)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
第二步:根据员工所对应的部门 id 查询部门信息
<select id="getDeptByStep" resultType="com.atguigu.mybatis.pojo.Dept">
select * from t_dept where dept_id=#{deptId};
</select>
测试方法:
public void test(){
SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
SqlSession sqlSession = sqlSessionUtils.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp empAndDeptByStep = mapper.getEmpAndDeptByStep(1);
System.out.println(empAndDeptByStep);
}
执行结果:
分步查询的优点:可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled
:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载aggressiveLazyLoading
:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的 sql 。- 此时可通 association和 collection 中的
fetchType
属性设置当前的分步查询是否使用延迟加载, fetchType=“lazy(延迟加载) | eager(立即加载)”
6. 处理一对多查询
接口:
使用collection
处理
-
collection :设置一对多的映射关系
-
ofType :设置 collection 标签所处理的集合属性中存储数据的类型
<resultMap id="DeptAndEmpByDeptIdResultMap" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<collection property="emps" ofType="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
</collection>
</resultMap>
<select id="getDeptAndEmpByDeptId" resultMap="DeptAndEmpByDeptIdResultMap">
SELECT t_emp.*,t_dept.* FROM t_dept
LEFT JOIN t_emp
ON t_emp.dept_id=t_dept.dept_id
WHERE t_dept.dept_id=#{deptId}
</select>
测试:
public void test4(){
SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
SqlSession sqlSession = sqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1);
System.out.println(deptAndEmpByDeptId);
}
执行结果:
7. 小结
关系映射主要处理复杂的SQl查询,如子查询,多表联查等复杂查询,应用此种需求时可以考虑使用。
相关文章
- 一劳永逸,使用 PicGo + GitHub 搭建个人图床工具
- 欧盟拟制定监管"黑名单" Facebook苹果或将上榜
- 通过htmlWebpackPlugin.options.title修改页面标题的两种方法
- 有效混合云迁移策略的五个关键
- 美国防部注资6亿将5G军事化,彻底颠覆战争理念
- 蓝牙核心规范(V5.2)7.6-深入详解之GATT(1)
- Chrome的下载实在太蛋疼!教你如何改善它
- 蓝牙核心规范(V5.2)7.7-深入详解之GATT(2)
- 去呼呼,云掌柜与订单来了 管理类民宿软件大评测
- 为什么无线通信需要同步?
- 蓝牙核心规范(V5.2)7.8-深入详解之SMP(安全管理协议)(1)
- 《经济观察报》专访 SAP 大中华区总裁纪秉盟|成功来自梦想与细节的完美结合
- MyBatis练习(2)查询所有的一级分类,同时查询二级分类,以及查询二级分类下的所有图书
- 业风口渐起,SaaS国产替代如何飞起来?
- 网络分段策略如何与SD-WAN配合使用
- [Flutter翻译]Beamer v1.0.0发布了! 什么是新的和如何迁移
- 图解AOP相关术语
- 软通动力亮相华为全联接2020,CTO刘会福畅谈如何抓住新基建时代机遇
- 蓝牙核心规范(V5.2)7.9-深入详解之SMP(安全管理协议)-配对详解和BLE安全性(2)
- 【图解数据结构与算法】LRU缓存淘汰算法面试时到底该怎么写