mybatis处理多对一的映射关系
创建数据库t_emp和t_dept


创建对应实体类
package org.example.entity;
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, Dept dept) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.gender = gender;
this.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 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;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", dept=" + dept +
'}';
}
}
package org.example.entity;
public class Dept {
private Integer deptId;
private String deptName;
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;
}
@Override
public String toString() {
return "Dept{" +
"deptId=" + deptId +
", deptName='" + deptName + '\'' +
'}';
}
}
mapper接口
public Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);
处理方式一:级联方式处理
mapper.xml
<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>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap> <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
SELECT t_emp.*,t_dept.dept_name FROM t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{empId}
</select>
处理方式二:使用association
mapper.xml
<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:设置需要处理映射关系的属性的属性名
javaType:设置需要处理的属性的类型
-->
<association property="dept" javaType="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap> <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
SELECT t_emp.*,t_dept.dept_name FROM t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = 1
</select>
测试代码

处理方式三:分步查询
创建EmpMapper
public interface EmpMapper {
public Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);
}
创建DeptMapper
public interface DeptMapper {
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
}
EmpMapper.xml
<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>
<!--
property:设置需要处理映射关系的属性的属性名
select:设置分布查询的sql的唯一标识
column:将查询出的某个字段作为分布查询的sql的条件
-->
<association property="dept" column="dept_id" select="org.example.mapper.DeptMapper.getEmpAndDeptByStepTwo">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap> <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select *
from t_emp where emp_id = #{empId};
</select>
DeptMapper.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="org.example.mapper.DeptMapper"> <select id="getEmpAndDeptByStepTwo" resultType="dept">
select * from t_dept where dept_id = #{deptId}
</select>
</mapper>
测试代码
@Test
public void testGetEmpAndDeptByStepOne(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = mapper.getEmpAndDeptByStepOne(2);
System.out.println(emp);
sqlSession.close();
}
mybatis懒加载全局配置
<settings>
<!--标准的日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启懒加载(开启延迟加载)-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭实时加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
mybatis处理多对一的映射关系的更多相关文章
- hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系
前面几篇都是介绍的用配置文件来实现实体类到数据库表的映射,这种方式是比较麻烦的,每一个pojo类都需要写一个相应的*.hbm.xml,无疑增加了很多代码量,不过也有优点就是利于维护,为了方便开发,Hi ...
- Hibernate框架双向多对多关联映射关系
建立双向多对多关联关系 Project.java (项目表) private Integer proid; private Strin ...
- Hibernate框架单向多对多关联映射关系
建立单向多对多关联关系 Project.java (项目表) private Integer proid; private Strin ...
- Hibernate框架单向多对一关联映射关系
建立多对一的单向关联关系 Emp.java private Integer empNo //员工编号 private String empName / ...
- hibernate笔记--单(双)向的多对多映射关系
在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我 ...
- hibernate(四) 双向多对多映射关系
序言 莫名长了几颗痘,真TM疼,可能是现在运动太少了,天天对着电脑,决定了,今天下午花两小时去跑步了, 现在继上一章节的一对多的映射关系讲解后,今天来讲讲多对多的映射关系把,明白了一对多,多对多个人感 ...
- Spring Boot 入门系列(二十八) JPA 的实体映射关系,一对一,一对多,多对多关系映射!
前面讲了Spring Boot 使用 JPA,实现JPA 的增.删.改.查的功能,同时也介绍了JPA的一些查询,自定义SQL查询等使用.JPA使用非常简单,功能非常强大的ORM框架,无需任何数据访问层 ...
- SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射
前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...
- 5、SpringBoot+Mybatis整合------多对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...
- resultMap处理字段和属性的映射关系
1.resultMap处理字段和属性的映射关系 若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射 <!-- resultMap:设置自定义映射 属性: id:表示自定 ...
随机推荐
- RSA_zd网校登录
网站 aHR0cHM6Ly91c2VyLndhbmd4aWFvLmNuL2xvZ2lu 点到密码登录,会返回验证码 输入错误的账号密码点登录抓包,可以看到密码是被加密的 initator点进去 简 ...
- uniapp 微信小程序-点击图片放大图片
<view class="pij-cont-imgbox" v-if='item.images.length>0'> <view class="p ...
- 【Python】pip的镜像安装异常解决方案
在安装pip的出现异常提示: ERROR: Could not find a version that satisfies the requirement pillow (from versions: ...
- python之路44 jQuery语法应用 与Bootstrap框架
写的略粗糙 咨询 https://www.cnblogs.com/Dominic-Ji/p/10490669.html 作业讲解 页面简陋定时器: <input type="text& ...
- Hugging News #0106
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...
- 笔记本USB接口案例_分析-笔记本USB接口案例_实现
笔记本USB接口案例_分析 笔记本电脑(laptop)通常具备使用USB设备的功能.在生产时,笔记本都预留了可以插入USB设备的USB接口, 但具体是什么USB设备,笔记本厂商并不关心,只要符合USB ...
- Windows静态库和动态库区别
个人建议:能使用静态库的就不要使用动态库,能使用隐式调用的就不要用显示调用. 注意: (1)动态库中的.lib文件叫做导入库,对于导入库而言,其实际的执行代码位于动态库中,导入库只包含了地址符 ...
- immutable.js学习笔记(六)----- OrderedSet
一.OrderedSet 二.普通Set 与 OrderedSet 注意:普通Set并不是严格的一定是升序的 三.takeWhile 四.升序 sort valueA - valueB 五.降序 va ...
- tomcat8 性能优化参考
https://www.jianshu.com/p/c770c1e97531 tomcat8 性能优化参考
- Idea未识别maven项目
https://blog.csdn.net/qq_41460654/article/details/120539509