Mainly reflected in the related query

1. Turn on delayed loading

You need to make two necessary configurations in the core configuration file

<configuration>
    
    <settings>
        <!– general switch for lazy loading –>
        <setting name="lazyLoadingEnabled" value="true" />
        <!– disable aggressive loading –>
        <setting name="aggressiveLazyLoading" value="false " />
    </settings>

 

Two, one-to-many

for Person

An SQL statement to query the basic information of Person

An SQL statement to query Person's order (lazy loaded statement)

1. In PersonMapper

    <resultMap type="com.yutouxiuxiu.model.Person" id="selectOrderByPersonIdLazyRM" extends="personRM">
        <!– 
             column - a field in the result set of the main SQL statement query is passed as a parameter to the sub SQL statement
             select - sub SQL statement –>
        <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. In OrdersMapper

    <!– lazy loaded sub-SQL statement in one-to-many case–>
    <select id="selectOrderByPersonId" parameterType="int" resultMap="BaseResultMap">
        select * from orders o where o.person_id = #{personId}
    < /select>

 

    @Test
    public void selectOrderByPersonIdLazy() {
        // Get session
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Person person = session.selectOne("com.yutouxiuxiu.Person.selectOrderByPersonIdLazy", 1);
            // Send out query for person information SQL statement
            System.out.println(person);
            // Issue SQL statement to query order information
            System.out.println(person.getOrderList());
        } finally {
            session.close();
        }
    }

Results of the:

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]]

 

 

Three, many to one

For Orders

An SQL statement to query the basic information of the Order

An SQL statement to query the owner of an Order (lazy loaded statement)

1. Order mapping file

    <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's mapping file

    <select id="selectPersonByOrderId">
        select * from person where person_id = #{personId}        
    </select>

 

    @Test
    public void selectPersonByOrderIdLazy() {
        // Get session
        SqlSession session = sqlSessionFactory.openSession();
        try {
            Orders orders = session.selectOne("com.yutouxiuxiu.Orders.selectPersonByOrderIdLazy", 1);
            // Send out the person information query SQL statement
            System.out.println(orders);
            // Issue SQL statement to query order information
            System.out.println(orders.getPerson());
        } finally {
            session.close();
        }
    }