延遲加載
延遲加載 :
就是在需要用到數據時才進行加載,不需要用到數據時就不加載數據。延遲加載也稱懶加載.
好處:先從單表查詢,需要時再從關聯表去關聯查詢,大大提高數據庫性能,因爲查詢單表要比關聯查詢多張錶速
度要快。
壞處 :
因爲只有當需要用到數據時,纔會進行數據庫查詢,這樣在大批量數據查詢時,因爲查詢工作也要消耗
時間,所以可能造成用戶等待時間變長,造成用戶體驗下降。
使用 assocation 實現一對一延遲加載
User
public class User implements Serializable {
private static final long serialVersionUID = 2 L;
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
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;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}' ;
}
}
Account
public class Account implements Serializable {
private static final long serialVersionUID = 3 L;
private int id;
private int uid;
private int money;
private User user;
public User getUser ( ) {
return user;
}
public void setUser ( User user) {
this . user = user;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getUid ( ) {
return uid;
}
public void setUid ( int uid) {
this . uid = uid;
}
public int getMoney ( ) {
return money;
}
public void setMoney ( int money) {
this . money = money;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}' ;
}
}
UserDao
public interface UserDao {
public User findById ( int id) ;
}
AccountDao
public interface AccountDao {
public List< Account> findAll ( ) ;
}
UserDao.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.itheima.dao.UserDao" >
< select id = " findById" resultType = " user" parameterType = " int" >
select * from user where id = #{uid}
</ select>
</ mapper>
AccountDao.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.itheima.dao.AccountDao" >
< resultMap id = " accountUserMap" type = " account" >
< id property = " id" column = " id" > </ id>
< result property = " uid" column = " uid" > </ result>
< result property = " money" column = " money" > </ result>
< association property = " user" column = " uid" javaType = " user"
select = " com.itheima.dao.UserDao.findById" > </ association>
</ resultMap>
< select id = " findAll" resultMap = " accountUserMap" >
select * from account
</ select>
</ mapper>
SqlMapConfig.xml
< settings>
< setting name = " lazyLoadingEnabled" value = " true" />
< setting name = " aggressiveLazyLoading" value = " false" />
</ settings>
Test
public class MybatisTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private AccountDao accountDao;
@Before
public void init ( ) throws IOException {
in = Resources. getResourceAsStream ( "SqlMapConfig.xml" ) ;
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder ( ) ;
factory = builder. build ( in) ;
session = factory. openSession ( ) ;
accountDao = session. getMapper ( AccountDao. class ) ;
}
@After
public void destory ( ) throws IOException {
session. commit ( ) ;
session. close ( ) ;
in. close ( ) ;
}
@Test
public void findAllTest ( ) {
List< Account> accountDaoAll = accountDao. findAll ( ) ;
for ( Account account : accountDaoAll) {
System. out. println ( account) ;
}
}
}
使用 Collection
User
public class User implements Serializable {
private static final long serialVersionUID = 2 L;
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List< Account> accounts;
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;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}' ;
}
}
Account
public class Account implements Serializable {
private static final long serialVersionUID = 3 L;
private int id;
private int uid;
private int money;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getUid ( ) {
return uid;
}
public void setUid ( int uid) {
this . uid = uid;
}
public int getMoney ( ) {
return money;
}
public void setMoney ( int money) {
this . money = money;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}' ;
}
}
UserDao
public interface UserDao {
public List< User> findAll ( ) ;
public User findById ( int id) ;
}
AccountDao
public interface AccountDao {
public List< Account> findAll ( ) ;
public Account findById ( int id) ;
}
UserDao.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.itheima.dao.UserDao" >
< resultMap id = " userMap" type = " user" >
< id property = " id" column = " id" > </ id>
< result property = " username" column = " username" />
< result property = " birthday" column = " birthday" />
< result property = " sex" column = " sex" />
< result property = " address" column = " address" />
< collection property = " accounts" ofType = " account" column = " id"
select = " com.itheima.dao.AccountDao.findById" > </ collection>
</ resultMap>
< select id = " findAll" resultMap = " userMap" >
select * from user
</ select>
</ mapper>
AccountDao.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.itheima.dao.AccountDao" >
< select id = " findById" resultType = " account" parameterType = " int" >
select * from account where uid = #{uid}
</ select>
</ mapper>
Test
@Test
public void findAllTest ( ) {
List< User> userDaoAll = userDao. findAll ( ) ;
for ( User user : userDaoAll) {
System. out. println ( user) ;
System. out. println ( user. getAccounts ( ) ) ;
}
}