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

作者: Mr_欢先生 | 来源:发表于2017-07-02 19:50 被阅读186次

    一.什么是延迟加载

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

    • 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<StudentBean> findStudentLoading() throws Exception;
    }
    
    

    3.配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="mybatis.ma.mapper.StudentEvaluateMapper">
        <resultMap id="StudentLoading" type="mybatis.ma.entity.StudentBean">
            <id column="s_id" property="s_id"/>
            <result column="s_sex" property="s_sex"/>
            <result column="s_username" property="s_username"/>
            <result column="s_password" property="s_password"/>
            <result column="s_class" property="s_class"/>
            <!--实现对用户信息惊醒延迟加载
            select:制定延迟加载需要执行的statement的id
            column:用户信息中关联查询的列-->
            <association property="adminBean" javaType="mybatis.ma.entity.AdminBean" select="mybatis.ma.mapper.StudentEvaluateMapper.findUserById" column="s_id">
    
            </association>
        </resultMap>
    
    
        <select id="findStudentLoading" resultMap="StudentLoading">
            select *from students
        </select>
    
    
        <select id="findUserById" parameterType="int" resultType="mybatis.ma.entity.AdminBean">
            SELECT * FROM admin WHERE a_id =#{a_id}
        </select>
    </mapper>
    

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

        <settings>
            <setting name="lazyLoadingEnabled" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>
        </settings>
    

    测试:

    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<StudentBean> studentBeans = studentEvaluateMapper.findStudentLoading();
            //遍历上边循环列表
            for (StudentBean studentBean:studentBeans){
                AdminBean adminBean =studentBean.getAdminBean();
                System.out.println(adminBean);
            }
    
            sqlSession.close();
    
        }
    }
    
    

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

    文集:mybatis框架学习

    相关文章

      网友评论

        本文标题:mybatis高级映射——(延迟加载)

        本文链接:https://www.haomeiwen.com/subject/rxincxtx.html