• Mybatis 工作原理


    MyBatis实现原理

    Mybatis底层还是采用原生jdbc来对数据库进行操作的,
    通过SqlSessionFactory,SqlSession,Executor,StatementHandler,
        ParameterHandler,ResultHandler,TypeHandler(类型转换)等几个处理器封装了这些过程
    
    
    执行器:
    	Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
    
    sql查询处理器:
    	StatementHandler
    
    参数处理器: 
    	ParameterHandler
    
    结果处理器 :
    	ResultSetHandler
    
    
    StatementHandler用通过ParameterHandler与ResultHandler分别进行参数预编译与结果处理。
    

    创建SqlSessionFacotry的过程

    创建SqlSession的过程

    创建Mapper的过程

    执行CRUD过程

    public class DefaultSqlSession implements SqlSession {
        public <T> T getMapper(Class<T> type) {
            return this.configuration.getMapper(type, this);
        }
    }
    
    public class Configuration {
        public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
            return this.mapperRegistry.getMapper(type, sqlSession);
        }
    }
    
    public class MapperRegistry {
        private final Configuration config;
        private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap();
    
            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 {
                    return mapperProxyFactory.newInstance(sqlSession);
                } catch (Exception var5) {
                    throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
                }
            }
        }
    }
    
    public class MapperProxyFactory<T> {
        protected T newInstance(MapperProxy<T> mapperProxy) {
            return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
        }
    
        public T newInstance(SqlSession sqlSession) {
            MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
            return this.newInstance(mapperProxy);
        }
    }
    
    public class MapperProxy<T> implements InvocationHandler, Serializable {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                if (Object.class.equals(method.getDeclaringClass())) {
                    return method.invoke(this, args);
                }
    
                if (this.isDefaultMethod(method)) {
                    return this.invokeDefaultMethod(proxy, method, args);
                }
            } catch (Throwable var5) {
                throw ExceptionUtil.unwrapThrowable(var5);
            }
    
            MapperMethod mapperMethod = this.cachedMapperMethod(method);
            return mapperMethod.execute(this.sqlSession, args);
        }
    }
    
    public class MapperMethod {
        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;
            }
        }
    }
    

    参考

    https://blog.csdn.net/u014297148/article/details/78696096

  • 相关阅读:
    主机与虚拟机之间的网络连接
    QT下过多点的曲线绘制
    C++返回对象和返回引用
    STS MVC与MyBatis的结合
    STS中依赖项的设置
    STS中MyBatis的基本实现
    STS中不同包但相同类名引起的问题:A component required a bean of type 'javax.activation.DataSource' that could not be found
    STS中AOP的实现
    STS如何将一个文件夹设置缺省的创建路径(build path)
    ARB扩展与标准OpenGL的关系
  • 原文地址:https://www.cnblogs.com/loveer/p/11582735.html
Copyright © 2020-2023  润新知