mybatis处理一对多的映射关系
实体类
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处理一对多的映射关系的更多相关文章
- Hibernate4.2.4入门(二)——一对多的映射关系
一.前言 前面我们已经学过hibernate的基础,学会增删改查简单的操作,然而我们数据库中存在着1对多,多对1,多对多的关系,hibernate又是基于ORM基础上的开源框架,可以让我们不用去编写S ...
- MyBatis 中一对一和一对多的映射关系
1 一对一映射 比如每位学生有一个地址. public class Address { private Integer addrId; private String street; private S ...
- mybatis的一对多双向映射
连表查询 select id resultType resultMap resultType和resultMap不能同时使用 association 属性 映射到多对一中的“一”方的“复杂类型”属性, ...
- 4、SpringBoot+Mybatis整合------一对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd99020 ...
- resultMap处理字段和属性的映射关系
1.resultMap处理字段和属性的映射关系 若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射 <!-- resultMap:设置自定义映射 属性: id:表示自定 ...
- hibernate(四) 双向多对多映射关系
序言 莫名长了几颗痘,真TM疼,可能是现在运动太少了,天天对着电脑,决定了,今天下午花两小时去跑步了, 现在继上一章节的一对多的映射关系讲解后,今天来讲讲多对多的映射关系把,明白了一对多,多对多个人感 ...
- Hibernate关联映射关系
Hibernate关联映射关系 一.双向一对多关联映射关系:当类与类之间建立了关联,就可以方便的从一个对象导航到另一个或另一组与它关联的对象(一对多双向关联和多对一双向关联是完全一样的) 1.1创建实 ...
- Hibernate基于注解的双向one-to-many映射关系的实现
在项目中用到了一对多的实体类关系映射,之前接触的都是基于配置文件的映射实现.可是公司的大部分都是基于注解的.因此自己參考之前的代码捣鼓了基于注解的一对多的映射关系实现. 背景: 一的一端:QingAo ...
- MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制
映射(多.一)对一的关联关系 1)若只想得到关联对象的id属性,不用关联数据表 2)若希望得到关联对象的其他属性,要关联其数据表 举例: 员工与部门的映射关系为:多对一 1.创建表 员工表 确定其外键 ...
- mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: CREATE TABLE items ( id INT NOT NULL AUTO_INCREMENT, itemsname ) NOT ...
随机推荐
- echarts去除下载小图标
toolbox: { show: true, orient: 'vertical', left: 'right', top: 'center', feature: { dataView: { read ...
- C#代码扫描工具Sonarqube + Win10+SqlServer2017
在之前的公司, 看到有用过代码扫描工具, 扫描C#代码, 最近公司也有考虑做这个,于是我便独自研究了一下,这里给大家做个分享 网上找了很多资料, 主要有以下问题: 1. Sonarqube用的是 旧版 ...
- SQLSERVER 的复合索引和包含索引到底有啥区别?
一:背景 1. 讲故事 在 SQLSERVER 中有非常多的索引,比如:聚集索引,非聚集索引,唯一索引,复合索引,Include索引,交叉索引,连接索引,奇葩索引等等,当索引多了之后很容易傻傻的分不清 ...
- idea的简单介绍
上一篇博客中只是了解一下java文件是怎么编译的,但是一般来说大家都是使用编程软件来进行开发,我是使用IntelliJ IDEA进行开发的 官网下载IDEA(自行安装哈):地址:https://www ...
- [C#]C++/CLI中interior_ptr和pin_ptr的区别
interior_ptr 当垃圾回收器移动对象时,Interior pointer能随之移动,并始终指向该对象. 但是如果把这个指针返回给外部函数,那么当垃圾回收时(垃圾回收期间会压缩对象,),对象地 ...
- 分布式协议与算法-Raft算法
本文总结自:极客时间韩健老师的分布式协议与算法实战课程. 大家都知道,Raft算法属于Multi-Paxos算法,它是在Multi-Paxos思想的基础上,做了一些简化和限制.关于Paxos算法,博主 ...
- maven依赖管理,生命周期,插件
依赖配置 依赖指当前项目运行所需的jar,一个项目可以设置多个依赖,在pom.xml中格式如下 <!--设置当前项目所依赖的所有jar--> <dependencies> &l ...
- Java 进阶P-8.15
对象串行化 ObjectInputStream类 readObject() ObjectOutputStream类 writeObject() Serializable接口 对象的寿命通常随着生成该对 ...
- Unity之生成扫描二维码
Unity之生成扫描二维码 Unity之生成扫描二维码 前言 开篇 Unity版本及使用插件 正题 前期准备 首先生成二维码 然后需要扫描二维码 该使用了 挂载脚本绑定按钮和输入框 运行内容 生成二维 ...
- ASP.NET Core 系列总结
<ASP.NET Core> 系列文章基于 .NET 3.1 和 .NET 6,主要是系统总结自己日常工作和学习中的知识点,之前是自己在 OneNote 上自己写,作为学习.总结笔记,逐渐 ...