1 延時加載
- 延遲加載又叫懶加載,也叫按需加載。也就是說先加載主信息,在需要的時候,再去加載從信息。
- 在mybatis中,resultMap標籤 的association標籤和collection標籤具有延遲加載的功能;
1.1 案例
- OrderMapper.xml
<!--懶加載-->
<resultMap id="orderLazyloadingRslMap" type="orders">
<id column="id" property="id"></id>
<result column="number" property="number"></result>
<result column="createtime" property="createtime"></result>
<result column="note" property="note"></result>
<!--配置查詢-->
<association
property="user"
select="com.tzb.mapper.UserMapper.findUserById"
column="user_id">
</association>
</resultMap>
<select id="findOrderAndUserByLazyLoading" resultMap="orderLazyloadingRslMap">
SELECT * FROM orders
</select>
- 爲使用懶加載時
- 測試
@Test
public void test1() throws IOException {
// 代理
OrderMapper mapper = session.getMapper(OrderMapper.class);
List<Orders> orders = mapper.findOrderAndUserByLazyLoading();
for (Orders order : orders) {
System.out.println("訂單信息:");
System.out.println(order);
System.out.println("訂單所屬用戶:");
System.out.println(order.getUser());
}
}
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@75e04ee8]
DEBUG [main] - ==> Preparing: SELECT * FROM orders
DEBUG [main] - ==> Parameters:
DEBUG [main] - ====> Preparing: SELECT * FROM user WHERE id = ?;
DEBUG [main] - ====> Parameters: 1(Integer)
DEBUG [main] - <==== Total: 1
DEBUG [main] - ====> Preparing: SELECT * FROM user WHERE id = ?;
DEBUG [main] - ====> Parameters: 10(Integer)
DEBUG [main] - <==== Total: 1
DEBUG [main] - <== Total: 3
訂單信息:
Orders{id=3, user_id=null, note='null', number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015}
訂單所屬用戶:
User [id=1, username=王五, sex=2, birthday=null, address=上海]
訂單信息:
Orders{id=4, user_id=null, note='null', number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015}
訂單所屬用戶:
User [id=1, username=王五, sex=2, birthday=null, address=上海]
訂單信息:
Orders{id=5, user_id=null, note='null', number='1000012', createtime=Thu Feb 12 16:13:23 CST 2015}
訂單所屬用戶:
User [id=10, username=張三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市]
after.......關閉 session
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@75e04ee8]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@75e04ee8]
DEBUG [main] - Returned connection 1977634536 to pool.
Process finished with exit code 0
- 配置懶加載
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@62158e60]
DEBUG [main] - ==> Preparing: SELECT * FROM orders
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 3
訂單信息:
DEBUG [main] - ==> Preparing: SELECT * FROM user WHERE id = ?;
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
Orders{id=3, user_id=null, note='null', number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015}
訂單所屬用戶:
User [id=1, username=王五, sex=2, birthday=null, address=上海]
訂單信息:
Orders{id=4, user_id=null, note='null', number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015}
訂單所屬用戶:
User [id=1, username=王五, sex=2, birthday=null, address=上海]
訂單信息:
DEBUG [main] - ==> Preparing: SELECT * FROM user WHERE id = ?;
DEBUG [main] - ==> Parameters: 10(Integer)
DEBUG [main] - <== Total: 1
Orders{id=5, user_id=null, note='null', number='1000012', createtime=Thu Feb 12 16:13:23 CST 2015}
訂單所屬用戶:
User [id=10, username=張三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市]
after.......關閉 session
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.Connection@62158e60]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.Connection@62158e60]
DEBUG [main] - Returned connection 1645579872 to pool.
Process finished with exit code 0