mybatis 版本:3.5.1
测试代码:
public interface MyUserMapperAnnotation { @Select("select * from myuser where id = #{id}") MyUser selectMyUser(Integer id); } public static void main(String[] args) throws IOException { SqlSession session = null; try { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); session = sqlSessionFactory.openSession(); MyUserMapperAnnotation mapper = session.getMapper(MyUserMapperAnnotation.class); System.out.println(mapper.selectMyUser(4)); } finally { if (session != null) { session.close(); } } }
System.out.println(mapper.selectMyUser(4))
org.apache.ibatis.binding.MapperProxy
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 代理以后,所有 Mapper 的方法调用时,都会调用这个 invoke 方法 try { // 并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是 Object 中通用的方法(toString、hashCode等)则无需执行 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 final MapperMethod mapperMethod = cachedMapperMethod(method); // 执行 return mapperMethod.execute(sqlSession, args); }
return mapperMethod.execute(this.sqlSession, args)
org.apache.ibatis.binding.MapperMethod
public Object execute(SqlSession sqlSession, Object[] args) { Object result; // 执行时就是4种情况,insert|update|delete|select,分别调用 SqlSession 的 4 大类方法 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()) { // 如果结果是 Map result = executeForMap(sqlSession, args); } else if (method.returnsCursor()) { // 如果结果是 Cursor result = executeForCursor(sqlSession, args); } else { // 封装查询的入参 Object param = method.convertArgsToSqlCommandParam(args); // 结果是一条记录 result = sqlSession.selectOne(command.getName(), param); if (method.returnsOptional() && (result == null || !method.getReturnType().equals(result.getClass()))) { result = Optional.ofNullable(result); } } 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; }
result = sqlSession.selectOne(this.command.getName(), param)
org.apache.ibatis.session.defaults.DefaultSqlSession
@Override public <T> T selectOne(String statement, Object parameter) { // Popular vote was to return null on 0 results and throw exception on too many. // 转而去调用selectList,如果得到 0 条则返回null,得到1条则返回1条,得到多条报 TooManyResultsException 异常 // 特别需要主要的是当没有查询到结果的时候就会返回null。因此一般建议在 mapper中编写 resultType 的时候使用包装类型 List<T> list = this.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; } } @Override public <E> List<E> selectList(String statement, Object parameter) { return this.selectList(statement, parameter, RowBounds.DEFAULT); } @Override public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) { try { // 根据 statement id 找到对应的 MappedStatement MappedStatement ms = configuration.getMappedStatement(statement); // 用执行器来查询结果,这里传入的 ResultHandler 是 null 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(); } } private Object wrapCollection(final Object object) { // 把参数包装成 Collection if (object instanceof Collection) { StrictMap<Object> map = new StrictMap<>(); // 参数若是 Collection 类型,做 collection 标记 map.put("collection", object); if (object instanceof List) { // 参数若是 List 类型,做 list 标记 map.put("list", object); } return map; } else if (object != null && object.getClass().isArray()) { StrictMap<Object> map = new StrictMap<>(); // 参数若是数组类型,做 array 标记 map.put("array", object); return map; } // 参数若不是集合类型,直接返回原来值 return object; }
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER)
org.apache.ibatis.executor.CachingExecutor
@Override public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { // 获取执行的 SQL BoundSql boundSql = ms.getBoundSql(parameterObject); // 生成缓存的key,query 时传入 CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql); return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); } @Override public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { Cache cache = ms.getCache(); // 默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/> // 简单的说,就是先查 CacheKey,查不到再委托给实际的执行器去查 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.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); tcm.putObject(cache, key, list); // issue #578 and #116 } return list; } } // 二级缓存没有开启,或未在二级缓存中找到,再继续查询 return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); }
return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql)
org.apache.ibatis.executor.BaseExecutor
@SuppressWarnings("unchecked") @Override public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { // 如果已经关闭,报错 ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId()); if (closed) { throw new ExecutorException("Executor was closed."); } // 先清局部缓存,再查询。但仅查询堆栈为0,才清。为了处理递归调用 if (queryStack == 0 && ms.isFlushCacheRequired()) { clearLocalCache(); } List<E> list; try { // 加一,这样递归调用到上面的时候就不会再清局部缓存了 queryStack++; // 先根据 cachekey 从 localCache(一级缓存)去查 list = resultHandler == null ? (List<E>) localCache.getObject(key) : null; if (list != null) { // 若查到 localCache 缓存,处理 localOutputParameterCache handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); } else { // 从数据库查 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 // 如果是 STATEMENT,清本地缓存 clearLocalCache(); } } return list; } private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { List<E> list; // 先向缓存中放入占位符??? localCache.putObject(key, EXECUTION_PLACEHOLDER); try { // 查询数据 list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql); } finally { // 最后删除占位符 localCache.removeObject(key); } // 加入缓存 localCache.putObject(key, list); // 如果是存储过程,OUT 参数也加入缓存 if (ms.getStatementType() == StatementType.CALLABLE) { localOutputParameterCache.putObject(key, parameter); } return list; }
list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql)
org.apache.ibatis.executor.SimpleExecutor
@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(); // 更具 StatementType 创建 StatementHandler,这里传入了 resultHandler 和 boundSql,这一步会还会创建 ParameterHandler 和 ResultSetHandler StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); // 准备语句 stmt = prepareStatement(handler, ms.getStatementLog()); // StatementHandler.query return handler.query(stmt, resultHandler); } finally { // 关闭资源 closeStatement(stmt); } } private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; Connection connection = getConnection(statementLog); // 进行参数预编译,这一步会应用 StatementHandler 插件 stmt = handler.prepare(connection, transaction.getTimeout()); // 根据 TypeHandler 设置 SQL 参数,这一步会应用 ParameterHandler 插件 handler.parameterize(stmt); return stmt; }
return handler.query(stmt, resultHandler)
org.apache.ibatis.executor.statement.PreparedStatementHandler
@Override public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; // 查询 ps.execute(); // 根据 TypeHandlee 封装结果集(getPropertyMappingValue()),这一步会应用 ResultSetHandler 插件 return resultSetHandler.handleResultSets(ps); }
时序图
查询流程
MyBatis 框架分层架构
https://github.com/tuguangquan/mybatis/tree/master/src/main/java/org/apache/ibatis