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 ...
随机推荐
- python循环结构之for循环
在python中,for循环是应用非常广的循环语句,遍历字典.遍历列表等等... # for语句结构 for 遍历 in 序列: 执行语句 遍历字典 lipsticks = {"Chanel ...
- vscode问题:由于找不到ffmpag.dll文件,无法继续执行代码
工作中发现VS code打不开了,显示如下: 解决方法: 一.打开Microsoft VS Code 文件夹,发现一部分文件被打包进了一个叫"_"的文件夹(第一个) 二.把该文 ...
- javascript计算器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C语言读写txt文件
写入和读取txt文件 #include<stdio.h> #include<string.h> int main( int argc, char *argv[] ) { int ...
- GitHub车牌检测识别项目调研
一,EasyOCR 1.1,仓库介绍 1.2,使用记录 二,HyperLPR 2.1,HyperLPR 概述 2.3,使用记录 2.3,使用建议 三,simple-car-plate-recognit ...
- [编程基础] C++多线程入门10-packaged_task示例
原始C++标准仅支持单线程编程.新的C++标准(称为C++11或C++0x)于2011年发布.在C++11中,引入了新的线程库.因此运行本文程序需要C++至少符合C++11标准. 文章目录 10 pa ...
- python之路48 django 视图层、模板层
视图层之必会三板斧 用来处理请求的视图函数都必须返回HttpResponse对象 完全正确 class HttpResponse: pass return HttpResponse() def ren ...
- [WPF]Win10便签软件
项目地址 Github:项目地址 软件截图 项目中用到的技术和问题 [WPF]限制程序单例运行 [WPF]创建系统栏小图标 [WPF]程序随系统自启动 [WPF]xml序列化以及反序列化数据 [WPF ...
- day03-Spring管理Bean-IOC-01
Spring管理Bean-IOC 1.Spring配置/管理bean介绍 Bean管理包括两方面: 创建bean对象 给bean注入属性 Bean的配置方式: 基于xml文件配置方式 基于注解配置方式 ...
- 使用 flex布局 制作携程网首页
1. 技术选型 2. 搭建相关文件夹结构 3. 引入视口标签以及初始化样式 4. 常用初始化样式 5. 首页布局分析以及搜索模块布局 index.css /*搜索模块*/ .search-index{ ...