聊聊Mybatis的binding模块之MapperMethod
聊聊Mybatis的binding模块之MapperMethod
Mybatis的绑定真正操作是通过MapperProxy来调用MapperMethod的execute()方法
构造方法
先看一下MapperMethod的构造方法:
public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
this.command = new SqlCommand(config, mapperInterface, method);
this.method = new MethodSignature(config, mapperInterface, method);
}
构造方法中填充了command属性和method属性,下面我们就看一下这两个属性的填充
SqlCommand
command属性填充的时候通过创建SqlCommand对象来完成。
SqlCommand是用来记录sql语句的唯一标识和sql语句的类型:UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH
我们看一下SqlCommand类在创建的时候进行了哪些操作,也就是它的构造方法的实现逻辑。
SqlCommand类的构造方法:
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
configuration);
if (ms == null) {
if (method.getAnnotation(Flush.class) != null) {
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
} else {
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}
- 获取Mapper接口对应的方法名
- 获取Mapper接口的类型
- 调用resolveMappedStatement()方法返回MappedStatement,MappedStatement是xml文件中的sql解析得到的对象,id是接口名+方法名
- 设置name 和type
可以看出SqlCommand类的构造方法主要是对一些属性的填充
而MapperMethod类中保存的是方法的一些信息,下面我们看看它这个类,重点在于下面的执行方法
MapperMethod的执行方法
上面我们说到通过MapperProxy来调用MapperMethod的execute()方法,我们就来看一下MapperMethod的execute()方法
MapperMethod的execute()方法:
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
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()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
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;
}
代码看着比较长,但整体的逻辑是很好理解的,下面就带大家分析一下
- 根据sql语句的类型进行调用,对应INSERT、UPDATE、DELETE,逻辑都差不多,都是调用convertArgsToSqlCommandParam()方法处理参数,然后调用sqlSession来进行执行sql最后返回的结果通过rowCountResult来进行处理
- 对于SELECT语句,根据方法的返回类型选择不同的execute方法执行,最后都是调用SqlSession中的方法
总结
至此,mybatis的binding模块差不多分析完了,大体流程就是MapperRegistry根据不同的Mapper接口获取MapperProxyFactory的实例,然后调用newInstance()方法,利用MapperProxy代理类获取Mapper接口的动态代理对象,最终调用MapperMethod的execute()传入参数和封装的sql对象信息,执行sql语句
- 点赞
- 收藏
- 关注作者
评论(0)