本博客着重介绍MyBatis执行Sql的流程,关于在执行过程中缓存、动态SQl生成等细节不在本博客中体现,相应内容后面再单独写博客分析吧。

还是以之前的查询作为列子:

  1. public class UserDaoTest {
  2. private SqlSessionFactory sqlSessionFactory;
  3. @Before
  4. public void setUp() throws Exception{
  5. ClassPathResource resource = new ClassPathResource("mybatis-config.xml");
  6. InputStream inputStream = resource.getInputStream();
  7. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  8. }
  9. @Test
  10. public void selectUserTest(){
  11. String id = "{0003CCCA-AEA9-4A1E-A3CC-06D884BA3906}";
  12. SqlSession sqlSession = sqlSessionFactory.openSession();
  13. CbondissuerMapper cbondissuerMapper = sqlSession.getMapper(CbondissuerMapper.class);
  14. Cbondissuer cbondissuer = cbondissuerMapper.selectByPrimaryKey(id);
  15. System.out.println(cbondissuer);
  16. sqlSession.close();
  17. }
  18. }

之前提到拿到sqlSession之后就能进行各种CRUD操作了,所以我们就从sqlSession.getMapper这个方法开始分析,看下整个Sql的执行流程是怎么样的。

进入sqlSession.getMapper方法,会发现调的是Configration对象的getMapper方法:

  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  2. //mapperRegistry实质上是一个Map,里面注册了启动过程中解析的各种Mapper.xml
  3. //mapperRegistry的key是接口的全限定名,比如com.csx.demo.spring.boot.dao.SysUserMapper
  4. //mapperRegistry的Value是MapperProxyFactory,用于生成对应的MapperProxy(动态代理类)
  5. return mapperRegistry.getMapper(type, sqlSession);
  6. }

进入getMapper方法:

  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  2. final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
  3. //如果配置文件中没有配置相关Mapper,直接抛异常
  4. if (mapperProxyFactory == null) {
  5. throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
  6. }
  7. try {
  8. //关键方法
  9. return mapperProxyFactory.newInstance(sqlSession);
  10. } catch (Exception e) {
  11. throw new BindingException("Error getting mapper instance. Cause: " + e, e);
  12. }
  13. }

进入MapperProxyFactory的newInstance方法:

  1. public class MapperProxyFactory<T> {
  2. private final Class<T> mapperInterface;
  3. private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();
  4. public MapperProxyFactory(Class<T> mapperInterface) {
  5. this.mapperInterface = mapperInterface;
  6. }
  7. public Class<T> getMapperInterface() {
  8. return mapperInterface;
  9. }
  10. public Map<Method, MapperMethod> getMethodCache() {
  11. return methodCache;
  12. }
  13. //生成Mapper接口的动态代理类MapperProxy
  14. @SuppressWarnings("unchecked")
  15. protected T newInstance(MapperProxy<T> mapperProxy) {
  16. return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  17. }
  18. public T newInstance(SqlSession sqlSession) {
  19. final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
  20. return newInstance(mapperProxy);
  21. }
  22. }

下面是动态代理类MapperProxy,调用Mapper接口的所有方法都会先调用到这个代理类的invoke方法(注意由于Mybatis中的Mapper接口没有实现类,所以MapperProxy这个代理对象中没有委托类,也就是说MapperProxy干了代理类和委托类的事情)。好了下面重点看下invoke方法。

  1. //MapperProxy代理类
  2. public class MapperProxy<T> implements InvocationHandler, Serializable {
  3. private static final long serialVersionUID = -6424540398559729838L;
  4. private final SqlSession sqlSession;
  5. private final Class<T> mapperInterface;
  6. private final Map<Method, MapperMethod> methodCache;
  7. public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
  8. this.sqlSession = sqlSession;
  9. this.mapperInterface = mapperInterface;
  10. this.methodCache = methodCache;
  11. }
  12. @Override
  13. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  14. try {
  15. if (Object.class.equals(method.getDeclaringClass())) {
  16. return method.invoke(this, args);
  17. } else if (isDefaultMethod(method)) {
  18. return invokeDefaultMethod(proxy, method, args);
  19. }
  20. } catch (Throwable t) {
  21. throw ExceptionUtil.unwrapThrowable(t);
  22. }
  23. //获取MapperMethod,并调用MapperMethod
  24. final MapperMethod mapperMethod = cachedMapperMethod(method);
  25. return mapperMethod.execute(sqlSession, args);
  26. }
  27. private MapperMethod cachedMapperMethod(Method method) {
  28. MapperMethod mapperMethod = methodCache.get(method);
  29. if (mapperMethod == null) {
  30. mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
  31. methodCache.put(method, mapperMethod);
  32. }
  33. return mapperMethod;
  34. }
  35. @UsesJava7
  36. private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
  37. throws Throwable {
  38. final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
  39. .getDeclaredConstructor(Class.class, int.class);
  40. if (!constructor.isAccessible()) {
  41. constructor.setAccessible(true);
  42. }
  43. final Class<?> declaringClass = method.getDeclaringClass();
  44. return constructor
  45. .newInstance(declaringClass,
  46. MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
  47. | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
  48. .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
  49. }
  50. /**
  51. * Backport of java.lang.reflect.Method#isDefault()
  52. */
  53. private boolean isDefaultMethod(Method method) {
  54. return ((method.getModifiers()
  55. & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC)
  56. && method.getDeclaringClass().isInterface();
  57. }
  58. }

所以这边需要进入MapperMethod的execute方法:

  1. public Object execute(SqlSession sqlSession, Object[] args) {
  2. Object result;
  3. //判断是CRUD那种方法
  4. switch (command.getType()) {
  5. case INSERT: {
  6. Object param = method.convertArgsToSqlCommandParam(args);
  7. result = rowCountResult(sqlSession.insert(command.getName(), param));
  8. break;
  9. }
  10. case UPDATE: {
  11. Object param = method.convertArgsToSqlCommandParam(args);
  12. result = rowCountResult(sqlSession.update(command.getName(), param));
  13. break;
  14. }
  15. case DELETE: {
  16. Object param = method.convertArgsToSqlCommandParam(args);
  17. result = rowCountResult(sqlSession.delete(command.getName(), param));
  18. break;
  19. }
  20. case SELECT:
  21. if (method.returnsVoid() && method.hasResultHandler()) {
  22. executeWithResultHandler(sqlSession, args);
  23. result = null;
  24. } else if (method.returnsMany()) {
  25. result = executeForMany(sqlSession, args);
  26. } else if (method.returnsMap()) {
  27. result = executeForMap(sqlSession, args);
  28. } else if (method.returnsCursor()) {
  29. result = executeForCursor(sqlSession, args);
  30. } else {
  31. Object param = method.convertArgsToSqlCommandParam(args);
  32. result = sqlSession.selectOne(command.getName(), param);
  33. }
  34. break;
  35. case FLUSH:
  36. result = sqlSession.flushStatements();
  37. break;
  38. default:
  39. throw new BindingException("Unknown execution method for: " + command.getName());
  40. }
  41. if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
  42. throw new BindingException("Mapper method '" + command.getName()
  43. + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
  44. }
  45. return result;
  46. }

然后,通过一层一层的调用,最终会来到doQuery方法, 这儿咱们就随便找个Excutor看看doQuery方法的实现吧,我这儿选择了SimpleExecutor:

  1. public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  2. Statement stmt = null;
  3. try {
  4. Configuration configuration = ms.getConfiguration();
  5. //内部封装了ParameterHandler和ResultSetHandler
  6. StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  7. stmt = prepareStatement(handler, ms.getStatementLog());
  8. //StatementHandler封装了Statement, 让 StatementHandler 去处理
  9. return handler.<E>query(stmt, resultHandler);
  10. } finally {
  11. closeStatement(stmt);
  12. }
  13. }

