实体类

package org.example.entity;
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 +
'}';
}
}

mapper接口

 Dept getEmpAndDeptByDeptId(@Param("deptId") int deptId);

方式一:

collection 标签

<?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"><resultMap id="empAndDeptResultMap" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result> <!--
collection:处理一对多的映射关系(处理集合类型的属性)
ofType:设置集合类型的属性中存储的数据的类型
-->
<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="getEmpAndDeptByDeptId" resultMap="empAndDeptResultMap">
select * from t_dept a left join t_emp b on a.dept_id = b.dept_id where a.dept_id = #{deptId}
</select> </mapper>

方式二:

分布查询

DeptMapper接口

Dept getDeptAndEmpStepOne(@Param("deptId") int deptId);

EmpMapper接口

List<Emp> getDeptAndEmpStepTwo(@Param("deptId") Integer deptId);

DeptMapper.xml

<resultMap id="DeptAndEmpResultMapStepOne" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result> <collection property="emps"
select="org.example.mapper.EmpMapper.getDeptAndEmpStepTwo"
column="dept_id">
</collection>
</resultMap> <select id="getDeptAndEmpStepOne" resultMap="DeptAndEmpResultMapStepOne">
select * from t_dept where dept_id = #{deptId}
</select>

EmpMapper.xml

 <select id="getDeptAndEmpStepTwo" resultType="org.example.entity.Emp">
select * from t_emp where dept_id = #{deptId}
</select>

测试代码

@Test
public void testGetDeptAndEmpByStep(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpStepOne(1);
System.out.println(dept);
sqlSession.close();
}

结果:

Dept{deptId=1, deptName='A', emps=[Emp{empId=1, empName='张三', age=10, gender='男', dept=null}, Emp{empId=4, empName='赵六', age=15, gender='男', dept=null}]}

mybatis处理一对多的映射关系的更多相关文章

  1. Hibernate4.2.4入门(二)——一对多的映射关系

    一.前言 前面我们已经学过hibernate的基础,学会增删改查简单的操作,然而我们数据库中存在着1对多,多对1,多对多的关系,hibernate又是基于ORM基础上的开源框架,可以让我们不用去编写S ...

  2. MyBatis 中一对一和一对多的映射关系

    1 一对一映射 比如每位学生有一个地址. public class Address { private Integer addrId; private String street; private S ...

  3. mybatis的一对多双向映射

    连表查询 select id resultType resultMap resultType和resultMap不能同时使用 association 属性 映射到多对一中的“一”方的“复杂类型”属性, ...

  4. 4、SpringBoot+Mybatis整合------一对多

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

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

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

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

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

  7. Hibernate关联映射关系

    Hibernate关联映射关系 一.双向一对多关联映射关系:当类与类之间建立了关联,就可以方便的从一个对象导航到另一个或另一组与它关联的对象(一对多双向关联和多对一双向关联是完全一样的) 1.1创建实 ...

  8. Hibernate基于注解的双向one-to-many映射关系的实现

    在项目中用到了一对多的实体类关系映射,之前接触的都是基于配置文件的映射实现.可是公司的大部分都是基于注解的.因此自己參考之前的代码捣鼓了基于注解的一对多的映射关系实现. 背景: 一的一端:QingAo ...

  9. MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制

    映射(多.一)对一的关联关系 1)若只想得到关联对象的id属性,不用关联数据表 2)若希望得到关联对象的其他属性,要关联其数据表 举例: 员工与部门的映射关系为:多对一 1.创建表 员工表 确定其外键 ...

  10. mybatis入门基础(六)----高级映射(一对一,一对多,多对多)

    一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: CREATE TABLE items ( id INT NOT NULL AUTO_INCREMENT, itemsname ) NOT ...

随机推荐

  1. [seaborn] seaborn学习笔记3-直方图Histogramplot

    文章目录 3 直方图Histogramplot 1. 基本直方图的绘制 Basic histogram 2. 数据分布与密度信息显示 Control rug and density on seabor ...

  2. Cesium for Unreal加载倾斜摄影

    本文介绍UE4中通过Cesium插架加载本地倾斜摄影模型.Cesium for Unreal插件运行在UE环境何总运行Cesium,这样方便做一个GIS应用. 安装Cesium for Unreal插 ...

  3. python之路55 cookie与session 操作 把模块变成字符串进行导入

    django中间件三个了解的方法 1.process_view 路由匹配成功之后执行视图函数/类之前自动触发(顺序同process_request) 2.process_exception 视图函数/ ...

  4. ADC-CH32FV2x_V3x 框图功能详解

    主要特性(CH32FV2x_V3x) 系列:l 12 位分辨率l 支持 16 个外部通道和 2 个内部信号源采样l 多通道的多种采样转换方式:单次.连续.扫描.触发.间断等l 数据对齐模式:左对齐.右 ...

  5. 06yarn简介

    YARN简介 一.YARN是什么 YARN不是facebook的那个yarn,它从Hadoop 2引入,最初目的是改善MapReduce的实现,但是因为具备足够通用性,同样也可以支持其他的分布式计算模 ...

  6. 命令行部署KingbaseES流复制+切换测试

    建立系统数据库安装用户组及用户,在所有的节点执行 root用户登陆服务器,创建用户组及用户并且设置密码 groupadd -g 2000 kingbase useradd -G kingbase -g ...

  7. Vue 04 谷歌浏览器配置vue开发者工具

    参考链接:https://blog.csdn.net/wswq2505655377/article/details/111476799 1 插件下载 由于国内打不开谷歌商店,直接从网盘下载 链接:ht ...

  8. python字典生成式,列表生成式

    list1 = [1,2,3,4,5] list2 = ['a','b','c','d','e'] {k: v for k, v in zip(list1, list2)} # {1:'a',2:'b ...

  9. 【多线程与高并发】- synchronized锁的认知

    synchronized锁的认知 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 s ...

  10. 华为云API Arts:用“1+1+5”的模式,为你带来API-First体验

    摘要:华为云API Arts是API全生命周期一体化协作平台,支持开发者一站式高效实现API设计.API开发.API测试.API托管.API运维.API变现,助力企业数字化转型. 本文分享自华为云社区 ...