springboot+mybatis一對(duì)多查詢+懶加載實(shí)例
springboot+mybatis一對(duì)多查詢+懶加載
?直接上圖:
- 父表

- 子表

parent相關(guān)代碼
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 相關(guān)代碼
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測(cè)試
@RestController
@RequestMapping("message")
public class MessageController {
@Autowired
ParentMessageService parentMessageService;
@Autowired
ChildMessagService childMessagService;
@GetMapping("findAll")
public ResponseEntity getParentMessage(){
return ResponseEntity.ok(parentMessageService.findAll(););
}
}
返回結(jié)果:

懶加載
配置屬性
mybatis.configuration.lazy-loading-enabled=true #false 為按需加載 mybatis.configuration.aggressive-lazy-loading=false
這樣就實(shí)現(xiàn)了全局懶加載,若個(gè)別需要關(guān)閉,可用 fetchType=“eager”
例如下圖:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java代碼格式化工具(PMD,Checkstyle)的學(xué)習(xí)指南
在?Java?開發(fā)的旅程中,編寫整潔、規(guī)范的代碼是每個(gè)開發(fā)者追求的目標(biāo),本文將深入探討?PMD?和?Checkstyle?的功能,使用方法以及它們?nèi)绾螏椭覀冏尨a煥然一新吧2025-05-05
ThreadLocal導(dǎo)致JVM內(nèi)存泄漏原因探究
ThreadLocal是JDK提供的線程本地變量機(jī)制,但若使用不當(dāng)可能導(dǎo)致內(nèi)存泄漏。正確的使用方式是在使用完后及時(shí)remove,或者使用弱引用等手段避免強(qiáng)引用導(dǎo)致的內(nèi)存泄漏。在多線程編程中,合理使用ThreadLocal可以提高并發(fā)性能,但也需要注意其潛在的內(nèi)存泄漏問(wèn)題2023-04-04
SpringBoot中的@ControllerAdvice注解原理詳解
這篇文章主要介紹了SpringBoot中的@ControllerAdvice注解原理詳解,在SpringBoot應(yīng)用程序啟動(dòng)過(guò)程中,Spring會(huì)掃描所有的類,尋找?guī)в蠤ControllerAdvice注解的類這些方法會(huì)被添加到一個(gè)映射表中,以便后續(xù)處理異常時(shí)能找到對(duì)應(yīng)的處理方法,需要的朋友可以參考下2024-01-01
SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法
Quartz是一個(gè)定時(shí)任務(wù)框架,其他介紹網(wǎng)上也很詳盡。這里要介紹一下Quartz里的幾個(gè)非常核心的接口。通過(guò)實(shí)例代碼給大家講解SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法,感興趣的朋友一起看看吧2020-05-05
Java實(shí)現(xiàn)按字典順序查找的booth算法的完整代碼
在字符串算法領(lǐng)域,最小表示法是一種用于求取一個(gè)環(huán)形字符串中字典序最小的排列位置的經(jīng)典問(wèn)題,本項(xiàng)目將基于Java完整實(shí)現(xiàn)Booth算法,幫助讀者深入理解算法原理,學(xué)會(huì)在工程中高效地對(duì)循環(huán)字符串進(jìn)行字典序比較,并掌握相關(guān)代碼優(yōu)化和邊界處理技巧,需要的朋友可以參考下2025-07-07
Java 改造ayui表格組件實(shí)現(xiàn)多重排序
layui 的表格組件目前只支持單列排序,在實(shí)際應(yīng)用中并不能很好的支撐我們的業(yè)務(wù)需求。今天一時(shí)手癢,決定改造一番以支持多重排序。2021-04-04
MyBatis-Plus實(shí)現(xiàn)優(yōu)雅處理JSON字段映射
默認(rèn)情況下,MyBatis-Plus 是不支持直接映射 JSON 類型的,這時(shí)候就需要借助其他的方法,下面小編就來(lái)和大家講講MyBatis-Plus如何優(yōu)雅處理JSON字段映射吧2025-04-04
ArrayList?foreach循環(huán)增添刪除導(dǎo)致ConcurrentModificationException解決分
這篇文章主要為大家介紹了ArrayList?foreach循環(huán)增添刪除導(dǎo)致ConcurrentModificationException解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-12-12
SpringBoot關(guān)于List集合的校驗(yàn)方式
這篇文章主要介紹了SpringBoot關(guān)于List集合的校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

