2mybatis-1条sql在mybatis的执行过程

MapperProxy --|》MapperMethod --|》 SqlSession 调用sh去执行ms -> PreparedStatementHandler 解析出stmt,然后执行stmt -> resultSetHandler解析结果

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<结果>查询

Object executeForMany(SqlSession sqlSession, Object[] args) {..}

result = sqlSession.selectList(command.getName(), param);

DefaultSqlSession

public List selectList(String statement/** 这个类名+方法名+参数*/, Object parameter) {..}

return this.selectList(statement, parameter, RowBounds.DEFAULT);

public List selectList(String statement, Object parameter, RowBounds rowBounds) {...}

MappedStatement ms = configuration.getMappedStatement(statement);
从configuration中获取MappedStatement

return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
executor执行查询

CachingExecutor

public List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) {...}

BoundSql boundSql = ms.getBoundSql(parameterObject);
在MappedStatement获取BoundSql

CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
创建CacheKey

return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

public List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql){...}

Cache cache = ms.getCache();
在MappedStatement中查找是否有缓存

BaseExecutor

public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {...}

list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
缓存中没有,从数据中查询

private List queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {...}

list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
使用SimpleExecutor执行查询,并返回结果,
SimpleExecutor -》 StatementHandler(不同类型的StatementHandler)具体执行

SimpleExecutor

public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {...}

Configuration configuration = ms.getConfiguration();

StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
获取一个RoutingStatementHandler

stmt = prepareStatement(handler, ms.getStatementLog());
制作stmt

return handler.query(stmt, resultHandler);
在StatementHandler中执行jdbc的stmt,并返回查询结果

-finally closeStatement(stmt);
关闭statement

Configuration

protected final Map<String, MappedStatement> mappedStatements = new StrictMap("Mapped Statements collection");

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 resultMaps、参数等

StatementHandler

制作statement,返回给exector调用

RoutingStatementHandler

构造函数
根据不同的ms.getStatementType() STATEMENT、PREPARED、CALLABLE 创建SimpleStatementHandler/PreparedStatementHandler/CallableStatementHandler

PreparedStatementHandler

public List query(Statement statement, ResultHandler resultHandler) throws SQLException {...}

PreparedStatement ps = (PreparedStatement) statement;
在PreparedStatementHandler中获取PreparedStatement

ps.execute();
执行请求

return resultSetHandler. handleResultSets(ps);
返回结果

posted @ 2021-08-05 01:43  ??,uunu  阅读(48)  评论(0编辑  收藏  举报