实体类

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 协程 爬百度小说西游记

    前言 方法,不止一种,有不同见解可以一起讨论 "" 使用协程爬取百度小说中的西游记整部小说 """ import asyncio import aio ...

  2. 4、PageHelper分页查询

    1.MyBatis的分页方式: 逻辑分页与物理分页 1.逻辑分页:使用MyBatis自带的RowBounds进行分页,是一次性查询很多数据,然后再在结果中检索分页的数据.这样做弊端是需要消耗大量的内存 ...

  3. 编写异步任务@Async出现bean无法注入的问题解决方案

    在编写一个异步任务时出现报错:"The bean 'asyncShenCe' could not be injected as a 'com.sinochem.api.service.imp ...

  4. [R语言] 基于R语言实现树形图的绘制

    树状图(或树形图)是一种网络结构.它由一个根节点组成,根节点产生由边或分支连接的多个节点.层次结构的最后一个节点称为叶.本文主要基于R语言实现树形图的绘制.关于python实现树形图的绘制见:基于ma ...

  5. python实现单向循环链表与双向链表

    目录 单向循环链表 操作 实现 双向链表 操作 实现 单向循环链表 单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点. 操作 is_empty() 判 ...

  6. [深度学习]Keras利用VGG进行迁移学习模板

    # -*- coding: UTF-8 -*- import keras from keras import Model from keras.applications import VGG16 fr ...

  7. 《Effective C++》资源管理章节

    Item 13:以对象管理资源 关键的两个想法(这种方式其实在很多地方都可以看出影子,比如managing pool的模型): 1.获得资源后立刻放入管理对象(managing object):以对象 ...

  8. SpringBoot 项目中配置多个 Jackson 的 ObjectMapper ,以及配置遇到的坑

    目录 问题说明 原因排查分析 结论总结 Jackson 自动装配分析 问题说明 我们都知道,SpringBoot 项目中,如果引入了 Jackson 的包,哪怕不配置,SpringBoot 也会帮我们 ...

  9. 【分析笔记】全志平台 TWI 上拉电压异常的问题

    记录说明 原本这么简单的芯片,没有必要做记录,后来发现其中有一颗单独挂在 TWI2 无法通信,而主要原因是最容易忽视的电源域的问题,因此记录一下这件事情. 芯片介绍 MCP3021 是一颗 10BIT ...

  10. 面试必问:说一下 Java 虚拟机的内存布局?

    我们通常所说的 Java 虚拟机(JVM)的内存布局,一般是指 Java 虚拟机的运行时数据区(Runtime Data Area),也就是当字节码被类加载器加载之后的执行区域划分.当然它通常是 JV ...