接下来,咱们看看StatementHandler 的一个实现类 PreparedStatementHandler(这也是我们最常用的,封装的是PreparedStatement), 看看它使怎么去处理的:

  1. public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  2. //到此,原形毕露, PreparedStatement, 这个大家都已经滚瓜烂熟了吧
  3. PreparedStatement ps = (PreparedStatement) statement;
  4. ps.execute();
  5. //结果交给了ResultSetHandler 去处理,处理完之后返回给客户端
  6. return resultSetHandler.<E> handleResultSets(ps);
  7. }

到此,整个调用流程结束。

这边结合获取SqlSession的流程,做下简单的总结:

  • SqlSessionFactoryBuilder解析配置文件,包括属性配置、别名配置、拦截器配置、环境(数据源和事务管理器)、Mapper配置等;解析完这些配置后会生成一个Configration对象,这个对象中包含了MyBatis需要的所有配置,然后会用这个Configration对象创建一个SqlSessionFactory对象,这个对象中包含了Configration对象;
  • 拿到SqlSessionFactory对象后,会调用SqlSessionFactory的openSesison方法,这个方法会创建一个Sql执行器(Executor组件中包含了Transaction对象),这个Sql执行器会代理你配置的拦截器方法
  • 获得上面的Sql执行器后,会创建一个SqlSession(默认使用DefaultSqlSession),这个SqlSession中也包含了Configration对象和上面创建的Executor对象,所以通过SqlSession也能拿到全局配置;
  • 获得SqlSession对象后就能执行各种CRUD方法了。

以上是获得SqlSession的流程,下面总结下本博客中介绍的Sql的执行流程:

  • 调用SqlSession的getMapper方法,获得Mapper接口的动态代理对象MapperProxy,调用Mapper接口的所有方法都会调用到MapperProxy的invoke方法(动态代理机制);
  • MapperProxy的invoke方法中唯一做的就是创建一个MapperMethod对象,然后调用这个对象的execute方法,sqlSession会作为execute方法的入参;
  • 往下,层层调下来会进入Executor组件(如果配置插件会对Executor进行动态代理)的query方法,这个方法中会创建一个StatementHandler对象,这个对象中同时会封装ParameterHandler和ResultSetHandler对象。调用StatementHandler预编译参数以及设置参数值,使用ParameterHandler来给sql设置参数。

Executor组件有两个直接实现类,分别是BaseExecutor和CachingExecutor。CachingExecutor静态代理了BaseExecutor。Executor组件封装了Transction组件,Transction组件中又分装了Datasource组件。

  • 调用StatementHandler的增删改查方法获得结果,ResultSetHandler对结果进行封装转换,请求结束。

Executor、StatementHandler 、ParameterHandler、ResultSetHandler,Mybatis的插件会对上面的四个组件进行动态代理。

  • MapperProxyFactory

  • MapperProxy

  • MapperMethod

  • SqlSession:作为MyBatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能;

  • Executor:MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护;

    StatementHandler 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数、将Statement结果集转换成List集合。
    ParameterHandler 负责对用户传递的参数转换成JDBC Statement 所需要的参数,
    ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合;
    TypeHandler 负责java数据类型和jdbc数据类型之间的映射和转换
    MappedStatement MappedStatement维护了一条<select|update|delete|insert>节点的封装,
    SqlSource 负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
    BoundSql 表示动态生成的SQL语句以及相应的参数信息

    Configuration MyBatis所有的配置信息都维持在Configuration对象之中。

  • https://www.cnblogs.com/dongying/p/4031382.html
  • https://blog.csdn.net/qq_38409944/article/details/82494187

版权声明:本文为54chensongxia原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/54chensongxia/p/11850470.html