• 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

  • 相关阅读:
    常用的正则表达式
    VScode格式化代码,开启ESlint代码检测方法,eslint代码配置
    git 常用的操作
    vscode 格式化的时候自动添加分号怎么去除
    echarts渐变色实现方法
    echart 柱状图 数值较小的时候,文字显示不全
    iview的Modal在提交表单时确认按钮loading状态冲突问题解决方案
    vue.js动态获取菜单
    12.18 webSocket消息推送
    12.18 微信模板消息推送
  • 原文地址:https://www.cnblogs.com/loveer/p/11582735.html
Copyright © 2020-2023  润新知