MyBatis源码解读之延迟加载
1. 目的
本文主要解读MyBatis 延迟加载实现原理
2. 延迟加载如何使用
Setting 参数配置
设置参数 | 描述 | 有效值 | 默认值 |
---|---|---|---|
lazyLoadingEnabled | 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置fetchType属性来覆盖该项的开关状态。 | true、false | false |
aggressiveLazyLoading | 当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载(参考lazyLoadTriggerMethods). | true、false | false (true in ≤3.4.1) |
lazyLoadTriggerMethods | 指定哪个对象的方法触发一次延迟加载。 | 用逗号分隔的方法列表。 | equals,clone,hashCode,toString |
配置
<code class="language-xml"><configuration>
<settings>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
</configuration>
Mapper 配置
<code class="language-xml"><mapper namespace="org.apache.ibatis.submitted.lazy_properties.Mapper">
<resultMap type="org.apache.ibatis.submitted.lazy_properties.User"
id="user">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>
<!-- 结果对象 -->
<resultMap type="org.apache.ibatis.submitted.lazy_properties.User" id="userWithLazyProperties" extends="user">
<!-- 延迟加载对象lazy1 -->
<association property="lazy1" column="id" select="getLazy1" fetchType="lazy" />
<!-- 延迟加载对象lazy2 -->
<association property="lazy2" column="id" select="getLazy2" fetchType="lazy" />
<!-- 延迟加载集合lazy3 -->
<collection property="lazy3" column="id" select="getLazy3" fetchType="lazy" />
</resultMap>
<!-- 执行的查询 -->
<select id="getUser" resultMap="userWithLazyProperties">
select * from users where id = #{id}
</select>
</mapper>
User 实体对象
<code class="language-Java">public class User implements Cloneable {
private Integer id;
private String name;
private User lazy1;
private User lazy2;
private List<User> lazy3;
public int setterCounter;
省略...
}
执行解析:
- 调用getUser查询数据,从查询结果集解析数据到User对象,当数据解析到lazy1,lazy2,lazy3判断需要执行关联查询
- lazyLoadingEnabled=true,将创建lazy1,lazy2,lazy3对应的Proxy延迟执行对象lazyLoader,并保存
- 当逻辑触发lazyLoadTriggerMethods 对应的方法(equals,clone,hashCode,toString)则执行延迟加载
- 如果aggressiveLazyLoading=true,只要触发到对象任何的方法,就会立即加载所有属性的加载
3. 延迟加载原理实现
延迟加载主要是通过动态代理的形式实现,通过代理拦截到指定方法,执行数据加载。
MyBatis延迟加载主要使用:Javassist,Cglib实现,类图展示:
4. 延迟加载源码解析
Setting 配置加载:
<code class="language-Java">public class Configuration {
/** aggressiveLazyLoading:
* 当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载(参考lazyLoadTriggerMethods).
* 默认为true
* */
protected boolean aggressiveLazyLoading;
/**
* 延迟加载触发方法
*/
protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" }));
/** 是否开启延迟加载 */
protected boolean lazyLoadingEnabled = false;
/**
* 默认使用Javassist代理工厂
* @param proxyFactory
*/
public void setProxyFactory(ProxyFactory proxyFactory) {
if (proxyFactory == null) {
proxyFactory = new JavassistProxyFactory();
}
this.proxyFactory = proxyFactory;
}
//省略...
}
延迟加载代理对象创建
DefaultResultSetHandler
<code class="language-Java">//#mark 创建结果对象
private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
this.useConstructorMappings = false; // reset previous mapping result
final List<Class<?>> constructorArgTypes = new ArrayList<Class<?>>();
final List<Object> constructorArgs = new ArrayList<Object>();
//#mark 创建返回的结果映射的真实对象
Object resultObject = createResultObject(rsw, resultMap, constructorArgTypes, constructorArgs, columnPrefix);
if (resultObject != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
for (ResultMapping propertyMapping : propertyMappings) {
// issue gcode #109 && issue #149 判断属性有没配置嵌套查询,如果有就创建代理对象
if (propertyMapping.getNestedQueryId() != null && propertyMapping.isLazy()) {
//#mark 创建延迟加载代理对象
resultObject = configuration.getProxyFactory().createProxy(resultObject, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
break;
}
}
}
this.useConstructorMappings = resultObject != null && !constructorArgTypes.isEmpty(); // set current mapping result
return resultObject;
}
代理功能实现
由于Javasisst和Cglib的代理实现基本相同,这里主要介绍Javasisst
ProxyFactory接口定义
<code class="language-Java">public interface ProxyFactory {
void setProperties(Properties properties);
/**
* 创建代理
* @param target 目标结果对象
* @param lazyLoader 延迟加载对象
* @param configuration 配置
* @param objectFactory 对象工厂
* @param constructorArgTypes 构造参数类型
* @param constructorArgs 构造参数值
* @return
*/
Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
}
JavasisstProxyFactory实现
<code class="language-Java">public class JavassistProxyFactory implements org.apache.ibatis.executor.loader.ProxyFactory {
/**
* 接口实现
* @param target 目标结果对象
* @param lazyLoader 延迟加载对象
* @param configuration 配置
* @param objectFactory 对象工厂
* @param constructorArgTypes 构造参数类型
* @param constructorArgs 构造参数值
* @return
*/
@Override
public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
}
//省略...
/**
* 代理对象实现,核心逻辑执行
*/
private static class EnhancedResultObjectProxyImpl implements MethodHandler {
/**
* 创建代理对象
* @param type
* @param callback
* @param constructorArgTypes
* @param constructorArgs
* @return
*/
static Object crateProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
ProxyFactory enhancer = new ProxyFactory();
enhancer.setSuperclass(type);
try {
//通过获取对象方法,判断是否存在该方法
type.getDeclaredMethod(WRITE_REPLACE_METHOD);
// ObjectOutputStream will call writeReplace of objects returned by writeReplace
if (log.isDebugEnabled()) {
log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
}
} catch (NoSuchMethodException e) {
//没找到该方法,实现接口
enhancer.setInterfaces(new Class[]{WriteReplaceInterface.class});
} catch (SecurityException e) {
// nothing to do here
}
Object enhanced;
Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
try {
//创建新的代理对象
enhanced = enhancer.create(typesArray, valuesArray);
} catch (Exception e) {
throw new ExecutorException("Error creating lazy proxy. Cause: " + e, e);
}
//设置代理执行器
((Proxy) enhanced).setHandler(callback);
return enhanced;
}
/**
* 代理对象执行
* @param enhanced 原对象
* @param method 原对象方法
* @param methodProxy 代理方法
* @param args 方法参数
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
final String methodName = method.getName();
try {
synchronized (lazyLoader) {
if (WRITE_REPLACE_METHOD.equals(methodName)) {
//忽略暂未找到具体作用
Object original;
if (constructorArgTypes.isEmpty()) {
original = objectFactory.create(type);
} else {
original = objectFactory.create(type, constructorArgTypes, constructorArgs);
}
PropertyCopier.copyBeanProperties(type, enhanced, original);
if (lazyLoader.size() > 0) {
return new JavassistSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
} else {
return original;
}
} else {
//延迟加载数量大于0
if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
//aggressive 一次加载性所有需要要延迟加载属性或者包含触发延迟加载方法
if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
log.debug("==> laze lod trigger method:" + methodName + ",proxy method:" + methodProxy.getName() + " class:" + enhanced.getClass());
//一次全部加载
lazyLoader.loadAll();
} else if (PropertyNamer.isSetter(methodName)) {
//判断是否为set方法,set方法不需要延迟加载
final String property = PropertyNamer.methodToProperty(methodName);
lazyLoader.remove(property);
} else if (PropertyNamer.isGetter(methodName)) {
final String property = PropertyNamer.methodToProperty(methodName);
if (lazyLoader.hasLoader(property)) {
//延迟加载单个属性
lazyLoader.load(property);
log.debug("load one :" + methodName);
}
}
}
}
}
return methodProxy.invoke(enhanced, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
}
5. 注意事项
- IDEA调试问题
当配置aggressiveLazyLoading=true,在使用IDEA进行调试的时候,如果断点打到代理执行逻辑当中,你会发现延迟加载的代码永远都不能进入,总是会被提前执行。
主要产生的原因在aggressiveLazyLoading,因为在调试的时候,IDEA的Debuger窗体中已经触发了延迟加载对象的方法。
如图:调试还未进入lazyLoader.loadAll(); 实际日志已经显示延迟加载已经完成,代码与日志通过颜色区分。
6. 参考资料
本博客MyBatis源码地址: - https://gitee.com/rainwen/mybatis
Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM) - http://blog.csdn.net/luanlouis/article/details/24589193
从类加载到动态编译 - http://zhaoyw.cn/2017/07/classloader-dynamic
MyBatis 记录二: lazy loading - http://yoncise.com/2016/11/05/MyBatis-%E8%AE%B0%E5%BD%95%E4%BA%8C-lazy-loading/
MyBatis官方文档 - http://www.mybatis.org/mybatis-3/zh/index.html
MyBatis-Spring官方文档 - http://www.mybatis.org/spring/zh/index.html
MyBatis-Spring源码 - https://github.com/rainwen/spring
关于MyBatis源码解读之延迟加载就介绍到这里。如有疑问,欢迎留言,谢谢。
原文链接:https://my.oschina.net/wenjinglian/blog/1857581
MyBatis源码解读之延迟加载的更多相关文章
- MyBatis源码解读(3)——MapperMethod
在前面两篇的MyBatis源码解读中,我们一路跟踪到了MapperProxy,知道了尽管是使用了动态代理技术使得我们能直接使用接口方法.为巩固加深动态代理,我们不妨再来回忆一遍何为动态代理. 我相信在 ...
- spring IOC DI AOP MVC 事务, mybatis 源码解读
demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...
- Mybatis源码解读-SpringBoot中配置加载和Mapper的生成
本文mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载. 建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此 ...
- Mybatis源码解读-插件
插件允许对Mybatis的四大对象(Executor.ParameterHandler.ResultSetHandler.StatementHandler)进行拦截 问题 Mybatis插件的注册顺序 ...
- 【转】Mybatis源码解读-设计模式总结
原文:http://www.crazyant.net/2022.html?jqbmtw=b90da1&gsjulo=kpzaa1 虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开 ...
- Mybatis源码解读-设计模式总结
虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开发中很少遇到,Mybatis源码中使用了大量的设计模式,阅读源码并观察设计模式在其中的应用,能够更深入的理解设计模式. Mybatis至少 ...
- MyBatis源码解读(1)——SqlSessionFactory
在前面对MyBatis稍微有点了解过后,现在来对MyBatis的源码试着解读一下,并不是解析,暂时定为解读.所有对MyBatis解读均是基于MyBatis-3.4.1,官网中文文档:http://ww ...
- MyBatis源码解读(4)——SqlSession(上)
在上一篇博客中提到MyBatis是如何实现代理类MapperProxy,并抛出了一个问题--是怎么执行一个具体的sql语句的,在文末中提到了MapperMethod的execute采用命令模式来判断是 ...
- mybatis源码解读(一)——初始化环境
本系列博客将对mybatis的源码进行解读,关于mybatis的使用教程,可以查看我前面写的博客——传送门. 为了便于后面的讲解,我们这里首先构造一个统一环境.也可以参考mybatis官网. 1.数据 ...
随机推荐
- python3----模块(序列化(json&pickle)+XML+requests)
一.序列化模块 Python中用于序列化的两个模块: json 跨平台跨语言的数据传输格式,用于[字符串]和 [python基本数据类型] 间进行转换 pickle python内置的数据 ...
- hdu4328(经典dp用悬线法求最大子矩形)
http://wenku.baidu.com/view/728cd5126edb6f1aff001fbb.html 关于悬线法,这里面有详解. 我当时只想到了记录最大长度,却没有想到如果连最左边和最右 ...
- JZOJ.5235【NOIP2017模拟8.7】好的排列
Description 对于一个1->n的排列 ,定义A中的一个位置i是好的,当且仅当Ai-1>Ai 或者Ai+1>Ai.对于一个排列A,假如有不少于k个位置是好的,那么称A是一个好 ...
- ORA-00257错误的解决办法
author: headsen chen date: 2018-04-17 11:12:39 notice:个人原创,转载请注明作者和出处,否则依法追击法律责任. 1,oracle数据库正常使用中 ...
- 【BZOJ2653】middle 二分+可持久化线段树
[BZOJ2653]middle Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个 长度为n的序列s.回答Q个 ...
- 【BZOJ2836】魔法树 树链剖分
[BZOJ2836]魔法树 Description Input Output Sample Input 4 0 1 1 2 2 3 4 Add 1 3 1 Query 0 Query 1 Query ...
- activeMQ安装与测试
Apache ActiveMQ简介 activeMQ是JMS的一种具体的实现,是最流行的,能力强劲的开源消息总线.activeMQ具有以下优势: 多种语言和协议编写客户端(java.C.C++.AJA ...
- PHP错误处理,无法显示验证码。。无法显示首页等莫名其妙的500
use the date.timezone setting or the date_default_timezone_set() 这是由于调用date时,若timezone设置不正确所产生的E_NOT ...
- git 学习(一)初始化和提交
git 学习(一) 创建git版本库 $ mkdir gitstudy $ cd gitstudy $ git init nitialized empty Git repository in /Use ...
- 微信支付 统一订单 $order = WxPayApi::unifiedOrder($input); 断点调试
定位至 CODE /** * 将xml转为array * @param string $xml * @throws WxPayException */ public static function I ...