Mybatis执行mapper过程(二)

案例

先看下一次完成的Mybatis查询过程示例代码:

1
2
3
4
5
6
7
8
9
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
//调用代理对象方法
User user = userMapper.findUserById(27);
//关闭session
session.close();

本文先重点分析,以下代码背后的原理

1
2
UserMapper userMapper = session.getMapper(UserMapper.class);  
User user = userMapper.findUserById(27);

思考一个问题,为什么userMapper没有接口实现类也能成功执行findUserById()?

创建UserMapper代理对象

首先我们看下getMapper()是怎样获取代理对象的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
DefaultSqlSession.java
public <T> T getMapper(Class<T> type) {
return configuration.<T>getMapper(type, this);
}

------>>>>>>

Configuration.java
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}

------>>>>>>

MapperRegistry.java
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//一个Mapper接口类对应一个MapperProxyFactory
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);
}
}

------>>>>>>

MapperProxyFactory.java
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}

创建Mapper代理对象过程.png

大致过程,先获取MapperProxyFactory,然后使用JDK动态代理方式构建出代理对象。在MapperRegistry.java中我们可以知道,mapperProxyFactory由knownMappers.get(type)得到,那么knownMappers中的数据什么时候维护进去的呢?我们看下下面的MapperRegistry.java完整的代码。

MapperRegistry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//这个类通过名字就可以看出 是用来注册Mapper接口与获取生成代理类实例的工具类 
public class MapperRegistry {
//全局配置文件对象
private Configuration config;
//一个HashMap Key是mapper的类型对象, Value是MapperProxyFactory对象
//这个MapperProxyFactory是创建Mapper代理对象的工厂 我们一会再分析
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
public MapperRegistry(Configuration config) {
this.config = config;
}
//获取生成的代理对象
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//通过Mapper的接口类型 去Map当中查找 如果为空就抛异常
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null)
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
try {
//否则创建一个当前接口的代理对象 并且传入sqlSession
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}

public <T> boolean hasMapper(Class<T> type) {
return knownMappers.containsKey(type);
}

//注册Mapper接口!!!!
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<T>(type));

MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}
public Collection<Class<?>> getMappers() {
return Collections.unmodifiableCollection(knownMappers.keySet());
}
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
}
//通过包名扫描下面所有接口
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
}

}

MapperRegistry 类是注册Mapper接口与获取代理类实例的工具类。从上述代码可以看到,addMapper(Class type)方法中维护了knownMappers。而XMLConfigBuilder.mapperElement(XNode) 是在读取配置文件时[XMLConfigBuilder 118行]调用了该方法addMapper(Class type),即在加载配置文件时候为每一个Mapper类(UserMapper)维护了对应的MapperProxyFactory。

MapperProxyFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//这个类负责创建具体Mapper接口代理对象的工厂类
public class MapperProxyFactory<T> {
//具体Mapper接口的Class对象
private final Class<T> mapperInterface;
//该接口下面方法的缓存 key是方法对象 value是对接口中方法对象的封装
private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();
//构造参数没啥好说的
public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}
public Class<T> getMapperInterface() {
return mapperInterface;
}
public Map<Method, MapperMethod> getMethodCache() {
return methodCache;
}
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
//创建了一个代理类并返回
//关于Proxy的API 可以查看java官方的API
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
//在这里传入sqlSession 创建一个Mapper接口的代理类
public T newInstance(SqlSession sqlSession) {
//在这里创建了MapperProxy对象 这个类实现了JDK的动态代理接口 InvocationHandler
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
//调用上面的方法 返回一个接口的代理类
return newInstance(mapperProxy);
}
}

从上述代码可以看到在newInstance(MapperProxy mapperProxy)中,利用mapperProxy作为InvocationHandler来创建代理对象。

MapperProxy

MapperProxy的源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//实现了JDK动态代理的接口 InvocationHandler
//在invoke方法中实现了代理方法调用的细节
public class MapperProxy<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
//SqlSession
private final SqlSession sqlSession;
//接口的类型对象
private final Class<T> mapperInterface;
//接口中方法的缓存 有MapperProxyFactory传递过来的。
private final Map<Method, MapperMethod> methodCache;
//构造参数
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
//接口代理对象所有的方法调用 都会调用该方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//判断是不是基础方法 比如toString() hashCode()等,这些方法直接调用不需要处理
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
}
//这里进行缓存
final MapperMethod mapperMethod = cachedMapperMethod(method);
//调用mapperMethod.execute 核心的地方就在这个方法里,这个方法对才是真正对SqlSession进行的包装调用
return mapperMethod.execute(sqlSession, args);
}
//缓存处理
private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}
}

从上述代码可以看到,invoke()方法中的逻辑是执行相应的 SQL 语句并将结果集返回,而没有相应的去执行接口实现类方法的代码。
这就是为什么mybatis的mapper没有实现类的原因?

MapperMethod

