主要体现在关联查询上
一、开启延时加载
需要在核心配置文件中做两个必须的配置
<configuration>
<settings>
<!– 延迟加载总开关 –>
<setting name="lazyLoadingEnabled" value="true" />
<!– 禁止积极主动的加载 –>
<setting name="aggressiveLazyLoading" value="false" />
</settings>
二、一对多
对于Person来说
一条SQL语句查询Person的基本信息
一条SQL语句查询Person的订单(懒加载的语句)
1.在PersonMapper中
<resultMap type="com.yutouxiuxiu.model.Person" id="selectOrderByPersonIdLazyRM" extends="personRM">
<!–
column - 主SQL语句查询的结果集的某一字段作为参数传给子SQL语句
select - 子SQL语句 –>
<collection property="orderList" column="person_id" select="com.yutouxiuxiu.Orders.selectOrderByPersonId">
</collection>
</resultMap>
<select id="selectOrderByPersonIdLazy" parameterType="int" resultMap="selectOrderByPersonIdLazyRM">
select * from person where person_id = #{personId}
</select>
2.在OrdersMapper中
<!– 一对多的时候的延迟加载的子SQL语句 –>
<select id="selectOrderByPersonId" parameterType="int" resultMap="BaseResultMap">
select * from orders o where o.person_id = #{personId}
</select>
@Test
public void selectOrderByPersonIdLazy() {
// 获得session
SqlSession session = sqlSessionFactory.openSession();
try {
Person person = session.selectOne("com.yutouxiuxiu.Person.selectOrderByPersonIdLazy", 1);
// 发出查询person信息的SQL语句
System.out.println(person);
// 发出查询订单信息的SQL语句
System.out.println(person.getOrderList());
} finally {
session.close();
}
}
执行结果:
2014-02-11 11:17:30,550 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ooo Using Connection [com.mysql.jdbc.Connection@4e94ac10]
2014-02-11 11:17:30,551 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ==> Preparing: select * from person where person_id = ?
2014-02-11 11:17:30,602 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ==> Parameters: 1(Integer)
2014-02-11 11:17:30,702 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ooo Using Connection [com.mysql.jdbc.Connection@4e94ac10]
2014-02-11 11:17:30,702 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ==> Preparing: select * from orders o where o.person_id = ?
2014-02-11 11:17:30,703 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ==> Parameters: 1(Integer)
Person [id=1, name=赵四, birthday=Mon Feb 10 00:00:00 CST 2014, address=象牙山, salary=1000, orderList=[Orders [orderId=1, orderSum=1000.0, orderTime=Sun Feb 09 16:28:26 CST 2014, personId=1, detialList=null, person=null], Orders [orderId=2, orderSum=200.0, orderTime=Sun Feb 09 09:09:00 CST 2014, personId=1, detialList=null, person=null]], roleList=null]
[Orders [orderId=1, orderSum=1000.0, orderTime=Sun Feb 09 16:28:26 CST 2014, personId=1, detialList=null, person=null], Orders [orderId=2, orderSum=200.0, orderTime=Sun Feb 09 09:09:00 CST 2014, personId=1, detialList=null, person=null]]
三、多对一
对于Orders来说
一条SQL语句查询Order是的基本信息
一条SQL语句查询Order的所属人(懒加载的语句)
1.Order的映射文件
<resultMap type="com.yutouxiuxiu.model.Orders" id="selectPersonByOrderIdLazyRM" extends="BaseResultMap">
<association property="person" column="person_id" select="com.yutouxiuxiu.Person.selectPersonByOrderId">
</association>
</resultMap>
<select id="selectPersonByOrderIdLazy" parameterType="int" resultMap="selectPersonByOrderIdLazyRM">
select * from orders where order_id = #{orderId}
</select>
2.Person的映射文件
<select id="selectPersonByOrderId">
select * from person where person_id = #{personId}
</select>
@Test
public void selectPersonByOrderIdLazy() {
// 获得session
SqlSession session = sqlSessionFactory.openSession();
try {
Orders orders = session.selectOne("com.yutouxiuxiu.Orders.selectPersonByOrderIdLazy", 1);
// 发出查询person信息的SQL语句
System.out.println(orders);
// 发出查询订单信息的SQL语句
System.out.println(orders.getPerson());
} finally {
session.close();
}
}