当前位置: 首页 > news >正文

Mybatis:一对一查询映射处理

Mybatis:一对一查询映射处理

  • 前言
  • 一、概述
  • 二、创建数据模型
  • 三、 问题
  • 四、解决方案
    • 1、方案一:级联方式处理映射关系
    • 2、方案二:使用association处理映射关系
    • 3、方案三:分步查询



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!

一、概述

MyBatis是一种流行的Java持久化框架,它提供了灵活而强大的查询映射功能。在一些复杂的数据模型中,一对一查询映射是一种常见的需求。本篇博客将详细介绍如何在MyBatis中处理一对一查询映射。

二、创建数据模型

假设我们有两张数据表,员工表和部门表,每个员工都只属于一个部门,我们需要创建对应的Java数据模型。

Emp.java

public class Emp {private Integer eid;private String empName;private Integer age;private String sex;private String email;private Dept dept;...}

Dept.java

public class Dept {private Integer did;private String deptName;private List<Emp> emps;...}

三、 问题

现在我们要查询员工信息以及员工所对应的部门信息,我们应该如何做呢?

四、解决方案

1、方案一:级联方式处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="dept_name"></result>
</resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(2);System.out.println(emp);//Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}}

2、方案二:使用association处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

   <resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--association:处理多对一的映射关系property:需要处理多对的映射关系的属性名javaType:该属性的类型过程:通过javaType,运用反射,确定其所有属性,再将column一一准确赋值给指定的属性,这样就得出了一个实体类对象,再将这个对象赋值给property中的对象名--><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}

3、方案三:分步查询

mybatis-config.xml

 <!--设置MyBatis的全局配置--><settings><!--将_自动映射为驼峰,emp_name:empName--><setting name="mapUnderscoreToCamelCase" value="true"/><!--开启延迟加载--><setting name="lazyLoadingEnabled" value="true"/></settings>

EmpMapper

/*** @description:通过分步查询查询员工以及员工所对应的部门信息*              分步查询第一步:查询员工信息* @author: Hey * @date: 2022/7/4 9:41* @param: [eid]* @return: com.ir.mybatis.pojo.Emp**/Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="empAndDeptByStepResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)column:设置分布查询的条件:根据员工的部门的did去查询该员工所属部门的信息fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载--><association property="dept"select="com.ir.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association>
</resultMap><!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);--><select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}</select>

DeptMapper

/*** @description:通过分步查询查询部门以及部门中所有的员工信息*              分步查询第二步:根据did查询员工信息* @author: Hey * @date: 2022/7/4 9:42* @param: [did]* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

DeptMapper.xml

 <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where did = #{did}</select>

ResultTest

/*** @description:通过分步查询查询部门以及部门中所有的员工信息* @author: Hey * @date: 2022/7/4 9:53* @param: []* @return: void**/@Testpublic void testGetEmpAndDeptByStep(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptByStepOne(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}
http://www.lryc.cn/news/112605.html

相关文章:

  • 九、用 ChatGPT 提高算法和编程能力
  • 【数模】主成分分析PCA
  • 全志F1C200S嵌入式驱动开发(从DDR中截取内存)
  • C++中点云聚类算法的实现与应用探索
  • 大数据Flink(五十六):Standalone伪分布环境(开发测试)
  • Godot 4 源码分析 - 碰撞
  • 前端面试经典算法题
  • ospf减少LSA更新
  • 万字长文解析深度学习中的术语
  • 冠达管理投资前瞻:三星加码机器人领域 大信创建设提速
  • 24届近5年上海交通大学自动化考研院校分析
  • 【PDF密码】PDF文件不能打印,为什么?
  • LeetCode-Java(03)
  • 【Linux命令行与Shell脚本编程】第十六章 Shell函数
  • SpringCloud-Hystrix服务熔断与降级工作原理源码 | 京东物流技术团队
  • (一)react脚手架
  • Typescript中的元组与数组的区别
  • SpringBoot的index首页的访问、自定义Favicon图标
  • 【C++】C++文件操作-文本文件/二进制文件
  • java通过http网络url下载文件
  • 网络安全【黑客】自学
  • PCA和自动编码器:每个人都能理解的算法
  • C++——STL容器【priority_queue】模拟实现
  • SpringBoot实现文件记录日志,日志文件自动归档和压缩
  • MySQL 窗口函数
  • 0140 数据链路层2
  • Python字典的应用场景
  • 关于外贸跟进客户过程中需要注意的地方
  • AI绘画:两组赛博咒语和ComfyUI使用方法
  • Nacos源码 (2) 核心模块