上篇记录了加载过程。现在介绍下执行过程。还是同一个demo
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("com/forcht/testMybatis/mybatis-config.xml");
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
PaperMapper mapper = session.getMapper(PaperMapper.class);
System.out.println(mapper.selectPaper(1));
}
1、调用sessionFactory.openSession();发生了什么?
第一步
SqlSession session = sessionFactory.openSession();
第二步
//先获取一个默认的DefaultExecutorType,再调用openSessionFromDataSource
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
//openSessionFromDataSource的执行过程,根据配置信息Environment
//创建Transaction 对象。根据Transaction 创建Executor。最终创建一个DefaultSqlSession
//DefaultSqlSession里包装了配置信息configuration,执行器executor
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
2、PaperMapper mapper = session.getMapper(PaperMapper.class);这个Mapper是如何生成的?
第一步
PaperMapper mapper = session.getMapper(PaperMapper.class);
第二步
//上面提到了DefaultSqlSession里包装了配置信息configuration。这里就用到了。
//将getMapper具体操作委托给了configuration
//回想下,configuration为啥能获取mapper?
@Override
public <T> T getMapper(Class<T> type) {
return configuration.<T>getMapper(type, this);
}
第三步
//configuration有直接将getMapper委托给了mapperRegistry
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
//第四步
//获取一个MapperProxyFactory,再使用这个工厂类创建mapper
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
第五步
//这里先看下MapperProxy。该类实现了InvocationHandler。熟悉了吧
//这里用到了JDK的动态代理。MapperProxy就是代理对象
public class MapperProxy<T> implements InvocationHandler, Serializable {
//mapperInterface就是需要代理的接口。我们调用该接口的方法时就会被MapperProxy代理
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
//下面这段代码大家都熟悉了,就是JDK的生成代理对象的典型的代码
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
3、调用mapper.selectPaper(1)会发生了什么
第一步
//上面的执行结果就会返回一个代理对象。当我们调用PaperMapper接口的方法时,就会执行MapperProxy的invoke方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
//判断是不是Object类的方法,如果是则直接执行
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else if (isDefaultMethod(method)) {
return invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
//获取一个MapperMethod ,并调用MapperMethod 的execute执行操作
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
第二步
//先判断执行的sql语句是什么类型,这里是select。
//调用sqlSession.selectOne(command.getName(), param);
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
default:
throw new BindingException("Unknown execution method for: " + command.getName());
}
if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
throw new BindingException("Mapper method '" + command.getName()
+ " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}
第三步
//select方法最终会调用selectList方法来统一执行,
//如果是selectOne。则list刚好大小为1.返回list.get(0),否则执行出错
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
第四步
//上面提到了DefaultSqlSession里包装了执行器executor。这里直接将作
//委托给executor执行
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
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();
}
}
第五步
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
//boundSql 包含了我们要执行的sql语句
BoundSql boundSql = ms.getBoundSql(parameterObject);
//暂时还不清楚CacheKey的作用,估计跟缓存有关系
CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
第六步
//先判断Cache 是否有记录,如果有直接返回。否则执行delegate.<E> query()方法
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
throws SQLException {
Cache cache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
if (ms.isUseCache() && resultHandler == null) {
ensureNoOutParams(ms, boundSql);
@SuppressWarnings("unchecked")
List<E> list = (List<E>) tcm.getObject(cache, key);
if (list == null) {
list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
tcm.putObject(cache, key, list); // issue #578 and #116
}
return list;
}
}
return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
第七步
//中间省略了一些过程。最终会将操作委托给ResultHandler来执行
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
第八步
//最后使用jdbc的PreparedStatement 来执行sql语句。
//并将结果交给resultSetHandler处理
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return resultSetHandler.<E> handleResultSets(ps);
}
以上就是mybatis的执行过程。一些细节后面再补充。