MapperMethod是MapperProxyFactory类在创建MapperProxy对象时传进来的。我们看下源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//这个类是整个代理机制的核心类,对Sqlsession当中的操作进行了封装
public class MapperMethod {
//一个内部封 封装了SQL标签的类型 insert update delete select
private final SqlCommand command;
//一个内部类 封装了方法的参数信息 返回类型信息等
private final MethodSignature method;
//构造参数
public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
this.command = new SqlCommand(config, mapperInterface, method);
this.method = new MethodSignature(config, method);
}
//这个方法是对SqlSession的包装调用
public Object execute(SqlSession sqlSession, Object[] args) {
//定义返回结果
Object result;
//如果是INSERT操作
if (SqlCommandType.INSERT == command.getType()) {
//处理参数
Object param = method.convertArgsToSqlCommandParam(args);
//调用sqlSession的insert方法
result = rowCountResult(sqlSession.insert(command.getName(), param));
//如果是UPDATE操作 同上
} else if (SqlCommandType.UPDATE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
//如果是DELETE操作 同上
} else if (SqlCommandType.DELETE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
//如果是SELECT操作 那么情况会多一些 但是也都和sqlSession的查询方法一一对应
} else if (SqlCommandType.SELECT == command.getType()) {
//如果返回void 并且参数有resultHandler
//则调用 void select(String statement, Object parameter, ResultHandler handler);方法
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
//如果返回多行结果这调用 <E> List<E> selectList(String statement, Object parameter);
//executeForMany这个方法调用的
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
//如果返回类型是MAP 则调用executeForMap方法
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else {
//否则就是查询单个对象
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
}
} else {
//如果全都不匹配 说明mapper中定义的方法不对
throw new BindingException("Unknown execution method for: " + command.getName());
}
//如果返回值为空 并且方法返回值类型是基础类型 并且不是VOID 则抛出异常
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;
}
private Object rowCountResult(int rowCount) {
final Object result;
if (method.returnsVoid()) {
result = null;
} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
result = rowCount;
} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
result = (long) rowCount;
} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
result = (rowCount > 0);
} else {
throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
}
return result;
}
private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
if (void.class.equals(ms.getResultMaps().get(0).getType())) {
throw new BindingException("method " + command.getName()
+ " needs either a @ResultMap annotation, a @ResultType annotation,"
+ " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
}
Object param = method.convertArgsToSqlCommandParam(args);
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
} else {
sqlSession.select(command.getName(), param, method.extractResultHandler(args));
}
}
//返回多行结果 调用sqlSession.selectList方法
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
List<E> result;
Object param = method.convertArgsToSqlCommandParam(args);
//如果参数含有rowBounds则调用分页的查询
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
} else {
//没有分页则调用普通查询
result = sqlSession.<E>selectList(command.getName(), param);
}
// issue #510 Collections & arrays support
if (!method.getReturnType().isAssignableFrom(result.getClass())) {
if (method.getReturnType().isArray()) {
return convertToArray(result);
} else {
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
}
return result;
}
private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
Object collection = config.getObjectFactory().create(method.getReturnType());
MetaObject metaObject = config.newMetaObject(collection);
metaObject.addAll(list);
return collection;
}
@SuppressWarnings("unchecked")
private <E> E[] convertToArray(List<E> list) {
E[] array = (E[]) Array.newInstance(method.getReturnType().getComponentType(), list.size());
array = list.toArray(array);
return array;
}
private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Map<K, V> result;
Object param = method.convertArgsToSqlCommandParam(args);
if (method.hasRowBounds()) {
RowBounds rowBounds = method.extractRowBounds(args);
result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey(), rowBounds);
} else {
result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey());
}
return result;
}
public static class ParamMap<V> extends HashMap<String, V> {
private static final long serialVersionUID = -2212268410512043556L;
@Override
public V get(Object key) {
if (!super.containsKey(key)) {
throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + keySet());
}
return super.get(key);
}
}

在核心方法execute(SqlSession sqlSession, Object[] args)中,最后还是根据类型去选择到底执行sqlSession中的哪个方法。
SqlCommand来封装底层的增删改查操作。我们看下SqlCommand源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//一个内部类 封装了具体执行的动作
public static class SqlCommand {
//xml标签的id
private final String name;
//insert update delete select的具体类型
private final SqlCommandType type;
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) throws BindingException {
//拿到全名 比如 org.mybatis.example.BlogMapper.selectBlog
String statementName = mapperInterface.getName() + "." + method.getName();
MappedStatement ms = null;
//获取MappedStatement对象 这个对象封装了XML当中一个标签的所有信息 比如下面
//<select id="selectBlog" resultType="Blog">
//select * from Blog where id = #{id}
//</select>
if (configuration.hasStatement(statementName)) {
ms = configuration.getMappedStatement(statementName);
} else if (!mapperInterface.equals(method.getDeclaringClass().getName())) { // 这里是一个BUG
String parentStatementName = method.getDeclaringClass().getName() + "." + method.getName();
if (configuration.hasStatement(parentStatementName)) {
ms = configuration.getMappedStatement(parentStatementName);
}
}
//为空抛出异常
if (ms == null) {
throw new BindingException("Invalid bound statement (not found): " + statementName);
}
//配置文件节点的ID
name = ms.getId();
//CRUD
type = ms.getSqlCommandType();
//判断SQL标签类型 未知就抛异常
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
public String getName() {
return name;
}
public SqlCommandType getType() {
return type;
}
}

