zoukankan      html  css  js  c++  java
  • Mybatis Mapper之见解

      以下个人见解,如有错误,请纠正。

    1.注册Mapper接口

    2.初始化Mapper

      当进行依赖注入的时候,需要初始化Mapper接口,而这里使用Mapper的是代理类。

    3.获取Mapper类型的bean实例

    4.执行mapper方法

        public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
            this.sqlSession = sqlSession;
            this.mapperInterface = mapperInterface;
            this.methodCache = methodCache;
        }
    
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(Object.class.equals(method.getDeclaringClass())) {
                try {
                    return method.invoke(this, args);
                } catch (Throwable var5) {
                    throw ExceptionUtil.unwrapThrowable(var5);
                }
            } else {
                MapperMethod mapperMethod = this.cachedMapperMethod(method);
                return mapperMethod.execute(this.sqlSession, args);
            }
        }
    
        private MapperMethod cachedMapperMethod(Method method) {
            MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
            if(mapperMethod == null) {
                mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
                this.methodCache.put(method, mapperMethod);
            }
    
            return mapperMethod;
        }

    这里说下Object.class.equals(method.getDeclaringClass())

      如果此方法是Object类声明的,则直接执行

      否则(如Mapper接口声明的),则使用MapperMethod进行执行

    而最终执行还是由SqlSession执行SQL的操作

    public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
            this.command = new MapperMethod.SqlCommand(config, mapperInterface, method);
            this.method = new MapperMethod.MethodSignature(config, method);
        }
      
        public Object execute(SqlSession sqlSession, Object[] args) {
            Object param;
            Object result;
            if(SqlCommandType.INSERT == this.command.getType()) {
                param = this.method.convertArgsToSqlCommandParam(args);
                result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
            } else if(SqlCommandType.UPDATE == this.command.getType()) {
                param = this.method.convertArgsToSqlCommandParam(args);
                result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
            } else if(SqlCommandType.DELETE == this.command.getType()) {
                param = this.method.convertArgsToSqlCommandParam(args);
                result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
            } else if(SqlCommandType.SELECT == this.command.getType()) {
                if(this.method.returnsVoid() && this.method.hasResultHandler()) {
                    this.executeWithResultHandler(sqlSession, args);
                    result = null;
                } else if(this.method.returnsMany()) {
                    result = this.executeForMany(sqlSession, args);
                } else if(this.method.returnsMap()) {
                    result = this.executeForMap(sqlSession, args);
                } else {
                    param = this.method.convertArgsToSqlCommandParam(args);
                    result = sqlSession.selectOne(this.command.getName(), param);
                }
            } else {
                if(SqlCommandType.FLUSH != this.command.getType()) {
                    throw new BindingException("Unknown execution method for: " + this.command.getName());
                }
    
                result = sqlSession.flushStatements();
            }
    
            if(result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
                throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
            } else {
                return result;
            }
        }
  • 相关阅读:
    HashMap 的实现原理(1.8)
    HashMap 的实现原理(1.7)
    Java面试总结 -2018(补录)
    在java中写出完美的单例模式
    ArrayList实现原理分析
    Ngigx+Tomcat配置动静分离,负载均衡
    微信小程序——常用快捷键【四】
    Linux服务器下安装vmware虚拟机
    微信小程序——部署云函数【三】
    微信小程序——安装开发工具和环境【二】
  • 原文地址:https://www.cnblogs.com/zgz21/p/7845045.html
Copyright © 2011-2022 走看看