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语句,不需要用到就不会执行。

  • 相关阅读:
    [转]ExtJS3.0与KindEditor4.1.2整合
    [转]Ext.grid常用属性和方法
    Extjs gridpanel 合并单元格
    [转]Httrack工具与使用指南
    [转]Teleport Ultra/Teleport Pro的冗余代码批量清理方法
    [转]最全的用正则批量去除Teleport Pro整站下载文件冗余代码
    Teleport Pro使用教程
    如何通过Dreamweaver批量对整个站点或目录进行代码搜索或部分全部替换
    [转]使用DW正则表达式批量替换实例介绍
    [转]tppabs是什么?如何去除tppabs?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309403.html
Copyright © 2011-2022 走看看