Mybatis查询mysql延时加载操作 (18)
Posted 东北大亨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis查询mysql延时加载操作 (18)相关的知识,希望对你有一定的参考价值。
Mybatis查询mysql延时加载操作。采用教师和课程一对一关系演示延时加载操作应用。
1、采用延时加载,需要在工程配置文件
工程主 mapper.xml 文件,mybatis-03.xml 中开启延迟加载配置,关闭立即加载设置
2、主mapper.xml 文件,既:teacherMapper.xml
2.1 teacherMapper.xml 文件
<!-- 延时加载 --> <select id="queryTeacherInfoWithResultMapByCourseNo2" parameterType="int" resultMap="teacher_course_lazy_load_map"> SELECT * FROM teacher </select> <resultMap id="teacher_course_lazy_load_map" type="com.mybatis03.bean.Teacher"> <!-- 老师信息 --> <id property="teaNo" column="teano"/> <result property="courseNo" column="courseno"/> <result property="teaName" column="teaname"/> <association property="teacherCourse" javaType="com.mybatis03.bean.TeacherCourse" select="com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID" column="courseno"> </association> </resultMap>
teacherMapper.xml配置文件说明
教师与课程一对一关系需要借助<association>标签
1)、采用延迟加载时 ,延迟子查询语句需要写在标签中(既:需要写在 <association>标签 中而不是标签体内),参照上图;
2)、通过 <select>中属性esultMap的value值 teacher_course_lazy_load_map 与 <resultmap>标签的id一一对应且在此配置中存在唯一性即可;
3)、<resultmap>标签 中的type为最终返回的结果集;
4)、<resultmap>标签 中包含teacher属性。property对应teacher实体类的属性,culumn对应数据库中的字段值;
5)、<association>标签 中select为引用了外标签查询,column为teacher实体类查询(主查询)与子查询(查询教师课程的 select标签)关联编号;
2.2 子查询xml文件,既:teacherMapper.xml 文件中子查询文件
<?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="com.mybatis03.mapper.TeacherCourseMapper"> <!-- 查询教师课程,延迟加载应用此配置文件 --> <!-- 查询教师对应的课程 --> <select id="queryTeacherCourseByID" parameterType="int" resultType="com.mybatis03.bean.TeacherCourse"> SELECT * FROM teachercourse WHERE courseno = #{courseno} </select> </mapper>
3、实体类
3.1 教师实体类
package com.mybatis03.bean; public class Teacher { @Override public String toString() { return "Teacher{" + "teaNo=" + teaNo + ", courseNo=" + courseNo + ", teaName=\'" + teaName + \'\\\'\' + ", teacherCourse{" + "courseNo=" + teacherCourse.getCourseNo() + ", courseName=" + teacherCourse.getCourseName() +"}"+ \'}\'; } /* 教师编号 */ private int teaNo; /* 课程编号 */ private int courseNo; /* 教师名称 */ private String teaName; /* 教师课程类,课程作为老师属性 */ private TeacherCourse teacherCourse; /* 新增一个教研组编号,用于关联教研组编号,实现多对一关联*/ private int groupNo; /** * <!--利用resultMap实现一对一 --> * @return */ public TeacherCourse getTeacherCourse() { return teacherCourse; } public void setTeacherCourse(TeacherCourse teacherCourse) { this.teacherCourse = teacherCourse; } public int getGroupNo() { return groupNo; } public void setGroupNo(int groupNo) { this.groupNo = groupNo; } public int getCourseNo() { return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public int getTeaNo() { return teaNo; } public void setTeaNo(int teaNo) { this.teaNo = teaNo; } public String getTeaName() { return teaName; } public void setTeaName(String teaName) { this.teaName = teaName; } }
3.2 教师对应课程实体类
public class TeacherCourse { /* 课程编号 */ private int courseNo; /* 课程名称 */ private String courseName; public int getCourseNo() { return courseNo; } public void setCourseNo(int courseNo) { this.courseNo = courseNo; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } @Override public String toString() { return "TeacherCourse{" + "courseNo=" + courseNo + ", courseName=\'" + courseName + \'\\\'\' + \'}\'; } }
4、查询接口类
public interface TeacherMapper { /* 查询全部学生然后做延迟加载 */ List<Teacher> queryTeacherInfoWithResultMapByCourseNo2(); }
5、测试类
/* 查询全部学生然后做延迟加载 */ @Test public void queryTeacherInfoWithResultMapByCourseNo2() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis-03.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); TeacherMapper personMapper = session.getMapper(TeacherMapper.class); List<Teacher> teacherList = personMapper.queryTeacherInfoWithResultMapByCourseNo2(); for (Teacher teacher : teacherList){ System.out.println("教师编号:"+teacher.getTeaNo()+",教师名称:"+teacher.getTeaName()+","+ "教师课程编号:"+teacher.getCourseNo()); // 获得教师课程信息 TeacherCourse teacherCourse = teacher.getTeacherCourse(); System.out.println("教师对应课程信息:["+"课程编号="+teacherCourse.getCourseNo()+",课程名称"+teacherCourse.getCourseName()+"]"); } //System.out.println("查询全部学生然后做延迟加载,查询结果为:" + teacherList); }
6、数据库数据
6.1 教师数据库表信息
6.2 教师对应课程数据库结构和对应数据
7、测试结果
[lsjSso]2021-10-19 22:57:30,277-org.apache.ibatis.transaction.jdbc.JdbcTransaction-9653[main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection [lsjSso]2021-10-19 22:57:30,804-org.apache.ibatis.datasource.pooled.PooledDataSource-10180[main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 1443967876. [lsjSso]2021-10-19 22:57:30,804-org.apache.ibatis.transaction.jdbc.JdbcTransaction-10180[main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@56113384] [lsjSso]2021-10-19 22:57:30,811-com.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-10187[main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-==> Preparing: SELECT * FROM teacher [lsjSso]2021-10-19 22:57:31,026-com.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-10402[main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-==> Parameters: [lsjSso]2021-10-19 22:57:31,302-com.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-10678[main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryTeacherInfoWithResultMapByCourseNo2-<== Total: 4 [lsjSso]2021-10-19 22:57:50,467-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-29843[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Preparing: SELECT * FROM teachercourse WHERE courseno = ? [lsjSso]2021-10-19 22:57:50,468-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-29844[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Parameters: 7001(Integer) [lsjSso]2021-10-19 22:57:50,468-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-29844[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-<== Total: 1 教师编号:1001,教师名称:sara,教师课程编号:7001 教师对应课程信息:[课程编号=7001,课程名称chinese] [lsjSso]2021-10-19 22:58:42,576-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-81952[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Preparing: SELECT * FROM teachercourse WHERE courseno = ? [lsjSso]2021-10-19 22:58:42,576-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-81952[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Parameters: 7002(Integer) [lsjSso]2021-10-19 22:58:42,577-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-81953[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-<== Total: 1 教师编号:1002,教师名称:sakula,教师课程编号:7002 教师对应课程信息:[课程编号=7002,课程名称english] [lsjSso]2021-10-19 22:58:56,555-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-95931[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Preparing: SELECT * FROM teachercourse WHERE courseno = ? [lsjSso]2021-10-19 22:58:56,555-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-95931[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Parameters: 7003(Integer) [lsjSso]2021-10-19 22:58:56,556-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-95932[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-<== Total: 1 教师编号:1003,教师名称:peter,教师课程编号:7003 教师对应课程信息:[课程编号=7003,课程名称japanese] [lsjSso]2021-10-19 22:59:05,300-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-104676[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Preparing: SELECT * FROM teachercourse WHERE courseno = ? [lsjSso]2021-10-19 22:59:05,300-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-104676[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-==> Parameters: 7004(Integer) [lsjSso]2021-10-19 22:59:05,301-com.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-104677[main]DEBUGcom.mybatis03.mapper.TeacherCourseMapper.queryTeacherCourseByID-<== Total: 1 教师编号:1004,教师名称:jack,教师课程编号:7004 教师对应课程信息:[课程编号=7004,课程名称German] Disconnected from the target VM, address: \'127.0.0.1:31862\', transport: \'socket\' Process finished with exit code 0
Mybatis 中延时加载
1 为了处理N+1 问题,Mybatis 引入了延时加载功能,意义是一开始并不取出关联数据,只有当使用时,才发送sql语句去取。
mybatis中两个全局设置 lazyLoadingEnabled 和 aggressiveLazyLoading 是否开启延迟加载功能。
mybatis 中的默认情况下是及时加载的,一旦关联比较多,会造成性能问题。
1.1)如果设置 lazyLoadingEnabled = true,默认情况下mybatis 是按层级延时加载的。
1.2)如果在设置了1.1 后,使用 aggressiveLazyLoading 可以按需加载。
1.2.1) aggressiveLazyLoading = true,mybatis 是按层级延时加载
1.2.2) aggressiveLazyLoading = false,mybatis 按需求加载。
1.2 有时全局配置不是那么的适合自己需求,mybatis 提供了局部延时加载功能。
在 collection 或 association 元素上加入属性值 fetchType 就可以了,他有两个取值 eager 和 lazy。
以上是关于Mybatis查询mysql延时加载操作 (18)的主要内容,如果未能解决你的问题,请参考以下文章
java:Mybatis框架3(二级缓存,延时和积极加载,SSI(Ibatis)集成,SSM集成)
JavaLearn#(28)MyBatis高级:无级联查询级联查询(立即加载结果映射延迟加载)多表连接查询MyBatis注解MyBatis运行原理面试题