实体类

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. Python如何像awk一样分割字符串

    若你使用过 Shell 中的 awk 工具,会发现用它来分割字符串是非常方便的.特别是多个连续空格会被当做一个处理. [root@localhost ~]# cat demo.txt hello wo ...

  2. Java开发学习(五十)----MyBatisPlus快速开发之代码生成器解析

    1.代码生成器原理分析 造句: 我们可以往空白内容进行填词造句,比如: 在比如: 观察我们之前写的代码,会发现其中也会有很多重复内容,比如: 那我们就想,如果我想做一个Book模块的开发,是不是只需要 ...

  3. MornHus--一个野生蒟蒻的生成

    野生蒟蒻一个. 蒟蒻的洛谷首页:MornHus 蒟蒻已经遗弃的CSDN首页:MornHus 写博的内容: 主要就是平时写写算法的笔记,有的时候写点数学题,有的时候还会搞一些奇怪的东西[doge] 大蒟 ...

  4. threeJs构建3D世界

    threejs官网 https://threejs.org/docs/index.html#manual/zh/introduction/Installation (官网非常的详细) 导入安装 npm ...

  5. 使用ng-zorro图标库出现the icon redo-o does not exist or is not registered.

    出现这种情况一般是因为没导入你要的图标 可以在项目目录找到这个文件  src\style-icons-auto.ts 然后打开,导入导出里加上你要导入的图标....  就可以了 如果你不知道要怎么加 ...

  6. Flutter 耗时监控 | 路由名为空原因分析

    前言 最近群里遇到获取Route名为空的问题,当时没在意... 直到自己在监控页面启动耗时,需要确定当前页面是哪个从而方便标记它加载的耗时时,遇到同样 route.settings.name 为空问题 ...

  7. BOM操作、DOM操作、jQuery类库

    BOM操作.DOM操作.jQuery类库 一.BOM操作 BOM(Browser Object Model)是指浏览器对象模型,它使JavaScript有能力与浏览器进行对话 1.window对象 浏 ...

  8. 从 Cloud-Native Relational DB 看数据库设计

    论文内容:Amazon Aurora: Design Considerations for HighThroughput Cloud-Native Relational Databases 里面介绍了 ...

  9. 梳理selenium的鼠标方法

    梳理selenium的鼠标方法 你需要有一定的selenium基础 基本用法 包导入  from selenium.webdriver import ActionChains 调用方式一:链式调用   ...

  10. 力扣---1148. 文章浏览 I

    Views 表:+---------------+---------+| Column Name   | Type    |+---------------+---------+| article_i ...