主要体现在关联查询上

一、开启延时加载

需要在核心配置文件中做两个必须的配置


    
    
        
        
        
        
    

 

二、一对多

对于Person来说

一条SQL语句查询Person的基本信息

一条SQL语句查询Person的订单(懒加载的语句)

1.在PersonMapper中

    
                     column  -  主SQL语句查询的结果集的某一字段作为参数传给子SQL语句
             select  -  子SQL语句 –>
        
        

    

    
    

2.在OrdersMapper中

    
    

 

    @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的映射文件

    
        
        

    

    
    

2.Person的映射文件

   

 

    @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();
        }
    }