一:简述
本文通过分析mybatis的源码来展示mybatis是如何帮我们实现一个查询的方法的。
二:时序图
三:源码分析
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
注:这里获取的是一个代理对象,而我们一般从Spring容器中获取的也是同样的代理对象,只不过是交给Spring管理了而已
1.获取代理类Mapper
sqlSession的getMapper()方法会先调用configuration的getMapper()方法,最后调用mapperRegistry.getMapper()
public <T> T getMapper(Class<T> type) {
//最后会去调用MapperRegistry.getMapper
return configuration.<T>getMapper(type, this);
}
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
在我们解析配置的时候,会将解析好的Mapper接口和对应的代理工厂关联起来,保存在mapperRegistry的knownMappers中。而mapperRegistry的getMapper方法就是从这个knownMappers的map中获取工厂对象,然后利用工厂创建代理类并且返回。
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//knownMapper是一个map key是Mapper接口的类对象,
//value是对应的代理工厂 用来创建代理对象的
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);
}
}
代理工厂创建代理类:
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
protected T newInstance(MapperProxy<T> mapperProxy) {
//用JDK自带的动态代理生成映射器
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] {
mapperInterface }, mapperProxy);
}
所以返回的对象应该是一个MapperProxy对象,而我们在调用mapper的方法的时候应该也是进入到MapperProxy的invoke()方法。
2.执行查询方法
根据上面的分析,我们会调用MapperProxy的invoke()方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//代理以后,所有Mapper的方法调用时,都会调用这个invoke方法
//并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是Object中通用的方法(toString、hashCode等)无需执行
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
//获取MapperMethod MapperMethod会执行代理的一些逻辑
final MapperMethod mapperMethod = cachedMapperMethod(method);
//执行
return mapperMethod.execute(sqlSession, args);
}
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
//可以看到执行时就是4种情况,insert|update|delete|select,
//分别调用SqlSession的4大类方法 我们这里就以查询为例
//查询是最复杂的 因为还需要对结果集进行处理
if (SqlCommandType.INSERT == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
} else if (SqlCommandType.UPDATE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
} else if (SqlCommandType.DELETE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
} else if (SqlCommandType.SELECT == command.getType()) {
if (method.returnsVoid() && method.hasResultHandler()) {
//如果有结果处理器
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
//如果结果有多条记录
result = executeForMany(sqlSession, args)