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

MyBatis 一对多关联映射在Spring Boot中的XML配置

在Spring Boot中使用MyBatis实现一对多关系时,可以通过XML映射文件来配置。下面我将详细介绍几种实现方式。

基本概念

一对多关系指的是一个实体对象包含多个子对象集合的情况,例如:

  • 一个部门有多个员工
  • 一个订单有多个订单项
  • 一个博客有多个评论

实现方式

1. 使用嵌套结果映射(ResultMap)

<!-- DepartmentMapper.xml -->
<resultMap id="departmentWithEmployeesMap" type="com.example.Department"><id property="id" column="dept_id"/><result property="name" column="dept_name"/><!-- 一对多关系配置 --><collection property="employees" ofType="com.example.Employee"><id property="id" column="emp_id"/><result property="name" column="emp_name"/><result property="email" column="emp_email"/></collection>
</resultMap><select id="findDepartmentWithEmployees" resultMap="departmentWithEmployeesMap">SELECT d.id as dept_id,d.name as dept_name,e.id as emp_id,e.name as emp_name,e.email as emp_emailFROM department dLEFT JOIN employee e ON d.id = e.dept_idWHERE d.id = #{id}
</select>

2. 使用嵌套查询(Nested Query)

<!-- DepartmentMapper.xml -->
<resultMap id="departmentMap" type="com.example.Department"><id property="id" column="id"/><result property="name" column="name"/><collection property="employees" column="id" ofType="com.example.Employee"select="com.example.mapper.EmployeeMapper.findByDepartmentId"/>
</resultMap><select id="findById" resultMap="departmentMap">SELECT id, name FROM department WHERE id = #{id}
</select><!-- EmployeeMapper.xml -->
<select id="findByDepartmentId" resultType="com.example.Employee">SELECT id, name, email FROM employee WHERE dept_id = #{deptId}
</select>

实体类示例

// Department.java
public class Department {private Long id;private String name;private List<Employee> employees;// getters and setters
}// Employee.java
public class Employee {private Long id;private String name;private String email;// getters and setters
}

使用注解的替代方案

如果你更喜欢使用注解而不是XML,也可以这样配置:

@Mapper
public interface DepartmentMapper {@Select("SELECT id, name FROM department WHERE id = #{id}")@Results({@Result(property = "id", column = "id"),@Result(property = "name", column = "name"),@Result(property = "employees", column = "id",many = @Many(select = "com.example.mapper.EmployeeMapper.findByDepartmentId"))})Department findByIdWithEmployees(Long id);
}

性能考虑

  1. 嵌套结果映射:单次SQL查询,适合关联数据量不大的情况
  2. 嵌套查询:多次SQL查询,适合关联数据量大或需要延迟加载的情况

可以通过 fetchType 属性控制加载方式:

<collection property="employees" column="id" ofType="com.example.Employee"select="com.example.mapper.EmployeeMapper.findByDepartmentId"fetchType="lazy"/>  <!-- 或 eager -->

以上就是在Spring Boot中MyBatis实现一对多关系的XML配置方式。根据你的具体需求选择合适的方法。

http://www.dtcms.com/a/186665.html

相关文章:

  • day23 机器学习管道 Pipeline
  • 【sqlmap需要掌握的参数】
  • Windows重置网络,刷新缓存
  • Web 架构之故障自愈方案
  • 网络基础1(应用层、传输层)
  • ​​​​​​​大规模预训练范式(Large-scale Pre-training)
  • 连接词化归律详解
  • 【android bluetooth 案例分析 03】【PTS 测试 】【PBAP/PCE/SGSIT/SERR/BV-01-C】
  • 软考错题(四)
  • 24.(vue3.x+vite)引入组件并动态挂载(mount)
  • PINN应用案例:神经网络求解热扩散方程高质量近似解
  • C#中程序集的详解一
  • 搭建高可用及负载均衡的Redis
  • 单片机ESP32天气日历闹铃语音播报
  • 供应链学习
  • 《AI大模型应知应会100篇》第60篇:Pinecone 与 Milvus,向量数据库在大模型应用中的作用
  • Java大师成长计划之第20天:Spring Framework基础
  • java----------->代理模式
  • 专业课复习笔记 8
  • 【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
  • pdf 不是扫描件,但却无法搜索关键词【问题尝试解决未果记录】
  • Java中堆栈
  • 一个极简单的 VUE3 + Element-Plus 查询表单展开收起功能组件
  • 基于HTTP头部字段的SQL注入:SQLi-labs第17-20关
  • spring中的@PropertySource注解详解
  • 记录裁员后的半年前端求职经历
  • 【氮化镓】GaN在不同电子能量损失的SHI辐射下的损伤
  • 歌曲《忘尘谷》基于C语言的歌曲调性检测技术解析
  • Linux常用命令详解(下):打包压缩、文本编辑与查找命令
  • Codeforces Round 1024 (Div. 2)(A-D)