mybatis源码(四)SqlSession执行mapper的过程下篇
代码案例:以SqlSession的selectList为例
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
public class TestUserMapper {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void init(){
try {
Reader reader = Resources.getResourceAsReader("mybatis.config.xml");
sqlSessiOnFactory= new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void TestMybatis(){
SqlSession sqlSession = sqlSessionFactory.openSession();
List
System.out.println(userList.size());
System.out.println(userList.get(0).getNickname());
System.out.println(userList.get(0).getCreateTime());
}
}
首先SqlSession是一个接口,有一个DefaultSqlSession的实现类,实现类实现该接口的代码如下:
@Override
public
try {
// 根据Mapper的Id,获取对应的MappedStatement对象
MappedStatement ms = configuration.getMappedStatement(statement);
// 以MappedStatement对象作为参数,调用Executor的query()方法
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
configuration的mapperstatement的对象的值 是在SqlSession创建的时候,通过XmlConfigBuilder解析mybatis-config主配置文件的时候,添加进去的 configration中有一个map集合如下图所示
protected final Map
该属性是一个Map对象,它的key为mapperSql配置的id。如果sql是通过xml注释的。则id为命名空间加上
通过上述源码可以看到,实际操作数据库的事Excutor执行器,这里用到了一个外观模式,为了对用户提供友好的访问接口,Excutor执行器是一个接口,有几个实现类
进入实现类BaseExecutor中,查看query的源码
queryFromDatabase 这个地方由于缓存中没有我们要找的数据,所以从数据库中进行数据的读取,这个doQuery方法用到了模板方法设计模式根据上述的类图,可以看到有几个实现类,分别提供了不同的实现
@Override
public
// 获取BoundSql对象,BoundSql是对动态SQL解析生成的SQL语句和参数映射信息的封装
BoundSql boundSql = ms.getBoundSql(parameter);
// 创建CacheKey,用于缓存Key
CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
// 调用重载的query()方法
return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
@SuppressWarnings("unchecked")
@Override
public
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List
try {
queryStack++;
// 从缓存中获取结果
list = resultHandler == null ? (List
if (list != null) {
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
// 缓存中获取不到,则调用queryFromDatabase()方法从数据库中查询
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
// issue #601
deferredLoads.clear();
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
// issue #482
clearLocalCache();
}
}
return list;
}
private
List
localCache.putObject(key, EXECUTION_PLACEHOLDER);
try {
// 调用doQuery()方法查询
list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
} finally {
localCache.removeObject(key);
}
// 缓存查询结果
localCache.putObject(key, list);
if (ms.getStatementType() == StatementType.CALLABLE) {
localOutputParameterCache.putObject(key, parameter);
}
return list;
}
protected abstract
throws SQLException;
@Override
public
Statement stmt = null;
try {
Configuration cOnfiguration= ms.getConfiguration();
// 获取StatementHandler对象
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
// 调用prepareStatement()方法,创建Statement对象,并进行设置参数等操作
stmt = prepareStatement(handler, ms.getStatementLog());
// 调用StatementHandler对象的query()方法执行查询操作
return handler.
} finally {
closeStatement(stmt);
}
}
实现类2:BatchExecutor
@Override
public
throws SQLException {
Statement stmt = null;
try {
flushStatements();
Configuration cOnfiguration= ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
Connection cOnnection= getConnection(ms.getStatementLog());
stmt = handler.prepare(connection, transaction.getTimeout());
handler.parameterize(stmt);
return handler.
} finally {
closeStatement(stmt);
}
}
实现类3:ReuseExecutor
@Override
public
Configuration cOnfiguration= ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
return handler.
}
Statement 操作数据库就是JDBC提供的操作数据的实现
从三述三种实现类中可以看出StatementHandler是操作Statement的。
@Override
public
String sql = boundSql.getSql();
statement.execute(sql);
return resultSetHandler.
}
实现类2:PreparedStatementHandler
@Override
public
PreparedStatement ps = (PreparedStatement) statement;
// 调用PreparedStatement对象的execute()方法,执行SQL语句
ps.execute();
// 调用ResultSetHandler的handleResultSets()方法处理结果集
return resultSetHandler.
}
实现类3:CallableStatementHandler
@Override
public
CallableStatement cs = (CallableStatement) statement;
cs.execute();
List
resultSetHandler.handleOutputParameters(cs);
return resultList;
}
ResultSetHandler 是一个接口实现,只有一个实现类
public interface ResultSetHandler {
void handleOutputParameters(CallableStatement cs) throws SQLException;
}
DefaultResultSetHandler
//
// 处理结果集
//
@Override
public List handleResultSets(Statement stmt) throws SQLException {
ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
final List multipleResults = new ArrayList();
int resultSetCount = 0;
// 1、获取ResultSet对象,將ResultSet对象包装为ResultSetWrapper
ResultSetWrapper rsw = getFirstResultSet(stmt);
// 2、获取ResultMap信息,一般只有一个ResultMap
List
int resultMapCount = resultMaps.size();
// 校验ResultMap,如果该ResultMap名称没有配置,则抛出异常
validateResultMapsCount(rsw, resultMapCount);
// 如果指定了多个ResultMap,则对每个ResultMap进行处理
while (rsw != null && resultMapCount > resultSetCount) {
ResultMap resultMap = resultMaps.get(resultSetCount);
// 3、调用handleResultSet方法处理结果集
handleResultSet(rsw, resultMap, multipleResults, null);
// 获取下一个结果集对象,需要JDBC驱动支持多结果集
rsw = getNextResultSet(stmt);
cleanUpAfterHandlingResultSet();
resultSetCount++;
}
// 如果JDBC驱动支持多结果集,可以通过
至此,就完成了SqlSession.selectList的 流程,源码涉及的东西比较多,这就是个大致的流程