14-延迟加载
目錄
- 一、Mybatis 中的延遲加載
- 1.什么是延遲加載
- 2.什么是立即加載
- 二、一對一的延遲加載
- 1.實體類
- 2.接口
- 3.SqlMapConfig.xml
- 4.IAccountDao.xml
- 5.測試類
- 三、一對多的延遲加載
- 1.IUserDao.xml
問題:
遇到的不懂:
mybatis 中javaType和OfType 的區別:
JavaType和ofType都是用來指定對象類型的,但是JavaType是用來指定pojo中屬性的類型,而ofType指定的是映射到list集合屬性中pojo的類型。
一、Mybatis 中的延遲加載
問題:
解析:
1.什么是延遲加載
在真正使用數據時才發起的查詢,不用的時候不查詢。按需加載(懶加載)
2.什么是立即加載
不管用不用,只要一調用方法,馬上發起查詢
在對應的四種表關系中:一對多,多對一,一對一,多對多。下面按關聯對象的(多 or 一)分組
一對多,多對多:通常情況下我們都是采用延遲加載。
多對一,一對一:通常情況下我們都是采用立即加載。
二、一對一的延遲加載
一個賬戶對應一個用戶
1.實體類
public class Account implements Serializable {private Integer id;private Integer uid;private double money;private User user; } public class User implements Serializable {private Integer id;private String username;private String password;private Date birthday;private String address; }2.接口
public interface IUserDao {List<User> findAll();User findById(Integer uid); } public interface IAccountDao {List<Account> findAll(); }3.SqlMapConfig.xml
多了個 settings 的配置
具體可見 Mybatis 的 settings
<?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"><configuration><properties resource="jdbcConfig.properties"/><!--配置參數--><settings><!--開啟Mybatis支持延遲加載--><setting name="lazyLoadingEnabled" value="true"/><!--當開啟時,任何方法的調用都會加載該對象的所有屬性。 否則,每個屬性會按需加載--><setting name="aggressiveLazyLoading" value="false"/></settings><typeAliases><package name="domain"/></typeAliases><environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><package name="dao"/></mappers> </configuration>4.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="dao.IAccountDao"><resultMap id="accountUserMap" type="account"><id property="id" column="id"/><result property="uid" column="uid"/><result property="money" column="money"/><!--一對一的關系映射,配置封裝 user 的內容select 屬性指定的內容,查詢用戶的唯一標識column屬性指定的內容:用戶根據 id 查詢時,所需要的參數的值--><association property="user" column="uid" javaType="user" select="dao.IUserDao.findById"/></resultMap><select id="findAll" resultMap="accountUserMap">select *from account;</select> </mapper>5.測試類
@Testpublic void testFindAll(){List<Account> accounts=accountDao.findAll();for (Account account:accounts){System.out.println("-------------------");System.out.println(account.toString());System.out.println(account.getUser());}}三、一對多的延遲加載
一個用戶對應多個賬戶
1.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="dao.IUserDao"><resultMap id="userAccountMap" type="User"><id property="id" column="id"/><result property="username" column="username"/><result property="password" column="password"/><result property="address" column="address"/><result property="birthday" column="birthday"/><collection property="account" ofType="Account" select="dao.IAccountDao.findAccountById" column="id"/></resultMap><select id="findAll" resultType="user">select *from user;</select><select id="findById" parameterType="integer" resultMap="userAccountMap">select *from user where id=#{uid}</select> </mapper>轉載于:https://www.cnblogs.com/zuiren/p/11406147.html
總結