MyBatis延遲加載與立即加載案例教程
MyBatis入門-延遲加載與立即加載
加載策略
延遲加載
延遲加載(lazy load)是(也稱為懶加載)Hibernate3關(guān)聯(lián)關(guān)系對象默認(rèn)的加載方式,延遲加載機(jī)制是為了避免一些無謂的性能開銷而提出來的,所謂延遲加載就是當(dāng)在真正需要數(shù)據(jù)的時候,才真正執(zhí)行數(shù)據(jù)加載操作。延遲加載,可以簡單理解為,只有在使用的時候,才會發(fā)出sql語句進(jìn)行查詢。
需要在主配置文件開啟加載策略,子配置文件使用collection屬性
立即加載
所謂立即加載就是查詢時,所有的相關(guān)數(shù)據(jù)一次被讀取出來,而不是分N次。
一對一實(shí)現(xiàn)理解加載——查詢賬戶可查出用戶信息(在查詢賬戶是綁定查詢用戶信息的方法)
一對多實(shí)現(xiàn)延遲加載——查詢用戶可查出所以賬戶信息(在查詢用戶是綁定查詢賬戶信息的方法)
基礎(chǔ)數(shù)據(jù)
實(shí)體類
public class User implements Serializable { /** * Java實(shí)體類為什么要實(shí)現(xiàn)Serializable接口 * 1.用于序列化與反序列化--一個類只有實(shí)現(xiàn)了Serializable接口,它的對象才能被序列化。 * 2.Serializable接口就是Java提供用來進(jìn)行高效率的異地共享實(shí)例對象的機(jī)制,實(shí)現(xiàn)這個接口即可。 */ private Integer id; private String username; private Date birthday; private String sex; private String address; //一對多關(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; //一對一的關(guān)系中 //從表實(shí)體應(yīng)該包含一個主表實(shí)體的對象引用 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層的兩個接口
/** * @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 { /** * 查詢所有賬戶,同時還有獲取當(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> <!--開啟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ù)庫的基本信息--> <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> <!--映射文件 注解方式(使用注解就要刪除源配置文件)--> <!-- <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> <!--單個查詢--> <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 是用于建立一對多中集合屬性的對應(yīng)關(guān)系 ofType 用于指定集合元素的數(shù)據(jù)類型 select 是用于指定查詢賬戶的唯一標(biāo)識(賬戶的 dao 全限定類名加上方法名稱) column 是用于指定使用哪個字段的值作為條件查詢 --> <!--延遲加載--> <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>
測試類
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對象 sqlSession = factory.openSession(); //4.使用SQLSession創(chuàng)建DAO接口的代理對象 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("------每個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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis高級映射學(xué)習(xí)教程
- MyBatis高級映射和查詢緩存
- Mybatis高級映射、動態(tài)SQL及獲得自增主鍵的解析
- mybatis高級映射一對多查詢實(shí)現(xiàn)代碼
- 基于mybatis高級映射多對多查詢的實(shí)現(xiàn)
- javaMybatis映射屬性,高級映射詳解
- 解析Mybatis延遲加載問題
- MyBatis高級映射ResultMap解決屬性問題
- Mybatis中的延遲加載,以及原理分析
- MyBatis實(shí)現(xiàn)高級映射的示例代碼
- 詳解MyBatis延遲加載是如何實(shí)現(xiàn)的
- MyBatis高級映射及延遲加載的實(shí)現(xiàn)
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問題案例詳解
這篇文章主要介紹了Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問題,結(jié)合具體演繹哲學(xué)家進(jìn)餐問題的案例形式詳細(xì)分析了死鎖機(jī)制與原理,需要的朋友可以參考下2019-10-10