当前位置: 首页 > 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配置方式。根据你的具体需求选择合适的方法。

相关文章:

  • 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三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
  • 俄乌拟在土耳其举行会谈,特朗普:我可能飞过去
  • 在对国宝的探索中,让美育浸润小学校园与家庭
  • OpenAI与微软正谈判修改合作条款,以推进未来IPO
  • 美国“贸易战”前线的本土受害者:安静的洛杉矶港和准备关门的小公司
  • 梅花奖在上海|穿上初演时的服装,“鹮仙”朱洁静再起飞
  • 西藏日喀则市拉孜县发生5.5级地震,震源深度10千米