ResultMapcollection的嵌套结果

    

    在OrdersExt类中添加以下属性,并提供get/set方法:

// 订单明细
private List detailList;

    

                   编写mapper接口
//查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套结果)
public List findOrdersResultMapOfCollection();

    

               编写映射文件

    
    
        
        
        
            
            
            
        
    

    
    
        Select
            ,
            ,
            orderdetail.id detail_id,
            orderdetail.items_id,
            orderdetail.items_num
        from orders,user,orderdetail
        where orders.user_id = user.id
        and
        orders.id = orderdetail.orders_id
    

        

                  编写测试代码
/**
     * 进行订单信息查询,包括用户的名称和地址信息(resultMap之嵌套结果)
     */
    @Test
    public void testFindOrdersResultMapOfCollection() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        
        // 测试
        List list = ordersMapper.findOrdersResultMapOfCollection();
        System.out.println(list);
        
        sqlSession.close();
    }

        


      resultMapcollection的嵌套查询

            

            编写mapper接口
// 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套查询)
public List findOrdersResultMapOfCollectionSelect();

            

            编写映射文件

    
    
        
        
        
    
    
    
        select * from orderdetail where id = #{id}
    
    
    
    
    
        SELECT
            ,
            
        FROM orders, user
        WHERE 
            orders.user_id = user.id
    

            

         编写测试代码
     /**
     * 查询订单信息,包括用户名称、用户地址,订单商品信息(嵌套查询)
     */
    @Test
    public void testFindOrdersResultMapOfCollectionSelect() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        
        // 测试
        List list = ordersMapper.findOrdersResultMapOfCollectionSelect();
        System.out.println(list);
        
        sqlSession.close();
    }