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

  • 相关阅读:
    PHP后门新玩法:一款猥琐的PHP后门分析
    中国菜刀批量导出数据
    渗透测试:反弹与转发小结
    怎样用Java自制优秀的图片验证码?这样!
    6条 Tips 为你照亮 GitHub 寻宝之路
    如何搭建一个“不可告人的”私人专属网盘?教程拿去
    6个炫酷又好用的 Python 工具,个个都很奔放呀
    20个Java练手项目,献给嗜学如狂的人
    如何自己动手写一个搜索引擎?我是一份害羞的教程🙈
    GitHub上个最有意思的项目合集(技术清单系列)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309403.html
Copyright © 2011-2022 走看看