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