mybatis高级映射——(延迟加载)

一.什么是延迟加载

延迟加载:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。

  • resultMap可以实现高级映射(使用association和collection实现一对一以及一对多映射)所以assocation. collection具备延迟加载功能.
使用association:中的select指定延迟加载去执行的statement的id

查询sql语句:

select students.*,
(select a_username from admin where students.s_studentid = admin.a_nameid) a_username,
(select a_trank from admin where students.s_studentid = admin.a_nameid ) a_trank
from students;

1.只查询订单信息
select * from students
在查询订单的statement中使用association去延迟加载(执行)下边的statement(关联查询)
2.关联查询用户信息:
通过上边查询到的订单信息中的id去关联用户ID去关联查询管理身份信息
使用mapper.xml中的findUserById
上面先去执行findStudentLoading 当需要去查询管理信息时 再去执行findUserById通过resultMap的定义将延迟加载执行配置起来


1.pojo代码:
AdminBean.java

package mybatis.ma.entity;

/**
 * Created by 马欢欢 on 2017/6/6.
 */
public class AdminBean {
    private int a_id;
    private String  a_nameid;
    private String  a_username;
    private String  a_password;
    private String  a_trank;

//省略getter and setter 方法

package mybatis.ma.entity;


/**
 * Created by 马欢欢 on 2017/6/6.
 */
public class StudentBean {
    private int s_id;
    private String  s_studentid;
    private String s_username;
    private String s_password;
    private String s_sex;
    private String s_class;
    private String s_rank;
    private  AdminBean adminBean;
//省略getter and setter 方法

2.接口:

package mybatis.ma.mapper;

import mybatis.ma.entity.StudentBean;

import java.util.List;

/**
 * Created by 马欢欢  on 17-7-1.
 */
public interface StudentEvaluateMapper {

    List findStudentLoading() throws Exception;
}

3.配置文件:




    
        
        
        
        
        
        
        

        
    


    


    

4.打开延迟加载开关,在SqlMapConfig.xml配置如下:

    
        
        
    

测试:

package mybatis.ma.mybatisTest;



import mybatis.ma.entity.AdminBean;
import mybatis.ma.entity.StudentBean;
import mybatis.ma.mapper.StudentEvaluateMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by 马欢欢  on 17-7-1.
 */
public class StudentEvaluateMapperText {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    //测试前执行
    public void setUp() throws IOException {
        //配置文件
        String resource = "mybatis-config.xml";
        //的到配置文件流
        InputStream inputStream =  Resources.getResourceAsStream(resource);
        //创建会话工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void findStudentLoading() throws Exception{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //StudentCustomMapper,mybatis自动生成mapper代理对象
        StudentEvaluateMapper studentEvaluateMapper = sqlSession.getMapper(StudentEvaluateMapper.class);
        List studentBeans = studentEvaluateMapper.findStudentLoading();
        //遍历上边循环列表
        for (StudentBean studentBean:studentBeans){
            AdminBean adminBean =studentBean.getAdminBean();
            System.out.println(adminBean);
        }

        sqlSession.close();

    }
}

mybatis高级映射——(延迟加载)_第1张图片

上一篇:mybatis高级映射——(一对多查询)

文集:mybatis框架学习

你可能感兴趣的:(mybatis高级映射——(延迟加载))