2mybatis-1条sql在mybatis的执行过程
2021/8/5 2:06:29
本文主要是介绍2mybatis-1条sql在mybatis的执行过程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
MapperProxy
Map<Method, MapperMethod> methodCache;
MapperMethod mapperMethod = cachedMapperMethod(method);
从methodCache获取mapper里的一个方法
return mapperMethod.execute(sqlSession, args);
MapperMethod
SqlCommand 封装了sql的类型,方法名
MethodSignature 方法的返回值、参数
Object execute(SqlSession sqlSession, Object[] args){...}
执行方法
Object param = method.convertArgsToSqlCommandParam(args);
解析方法参数
result = rowCountResult(sqlSession.insert(command.getName(), param));
解析方法结果
result = sqlSession.selectOne(command.getName(), param);
通过sqlSession执行方法
result = executeForMany(sqlSession, args);
执行List<结果>查询
result = sqlSession.
DefaultSqlSession
public
return this.selectList(statement, parameter, RowBounds.DEFAULT);
public
MappedStatement ms = configuration.getMappedStatement(statement);
从configuration中获取MappedStatement
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
executor执行查询
CachingExecutor
public
BoundSql boundSql = ms.getBoundSql(parameterObject);
在MappedStatement获取BoundSql
CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
创建CacheKey
return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
public
Cache cache = ms.getCache();
在MappedStatement中查找是否有缓存
BaseExecutor
public
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
缓存中没有,从数据中查询
private
list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
使用SimpleExecutor执行查询,并返回结果,
SimpleExecutor -》 StatementHandler(不同类型的StatementHandler)具体执行
SimpleExecutor
public
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
获取一个RoutingStatementHandler
stmt = prepareStatement(handler, ms.getStatementLog());
制作stmt
return handler.
在StatementHandler中执行jdbc的stmt,并返回查询结果
-finally closeStatement(stmt);
关闭statement
Configuration
protected final Map<String, MappedStatement> mappedStatements = new StrictMap
public MappedStatement getMappedStatement(String id, boolean validateIncompleteStatements) {...}
return mappedStatements.get(id);//id是com.roncoo.eshop.inventory.mapper.UserMapper.findUserInfo
从全局Map mappedStatements获取MappedStatement
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {...}
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
创建一个RoutingStatementHandler
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
这里也有拦截,new executor时给它增强类似,,返回一个增强过后的实例
MappedStatement
存放了一个mapper的元素信息,比如useCache、List
StatementHandler
制作statement,返回给exector调用
RoutingStatementHandler
构造函数
根据不同的ms.getStatementType() STATEMENT、PREPARED、CALLABLE 创建SimpleStatementHandler/PreparedStatementHandler/CallableStatementHandler
PreparedStatementHandler
public
PreparedStatement ps = (PreparedStatement) statement;
在PreparedStatementHandler中获取PreparedStatement
ps.execute();
执行请求
return resultSetHandler.
返回结果
这篇关于2mybatis-1条sql在mybatis的执行过程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-26大厂数据结构与算法教程:入门级详解
- 2024-12-26大厂算法与数据结构教程:新手入门指南
- 2024-12-26Python编程入门指南
- 2024-12-26数据结构高级教程:新手入门及初级提升指南
- 2024-12-26并查集入门教程:从零开始学会并查集
- 2024-12-26大厂数据结构与算法入门指南
- 2024-12-26大厂算法与数据结构入门教程
- 2024-12-26二叉树入门教程:轻松掌握基础概念与操作
- 2024-12-26初学者指南:轻松掌握链表
- 2024-12-26平衡树入门教程:轻松理解与应用