zoukankan      html  css  js  c++  java
  • MyBatis 延迟加载怎么实现?

    一.Mybatis 极简入门

    二.Mybatis 级联查询

    本篇:Mybatis 延迟加载

    现在我们有如下需求:有时候只需要查询学生姓名,不需要查询学生班级,有时候不仅需要查询姓名还要查询班级。怎么才能实现这个功能呢?Mybatis为我们提供了延迟加载功能。

    上一篇级联查询中,我们根据学生id查询出学生姓名和班级,同时查询了两张表,我们要重写一个延迟加载。

    第一步:在config.xml中开启打印SQL语句和懒加载。

    <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
            <setting name="lazyLoadingEnabled" value="true"/>
     </settings>
    

    第二步:添加查询方法如下

    package com.ibuyi.mybatis.repository;
    
    import com.ibuyi.mybatis.entity.Student;
    
    public interface StudentDAO {
        //根据student ID查询学生姓名班级
        Student findStudentByID(long id);
        //懒加载添加的方法
        Student findStudentByIDLazy(long id);
    }
    
    

    紧接着,在mapper文件中

    <resultMap id="studentLazy" type="com.ibuyi.mybatis.entity.Student">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <!--这里进行修改,不是以前的和查询结果一一对应,而是调用ClassDAO中的查询方法-->
            <association property="classes" javaType="com.ibuyi.mybatis.entity.Classes" select="com.ibuyi.mybatis.repository.ClassDAO.findClassedByID" column="cid"></association>
        </resultMap>
        <select id="findStudentByIDLazy" parameterType="long" resultMap="studentLazy">
            select * from student where id=#{id}
        </select>
    

    在ClassDAO中也要声明一个方法:

    package com.ibuyi.mybatis.repository;
    import com.ibuyi.mybatis.entity.Classes;
    import java.util.List;
    public interface ClassDAO {
        //根据cid查询班级全部学生
        List<Classes> findStudentByID(long id);
        Classes findClassedByID(long id);
    }
    
    
    <select id="findClassedByID" parameterType="long" resultType="com.ibuyi.mybatis.entity.Classes">
       select * from classes where id=#{id}
    </select>
    

    测试代码:

    package com.ibuyi.mybatis.test;
    import com.ibuyi.mybatis.entity.Student;
    import com.ibuyi.mybatis.repository.StudentDAO;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.InputStream;
    
    public class Test {
        public static void main(String[] args) {
            InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
            Student student=studentDAO.findStudentByIDLazy(2L);
            //System.out.println(student.getName());
            System.out.println(student.getClasses());
        }
    }
    
    

    在这里插入图片描述

    public class Test {
        public static void main(String[] args) {
            InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
            Student student=studentDAO.findStudentByIDLazy(2L);
            System.out.println(student.getName());
            //System.out.println(student.getClasses());
        }
    }
    
    

    在这里插入图片描述

    根据结果我们可以看出,只有在需要用到班级信息的时候,mybatis才会帮我们执行第二条sql语句,不需要用到就不会执行。

  • 相关阅读:
    Hdu 5396 Expression (区间Dp)
    Lightoj 1174
    codeforces 570 D. Tree Requests (dfs)
    codeforces 570 E. Pig and Palindromes (DP)
    Hdu 5385 The path
    Hdu 5384 Danganronpa (AC自动机模板)
    Hdu 5372 Segment Game (树状数组)
    Hdu 5379 Mahjong tree (dfs + 组合数)
    Hdu 5371 Hotaru's problem (manacher+枚举)
    Face The Right Way---hdu3276(开关问题)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309403.html
Copyright © 2011-2022 走看看