springboot+mybatis一对多查询+懒加载实例

 更新时间:2025年11月19日 11:07:45   作者:一枚小麻瓜  
文章介绍了如何在Spring Boot和MyBatis中实现一对多查询的懒加载,通过配置MyBatis的`fetchType`属性,可以全局启用懒加载,或者在个别需要时关闭懒加载,文章通过代码示例和测试结果,展示了懒加载的实现过程

springboot+mybatis一对多查询+懒加载

​直接上图:

  • 父表

  • 子表

parent相关代码

entity

public class ParentMessage implements Serializable {

    private Integer id;

    private String value;

    private List<ChildMessage> childMessages;

    get set ......
}

mapper

@Repository
public interface ParentMessageMapper {

     List<ParentMessage> findAll();

     ParentMessage findById(Integer id);
}

service

@Service
public class ParentMessageService {

    @Autowired
    ParentMessageMapper parentMessageMapper;

    public List<ParentMessage> findAll(){

        return parentMessageMapper.findAll();
    }
}

mapper.xml

<?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="com.demo.mapper.ParentMessageMapper">

    <resultMap id="parentMessageMap" type="parentMessage">
        <id column="id" property="id"/>
        <result column="value" property="value" javaType="String"/>
        <collection fetchType="eager" property="childMessages" column="id" select="com.demo.mapper.ChildMessageMapper.findByOtherId" javaType="List" typeHandler="com.demo.mybatistypehandler.ListTypeHandler"/>
    </resultMap>

    <select id="findAll" resultMap="parentMessageMap">
        select id , value from mk_parentmessage
    </select>

    <select id="findById" resultType="parentMessage">
        select * from mk_parentmessage;
    </select>
</mapper>

child 相关代码

entity

public class ChildMessage implements Serializable {

    private Integer id;

    private String value;

    private ParentMessage parentMessage;

}

mapper

@Repository
public interface ChildMessageMapper {

    List<ChildMessage> findByOtherId(Integer id);

    List<ChildMessage> findAll();
}

service

@Service
public class ChildMessagService {

    @Autowired
    ChildMessageMapper childMessageMapper;

    public List<ChildMessage> findAll(){

        return childMessageMapper.findAll();
    }
}

mapper.xlm

<?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="com.demo.mapper.ChildMessageMapper">

    <resultMap id="childMessageMap" type="childMessage">
        <id column="id" property="id"/>
        <result column="value" property="value"/>
        <association property="parentMessage" column="parent_id" select="com.demo.mapper.ParentMessageMapper.findById" javaType="ParentMessage"/>
    </resultMap>

    <select id="findByOtherId" resultType="childMessage">
        select id id, value value from mk_childmessage;
    </select>

    <select id="findAll" resultMap="childMessageMap">
        select * from mk_childmessage;
    </select>
</mapper>

Controller测试

@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    ParentMessageService parentMessageService;

    @Autowired
    ChildMessagService childMessagService;

    @GetMapping("findAll")
    public ResponseEntity getParentMessage(){

        return ResponseEntity.ok(parentMessageService.findAll(););
    }

}

返回结果:

懒加载

配置属性

mybatis.configuration.lazy-loading-enabled=true
#false 为按需加载
mybatis.configuration.aggressive-lazy-loading=false

这样就实现了全局懒加载,若个别需要关闭,可用 fetchType=“eager”

例如下图:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java代码格式化工具(PMD,Checkstyle)的学习指南

    Java代码格式化工具(PMD,Checkstyle)的学习指南

    在 Java 开发的旅程中,编写整洁、规范的代码是每个开发者追求的目标,本文将深入探讨 PMD 和 Checkstyle 的功能,使用方法以及它们如何帮助我们让代码焕然一新吧
    2025-05-05
  • ThreadLocal导致JVM内存泄漏原因探究

    ThreadLocal导致JVM内存泄漏原因探究

    ThreadLocal是JDK提供的线程本地变量机制,但若使用不当可能导致内存泄漏。正确的使用方式是在使用完后及时remove,或者使用弱引用等手段避免强引用导致的内存泄漏。在多线程编程中,合理使用ThreadLocal可以提高并发性能,但也需要注意其潜在的内存泄漏问题
    2023-04-04
  • SpringBoot中的@ControllerAdvice注解原理详解

    SpringBoot中的@ControllerAdvice注解原理详解

    这篇文章主要介绍了SpringBoot中的@ControllerAdvice注解原理详解,在SpringBoot应用程序启动过程中,Spring会扫描所有的类,寻找带有@ControllerAdvice注解的类这些方法会被添加到一个映射表中,以便后续处理异常时能找到对应的处理方法,需要的朋友可以参考下
    2024-01-01
  • SpringBoot集成Quartz实现定时任务的方法

    SpringBoot集成Quartz实现定时任务的方法

    Quartz是一个定时任务框架,其他介绍网上也很详尽。这里要介绍一下Quartz里的几个非常核心的接口。通过实例代码给大家讲解SpringBoot集成Quartz实现定时任务的方法,感兴趣的朋友一起看看吧
    2020-05-05
  • 一文读懂Spring Bean的生命周期

    一文读懂Spring Bean的生命周期

    今天我们来说一说 Spring Bean 的生命周期,小伙伴们应该在面试中经常遇到,这是正常现象,本文让更多的小伙伴们可以轻松的读懂 Spring Bean 的生命周期
    2023-03-03
  • Java实现按字典顺序查找的booth算法的完整代码

    Java实现按字典顺序查找的booth算法的完整代码

    在字符串算法领域,最小表示法是一种用于求取一个环形字符串中字典序最小的排列位置的经典问题,本项目将基于Java完整实现Booth算法,帮助读者深入理解算法原理,学会在工程中高效地对循环字符串进行字典序比较,并掌握相关代码优化和边界处理技巧,需要的朋友可以参考下
    2025-07-07
  • Java 改造ayui表格组件实现多重排序

    Java 改造ayui表格组件实现多重排序

    layui 的表格组件目前只支持单列排序,在实际应用中并不能很好的支撑我们的业务需求。今天一时手痒,决定改造一番以支持多重排序。
    2021-04-04
  • MyBatis-Plus实现优雅处理JSON字段映射

    MyBatis-Plus实现优雅处理JSON字段映射

    默认情况下,MyBatis-Plus 是不支持直接映射 JSON 类型的,这时候就需要借助其他的方法,下面小编就来和大家讲讲MyBatis-Plus如何优雅处理JSON字段映射吧
    2025-04-04
  • ArrayList foreach循环增添删除导致ConcurrentModificationException解决分析

    ArrayList foreach循环增添删除导致ConcurrentModificationException解决分

    这篇文章主要为大家介绍了ArrayList foreach循环增添删除导致ConcurrentModificationException解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪<BR>
    2023-12-12
  • SpringBoot关于List集合的校验方式

    SpringBoot关于List集合的校验方式

    这篇文章主要介绍了SpringBoot关于List集合的校验方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07

最新评论