zoukankan      html  css  js  c++  java
  • mybatis源码1.3 MapperMethod

    先看一下MapperMethod类,SqlCommandMethodSignature都是MapperMethod的静态内部类

    public class MapperMethod {
        private final MapperMethod.SqlCommand command;
        private final MapperMethod.MethodSignature method;
    
        public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
          //SqlCommand中封装了sql的类型,insert,update.... MethodSignature封装了方法的参数和返回值信息
    this.command = new MapperMethod.SqlCommand(config, mapperInterface, method); this.method = new MapperMethod.MethodSignature(config, mapperInterface, method); } }

    回到MapperProxy中的invoke方法

        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);
            }
         也就是判断MapperPorxy中的methodCache中是否又该值,MapperProxymethodCache是一个Map<Mehtod,MapperMethod>
        cachedMapperMethod判断当前方法是否已经执行过了,如果map不能找到该元素,则说明没有执行过,会创建一个MapperMethod,并添加到methodCache中
            MapperMethod mapperMethod = this.cachedMapperMethod(method);
            return mapperMethod.execute(this.sqlSession, args);
        }
        private MapperMethod cachedMapperMethod(Method method) {
            return (MapperMethod)this.methodCache.computeIfAbsent(method, (k) -> {
                return new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
            });
        }

    接下来进入  mapperMethod.execute(this.sqlSession, args)方法

        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;
            case DELETE:
                param = this.method.convertArgsToSqlCommandParam(args);
                result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
                break;
            case SELECT:
                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 if (this.method.returnsCursor()) {
                    result = this.executeForCursor(sqlSession, args);
                } else {
                    param = this.method.convertArgsToSqlCommandParam(args);
                    result = sqlSession.selectOne(this.command.getName(), param);
                    if (this.method.returnsOptional() && (result == null || !this.method.getReturnType().equals(result.getClass()))) {
                        result = Optional.ofNullable(result);
                    }
                }
                break;
            case FLUSH:
                result = sqlSession.flushStatements();
                break;
            default:
                throw new BindingException("Unknown execution method for: " + this.command.getName());
            }
    
            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;
            }
        }
  • 相关阅读:
    docker容器的应用
    KVM虚拟机迁移
    centos6.5虚拟机快照技术
    centos6.5网络虚拟化技术
    centos6.5制作OpenStack云平台Windows7镜像
    centos6.5远程桌面连接(VNCSPice)
    centos6.5kvm虚拟化技术
    centos7安装Jenkins及其卸载(yum和rpm安装)
    CentOS 7安装JDK
    [leetcode]Reverse Nodes in k-Group
  • 原文地址:https://www.cnblogs.com/a-small-lyf/p/12713131.html
Copyright © 2011-2022 走看看