创建数据库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处理多对一的映射关系的更多相关文章

  1. hibernate笔记--使用注解(annotation)方式配置单(双)向多对一的映射关系

    前面几篇都是介绍的用配置文件来实现实体类到数据库表的映射,这种方式是比较麻烦的,每一个pojo类都需要写一个相应的*.hbm.xml,无疑增加了很多代码量,不过也有优点就是利于维护,为了方便开发,Hi ...

  2. Hibernate框架双向多对多关联映射关系

    建立双向多对多关联关系    Project.java (项目表)                private Integer proid;                private Strin ...

  3. Hibernate框架单向多对多关联映射关系

    建立单向多对多关联关系    Project.java (项目表)                private Integer proid;                private Strin ...

  4. Hibernate框架单向多对一关联映射关系

    建立多对一的单向关联关系    Emp.java            private Integer empNo //员工编号            private String empName / ...

  5. hibernate笔记--单(双)向的多对多映射关系

    在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我 ...

  6. hibernate(四) 双向多对多映射关系

    序言 莫名长了几颗痘,真TM疼,可能是现在运动太少了,天天对着电脑,决定了,今天下午花两小时去跑步了, 现在继上一章节的一对多的映射关系讲解后,今天来讲讲多对多的映射关系把,明白了一对多,多对多个人感 ...

  7. Spring Boot 入门系列(二十八) JPA 的实体映射关系,一对一,一对多,多对多关系映射!

    前面讲了Spring Boot 使用 JPA,实现JPA 的增.删.改.查的功能,同时也介绍了JPA的一些查询,自定义SQL查询等使用.JPA使用非常简单,功能非常强大的ORM框架,无需任何数据访问层 ...

  8. SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射

    前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...

  9. 5、SpringBoot+Mybatis整合------多对多

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...

  10. resultMap处理字段和属性的映射关系

    1.resultMap处理字段和属性的映射关系 若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射 <!-- resultMap:设置自定义映射 属性: id:表示自定 ...

随机推荐

  1. 第四篇:前端之BOM与DOM

    前端基础之BOM和DOM   前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互 ...

  2. [seaborn] seaborn学习笔记8-避免过度绘图Avoid Overplotting

    8 避免过度绘图Avoid Overplotting(代码下载) 过度绘图是散点图及几天常见图表中最常见的问题之一.如下图所示当数据集很大时,散点图的点会重叠,使得图形变得不可读.在这篇文章中,提出了 ...

  3. LeetCode HOT 100:验证二叉搜索树(从左右子树获取信息进行推导)

    题目:98. 验证二叉搜索树 题目描述: 给你一个二叉树,让你判断该二叉树是否是二叉搜索树.什么是二叉搜索树呢?就是某一个节点的左子树上的所有节点的值都小于当前节点,右子树上的所有节点值都大于当前节点 ...

  4. appium基本使用(Android)

    一.环境搭建 详情可见:https://www.cnblogs.com/lihongtaoya/p/16971096.html 二.元素定位 详情可见:https://www.cnblogs.com/ ...

  5. VSCode运行C/C++配置

    将MinGw安装目录下的 1.安装 VSCode 2.安装 MinGW 链接:点击跳转 3.MinGW 内安装两个模块 1.右键 Mark for Installation 勾选 (此处已安装好,所以 ...

  6. 聊聊MongoDB中连接池、索引、事务

    大家好,我是哪吒. 三分钟你将学会: MongoDB连接池的使用方式与常用参数 查询五步走,能活九十九? MongoDB索引与MySQL索引有何异同? MongoDB事务与ACID 什么是聚合框架? ...

  7. 如何通过Java代码在PDF中插入、替换或删除图像?

    图文并茂的内容往往让人看起来更加舒服,如果只是文字内容的累加,往往会使读者产生视觉疲劳.搭配精美的文章配图则会使文章内容更加丰富,增加文章可读性的同时,也能提升用户体验.但由于PDF文档安全性较高,不 ...

  8. ABP Framework 手动升级指南:从6.0.1升级到7.0.0

    ABP 7.0.0 正式版已经发布,ABP-Framework-All-In-One 项目同步升级. LeptonX Lite Theme 目前还没有包含在源码解决方案中,还是以 Nuget 包提供, ...

  9. 题解 P5607 [Ynoi2013] 无力回天 NOI2017

    简要题意 其实我觉得这个部分可以不要,因为这道题的题面还是很清晰的. 你需要维护一个数据结构,支持区间异或和区间求与 \(v\) 的最大异或和. 思路 对于这种区间问题,最容易想到的就是 分块 线段树 ...

  10. LIS求解(包括优化)

    首先,让我来看看LIS问题 Description 一个数的序列 bi,当 b1 < b2 < ... < bS 的时候,我们称这个序列是上升的.对于给定的一个序列(a1,a2,.. ...