springboot+mybatis一對多查詢+懶加載實例
springboot+mybatis一對多查詢+懶加載
?直接上圖:
- 父表

- 子表

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測試
@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
這樣就實現(xiàn)了全局懶加載,若個別需要關(guān)閉,可用 fetchType=“eager”
例如下圖:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java經(jīng)典算法匯總之選擇排序(SelectionSort)
選擇排序也是比較簡單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過程中只記錄下來最小的一個元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個元素交換,這樣就降低了交換的次數(shù),提高了排序效率。2016-04-04
JavaEE中關(guān)于ServletConfig的小結(jié)
ServletConfig是針對特定的Servlet的參數(shù)或?qū)傩浴ervletConfig是表示單獨的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個servlet被實例化后,對任何客戶端在任何時候訪問有效,但僅對本servlet有效,一個servlet的ServletConfig對象不能被另一個servlet訪問2014-10-10
log4j中l(wèi)ogger標(biāo)簽中additivity屬性的用法說明
這篇文章主要介紹了log4j中l(wèi)ogger標(biāo)簽中additivity屬性的用法說明,基于很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java利用trueLicense實現(xiàn)項目離線證書授權(quán)操作步驟
文章介紹了如何使用trueLicense實現(xiàn)離線授權(quán)控制,包括生成公私鑰、創(chuàng)建證書校驗?zāi)K、生成證書模塊和測試模塊,通過這種方式,可以控制用戶使用的項目模塊、授權(quán)周期、使用的設(shè)備和服務(wù)器,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java8的Stream()與ParallelStream()的區(qū)別說明
這篇文章主要介紹了Java8的Stream()與ParallelStream()的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實現(xiàn)上傳網(wǎng)絡(luò)圖片到七牛云存儲詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)上傳網(wǎng)絡(luò)圖片到七牛云存儲,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12