我们都会知道节点上有id属性值。那么MyBatis框架会把每一个节点(如:select节点、delete节点)生成一个MappedStatement类。在SqlCommand中,要找到MappedStatement类就必须通过接口类全限定名+id(方法名)来获得。
以selectOne()为例,看下底层是怎么执行?
1、selectOne()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
//会调用selectList()方法
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
//只返回一个,取第一个即可
return list.get(0);
} else if (list.size() > 1) {
//这是MyBatis中很常见的一种异常
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}

2、selectList()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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 {
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();
}
}

Executor是一个非常重要的接口:
excutor
其中BaseExecutor是一个抽象实现类,这里也使用了模板设计模式,这里将一些共性抽取到了BaseExecutor中。
进入了org.apache.ibatis.executor.BaseExecutor#query

BoundSql是对SQL语句的封装,ms(MapperStatment)由前面接口类全限定名+接口方法从Configuration对象中获取。
继续看下一步的query方法

执行querFormData()

localCache会先putObject()一下,这行代码的意义是声明一个占位符,当发送一次查询数据的请求时,设置该占位符告诉其他请求正在查询数据库,请其他请求先阻塞或休眠。当这次请求查询到数据之后,将真正的数据放到占位符的位置,缓存数据。如果其他请求与该次请求查询的数据时一样的,直接从一级缓存中拿数据减少了查询请求对数据库的压力 (org.apache.ibatis.executor.BaseExecutor.DeferredLoad#load org.apache.ibatis.executor.BaseExecutor.DeferredLoad#canLoad),接下来会执行doQuery()方法,doQuery()方法是BaseExecutor中的一个模板方法:

这里会有一个拦截器链去执行Plugins的拦截:

当sqlsessionFactory获取sqlsession时,产生的ParameterHandler、ResultSetHandler、StatementHandler、Executor都是由org.apache.ibatis.session.Configuration 类的方法 newParameterHandler、newResultSetHandler、newStatementHandler、newExecutor产生的代理对象,而这些代理对象是经过适用规则的plugin层层代理的 ,最后会返回一个StatementHandler。

在prepareStatement()方法中,获取了Connection:

接下来会执行handler.query(stmt, resultHandler):

返回结果处理

执行完成后会由ResultSetHandler处理结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public List<Object> handleResultSets(Statement stmt) throws SQLException {
//又看到了熟悉的ErrorContext,不过这个activity的名称与之前的不一样
ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

final List<Object> multipleResults = new ArrayList<Object>();

int resultSetCount = 0;
ResultSetWrapper rsw = getFirstResultSet(stmt);

List<ResultMap> resultMaps = mappedStatement.getResultMaps();
int resultMapCount = resultMaps.size();
validateResultMapsCount(rsw, resultMapCount);
while (rsw != null && resultMapCount > resultSetCount) {
ResultMap resultMap = resultMaps.get(resultSetCount);
//循环封装好每个查询结果对象
handleResultSet(rsw, resultMap, multipleResults, null);
rsw = getNextResultSet(stmt);
cleanUpAfterHandlingResultSet();
resultSetCount++;
}

String[] resultSets = mappedStatement.getResultSets();
if (resultSets != null) {
while (rsw != null && resultSetCount < resultSets.length) {
ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
if (parentMapping != null) {
String nestedResultMapId = parentMapping.getNestedResultMapId();
ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
handleResultSet(rsw, resultMap, null, parentMapping);
}
rsw = getNextResultSet(stmt);
cleanUpAfterHandlingResultSet();
resultSetCount++;
}
}

return collapseSingleResultList(multipleResults);
}

由handleResultSet()方法逐层深入最后来到getRowValue()方法。

执行org.apache.ibatis.executor.resultset.DefaultResultSetHandler#createResultObject(org.apache.ibatis.executor.resultset.ResultSetWrapper, org.apache.ibatis.mapping.ResultMap, org.apache.ibatis.executor.loader.ResultLoaderMap, java.lang.String)方法,首先会循环遍历为ResultMapping属性赋值。如果是嵌套查询而且配置了延迟加载,其中这里的createProxy()方法会生成一个具有延迟加载功能的代理对象(这里采用CGLIB代理生成!!!)

这里返回的是一个所有属性都为空的结果:

需要在前文getRowValue()中,给各个属性赋值:

大体的执行过程如下:

整体过程

参考资料