美文网首页
MyBatis-Mapper动态代理

MyBatis-Mapper动态代理

作者: GIT提交不上 | 来源:发表于2020-03-20 18:14 被阅读0次

MyBatis官方教程
MyBatis二级缓存设计
Mybatis中Mapper动态代理的实现原理
制作Mybatis插件---针对Mybatis的四大对象

  调用SqlSession的getMapper方法 <==> MapperRegistry的getMapper方法(注册Mapper接口与获取生成代理类实例的工具类)。

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
        if (mapperProxyFactory == null) {
            throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
        } else {
            try {
                //获取MapperProxy对象
                return mapperProxyFactory.newInstance(sqlSession);
            } catch (Exception var5) {
                throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
            }
        }
    }

  MapperProxy类构造方法:

  //sqlSession & 接口 & 接口方法
   public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = methodCache;
    }

  MapperProxy类invoke方法:从缓存中获得执行方法对应的MapperMethod类实例;如果MapperMethod类实例不存在的情况,创建加入缓存并返回相关的实例。

   //接口代理对象所有的方法调用都会调用该方法
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            if (Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(this, args);
            }

            if (method.isDefault()) {
                return this.invokeDefaultMethod(proxy, method, args);
            }
        } catch (Throwable var5) {
            throw ExceptionUtil.unwrapThrowable(var5);
        }
        //缓存
        MapperMethod mapperMethod = this.cachedMapperMethod(method);
        //执行
        return mapperMethod.execute(this.sqlSession, args);
    }

  MapperMethod类构造方法:SqlCommand & MethodSignature(说明方法的一些信息)。

  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
        this.command = new MapperMethod.SqlCommand(config, mapperInterface, method);
        this.method = new MapperMethod.MethodSignature(config, mapperInterface, method);
    }

  SqlCommand来封装底层的增删改查操作,MappedStatement的resource对应xml路径,id对应接口名+方法名。

图1-1 MappedStatement结构.png

  MappedStatement创建:在SqlSessionFactory创建时,SqlSessionFactoryBuilder类build方法。MapperBuilderAssistant类的addMappedStatement方法。

图1-2 MappedStatement创建堆栈信息.png

  MapperMethod类execute方法部分源码:

public Object execute(SqlSession sqlSession, Object[] args) {
        Object result;
        Object param;
        switch(this.command.getType()) {
        case INSERT:
            param = this.method.convertArgsToSqlCommandParam(args);
            result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
            break;
        case UPDATE:
            param = this.method.convertArgsToSqlCommandParam(args);
            result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
            break;
图1-3 缓存.png 图1-4 查询流程.png

补充知识:四大对象 & 插件拦截 & 分页插件 & 批量执行 & 存储过程 & 自定义类型处理器(处理枚举)

相关文章

  • MyBatis-Mapper动态代理

    MyBatis官方教程MyBatis二级缓存设计Mybatis中Mapper动态代理的实现原理制作Mybatis插...

  • 面试系列~动态代理实现与原理

    动态代理有JDK动态代理, CGLIB动态代理, SpringAOP动态代理 一,JDK动态代理  jdk动态代理...

  • 编程常用的设计模式

    动态代理和静态代理 静态代理 动态代理 静态代理与动态代理的区别 JDK中的动态代理和CGLIB 实现动态代理的方...

  • Spring的AOP原理分析

    一 动态代理 动态代理分为JDK动态代理和CGLIB动态代理 jdk动态代理 被代理类(目标类)和代理类必须实现同...

  • 设计模式之代理模式

    代理分为静态代理和动态代理。 动态代理又包括基于JDK的动态代理、基于CGlib 的动态代理、基于Aspectj实...

  • Java高级主题(五)——动态代理

    代理可以分为静态代理、动态代理,动态代理又可以分为 jvm的动态代理 和 cglib的动态代理。像spring框架...

  • 动态代理

    动态代理分为两类:1、基于接口的动态代理; (JDK动态代理 )2、基于类的动态代理;(cglib动态代理)3、J...

  • 动态代理的两种方式

    静态代理就不说了,基本用到的都是动态代理。 Java中动态代理有JDK动态代理和CGLIB动态代理。 JDK代理的...

  • Java动态代理

    通过以下几种方式介绍动态代理 动态代理涉及到的类 动态代理用法 Proxy类解析 动态代理类解析 动态代理涉及到的...

  • Spring之代理模式

    九、代理模式 目录:静态代理、动态代理AOP的底层机制就是动态代理。代理模式分为静态代理和动态代理。接触aop之前...

网友评论

      本文标题:MyBatis-Mapper动态代理

      本文链接:https://www.haomeiwen.com/subject/jntoehtx.html