MyBatis延遲加載與立即加載案例教程
MyBatis入門-延遲加載與立即加載
加載策略
延遲加載
延遲加載(lazy load)是(也稱為懶加載)Hibernate3關(guān)聯(lián)關(guān)系對(duì)象默認(rèn)的加載方式,延遲加載機(jī)制是為了避免一些無(wú)謂的性能開(kāi)銷而提出來(lái)的,所謂延遲加載就是當(dāng)在真正需要數(shù)據(jù)的時(shí)候,才真正執(zhí)行數(shù)據(jù)加載操作。延遲加載,可以簡(jiǎn)單理解為,只有在使用的時(shí)候,才會(huì)發(fā)出sql語(yǔ)句進(jìn)行查詢。
需要在主配置文件開(kāi)啟加載策略,子配置文件使用collection屬性

立即加載
所謂立即加載就是查詢時(shí),所有的相關(guān)數(shù)據(jù)一次被讀取出來(lái),而不是分N次。
一對(duì)一實(shí)現(xiàn)理解加載——查詢賬戶可查出用戶信息(在查詢賬戶是綁定查詢用戶信息的方法)
一對(duì)多實(shí)現(xiàn)延遲加載——查詢用戶可查出所以賬戶信息(在查詢用戶是綁定查詢賬戶信息的方法)
基礎(chǔ)數(shù)據(jù)
實(shí)體類
public class User implements Serializable {
/**
* Java實(shí)體類為什么要實(shí)現(xiàn)Serializable接口
* 1.用于序列化與反序列化--一個(gè)類只有實(shí)現(xiàn)了Serializable接口,它的對(duì)象才能被序列化。
* 2.Serializable接口就是Java提供用來(lái)進(jìn)行高效率的異地共享實(shí)例對(duì)象的機(jī)制,實(shí)現(xiàn)這個(gè)接口即可。
*/
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
//一對(duì)多關(guān)系映射:主表實(shí)體應(yīng)該包含從表實(shí)體的集合引用
private List<Account> accounts;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//一對(duì)一的關(guān)系中
//從表實(shí)體應(yīng)該包含一個(gè)主表實(shí)體的對(duì)象引用
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}';
}
}
dao層的兩個(gè)接口
/**
* @Author: Promsing
* @Date: 2021/4/4 - 16:21
* @Description: 描述 形容
* @version: 1.0
*/
public interface IUserDao {
/**
* 查詢所有
* @return
*/
List<User> findAll();
/**
* 根據(jù)id查詢
* @param i
* @return
*/
User findById(Integer i);
}
public interface IAccountDao {
/**
* 查詢所有賬戶,同時(shí)還有獲取當(dāng)前賬戶所屬的用戶信息
* @return
*/
List<Account> findAll();
/**
* 根據(jù)用戶id查詢
* @param i
* @return
*/
Account findById(Integer i);
}
resource下的主映射文件與子映射文件
主:SqlMapConfig_anno.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--myBatis的主配置文件 -->
<configuration>
<settings>
<!--開(kāi)啟mybatis支持延遲加載-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!--配置環(huán)境-->
<environments default="mysql">
<!--配置mysql環(huán)境-->
<environment id="mysql">
<!--配置事務(wù)的類型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置數(shù)據(jù)源(連接池)-->
<dataSource type="POOLED">
<!--配置連接數(shù)據(jù)庫(kù)的基本信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--映射文件 配置文件方式-->
<mappers>
<mapper resource="com/dynamic_annotation/dao/IAccountDao.xml"></mapper>
<mapper resource="com/dynamic_annotation/dao/IUserDao.xml"></mapper>
</mappers>
<!--映射文件 注解方式(使用注解就要?jiǎng)h除源配置文件)-->
<!-- <mappers>
<mapper class="com.dynamic_basics.dao.IUserDao"></mapper>
</mappers>-->
</configuration>
子:IAccountDao.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.dynamic_annotation.dao.IAccountDao">
<!--定義封裝account和user的resultMap-->
<resultMap id="accountUserMap" type="com.dynamic_annotation.domain.Account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--立即加載-->
<!--使用select屬性,使用其他DAO層的方法-->
<association property="user" column="uid" javaType="com.dynamic_annotation.domain.User" select="com.dynamic_annotation.dao.IUserDao.findById">
</association>
</resultMap>
<!--查詢所有 id使用方法名-->
<select id="findAll" resultMap="accountUserMap">
select * from Account
</select>
<!--單個(gè)查詢-->
<select id="findById" parameterType="int" resultType="com.dynamic_annotation.domain.Account">
select * from Account where uid=#{id}
</select>
</mapper>
子:IUserDao.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.dynamic_annotation.dao.IUserDao">
<!--定義封裝account和user的resultMap-->
<resultMap id="userAccountMap" type="com.dynamic_annotation.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- collection 是用于建立一對(duì)多中集合屬性的對(duì)應(yīng)關(guān)系
ofType 用于指定集合元素的數(shù)據(jù)類型
select 是用于指定查詢賬戶的唯一標(biāo)識(shí)(賬戶的 dao 全限定類名加上方法名稱)
column 是用于指定使用哪個(gè)字段的值作為條件查詢
-->
<!--延遲加載-->
<collection property="accounts" ofType="com.dynamic_annotation.domain.Account" select="com.dynamic_annotation.dao.IAccountDao.findById" column="id"></collection>
</resultMap>
<!--查詢所有 id使用方法名-->
<select id="findAll" resultMap="userAccountMap">
select * from User
</select>
<!--根據(jù)id查詢-->
<select id="findById" parameterType="int" resultType="com.dynamic_annotation.domain.User">
select * from user where id=#{id}
</select>
</mapper>
測(cè)試類
public class AnnotationTest {
private InputStream in;
private SqlSession sqlSession;
private IAccountDao accountDao;
private IUserDao userDao;
@Before
public void init()throws Exception{
//1.讀取配置文件 Resources是myBatis封裝的類
in= Resources.getResourceAsStream("SqlMapConfig_anno.xml");
//2.創(chuàng)建SQLSessionFactory工廠
// SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
//3.使用工廠生產(chǎn)SQLSession對(duì)象
sqlSession = factory.openSession();
//4.使用SQLSession創(chuàng)建DAO接口的代理對(duì)象
accountDao = sqlSession.getMapper(IAccountDao.class);
userDao=sqlSession.getMapper(IUserDao.class);
}
@After
public void destroy()throws Exception{
//6.釋放資源
//提交事務(wù)
sqlSession.commit();
sqlSession.close();
in.close();
}
//入門案例
@Test
public void testFindAllAccount(){
List<Account> accounts = accountDao.findAll();
System.out.println("------每個(gè)account的信息------");
for (Account account : accounts) {
System.out.println(account);
System.out.println(account.getUser());
}
}
@Test
public void testFindAccount(){
Account account = accountDao.findById(46);
System.out.println(account);
}
@Test
public void testFindAllUser(){
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
System.out.println(user.getAccounts());
}
}
@Test
public void testFindUser(){
User user = userDao.findById(49);
System.out.println(user);
}
}
到此這篇關(guān)于MyBatis延遲加載與立即加載案例教程的文章就介紹到這了,更多相關(guān)MyBatis延遲加載與立即加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis高級(jí)映射學(xué)習(xí)教程
- MyBatis高級(jí)映射和查詢緩存
- Mybatis高級(jí)映射、動(dòng)態(tài)SQL及獲得自增主鍵的解析
- mybatis高級(jí)映射一對(duì)多查詢實(shí)現(xiàn)代碼
- 基于mybatis高級(jí)映射多對(duì)多查詢的實(shí)現(xiàn)
- javaMybatis映射屬性,高級(jí)映射詳解
- 解析Mybatis延遲加載問(wèn)題
- MyBatis高級(jí)映射ResultMap解決屬性問(wèn)題
- Mybatis中的延遲加載,以及原理分析
- MyBatis實(shí)現(xiàn)高級(jí)映射的示例代碼
- 詳解MyBatis延遲加載是如何實(shí)現(xiàn)的
- MyBatis高級(jí)映射及延遲加載的實(shí)現(xiàn)
相關(guān)文章
springboot解決XSS存儲(chǔ)型漏洞問(wèn)題
這篇文章主要介紹了springboot解決XSS存儲(chǔ)型漏洞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解
這篇文章主要介紹了Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
手寫(xiě)java性能測(cè)試框架的實(shí)現(xiàn)示例
這篇文章主要為大家介紹了java實(shí)現(xiàn)性能測(cè)試框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Java中的接口以及常見(jiàn)的Cloneable接口用法
這篇文章主要介紹了Java中的接口以及常見(jiàn)的Cloneable接口用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問(wèn)題案例詳解
這篇文章主要介紹了Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問(wèn)題,結(jié)合具體演繹哲學(xué)家進(jìn)餐問(wèn)題的案例形式詳細(xì)分析了死鎖機(jī)制與原理,需要的朋友可以參考下2019-10-10

