11.23 Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

来源:https://my.oschina.net/zudajun/blog/670373

Mybatis中的Sql命令,在枚举类SqlCommandType中定义的。

public enum SqlCommandType {
UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;
}

下面,我们以Mapper接口中的一个方法作为例子,看看Sql命令的执行完整流程。

public interface StudentMapper {
List<student> findAllStudents(Map<string> map, RowBounds rowBounds, ResultSetHandler rh);
}
/<string>/<student>

参数RowBounds和ResultSetHandler是可选参数,表示分页对象和自定义结果集处理器,一般不需要。

一个完整的Sql命令,其执行的完整流程图如下:

Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

一个图,就完整展示了其执行流程。

MapperProxy的功能:

1.因为Mapper接口不能直接实例化,MapperProxy的作用,就是使用JDK动态代理功能,间接实例化Mapper的proxy对象。

2.缓存MapperMethod对象。

Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

MapperMethod的功能:

1.解析Mapper接口的方法,并封装成MapperMethod对象。

2.将Sql命令,正确路由到恰当的SqlSession的方法上。

Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

org.apache.ibatis.binding.MapperMethod.SqlCommand。

public static class SqlCommand {
// full id, 通过它可以找到MappedStatement
private final String name;
private final SqlCommandType type;
// ...

org.apache.ibatis.binding.MapperMethod.MethodSignature。

Mybatis3.3.x技术内幕:执行一个Sql命令的完整流程

以上是对MapperMethod的补充说明。

本节的重点,是上面的那个Sql命令完整执行流程图。如果不是使用Mapper接口调用,而是直接调用SqlSession的方法,那么,流程图从SqlSession的地方开始即可,后续都是一样的。


分享到:


相關文章: