环境
MyBatis 3.5.1
结论
- 当声明了method属性时,会调用method属性值对应的方法
@DeleteProvider(type = TestProvider.class, method = "abc")
int delete(int id);
class TestProvider {
public String abc(int id) {
//...
}
}
- 如果未声明method属性,且type属性声明的类未实现ProviderMethodResolver接口,则会调用Provider中名为provideSql的方法
@DeleteProvider(type = TestProvider.class)
int delete(int id);
class TestProvider {
public String provideSql(int id) {
//...
}
}
- 如果未声明method属性,且type属性声明的类实现了ProviderMethodResolver接口,则会调用被Provider注解的方法的同名方法
@DeleteProvider(type = TestProvider.class)
int delete(int id);
class TestProvider implements ProviderMethodResolver {
public String delete(int id) {
//...
}
}
详解
在使用Provider注解时,发现method属性不是必须的,查阅MyBatis API后发现
Since 3.5.1, you can omit method attribute, the MyBatis will resolve a target method via the ProviderMethodResolver interface. If not resolve by it, the MyBatis use the reserved fallback method that named provideSql
从3.5.1版本开始,可以省略method属性,MyBatis将通过ProviderMethodResolver接口解析目标方法,如果解析失败,MyBatis使用名为provideSql的预留备用方法
查看ProviderMethodResolver接口的源码,内含一个resolveMethod方法
default Method resolveMethod(ProviderContext context) {
//选取实现类中与被注解方法相同名称的方法
List<Method> sameNameMethods = Arrays.stream(getClass().getMethods())
.filter(m -> m.getName().equals(context.getMapperMethod().getName()))
.collect(Collectors.toList());
if (sameNameMethods.isEmpty()) {
//如果不存在相同名称的方法,抛出异常,猜测被上层捕获后去寻找名为provideSql的方法
throw new BuilderException("Cannot resolve the provider method because '"
+ context.getMapperMethod().getName() + "' not found in SqlProvider '" + getClass().getName() + "'.");
}
//在相同名称的方法中继续寻找返回值是CharSequence或其的子类的方法
List<Method> targetMethods = sameNameMethods.stream()
.filter(m -> CharSequence.class.isAssignableFrom(m.getReturnType()))
.collect(Collectors.toList());
if (targetMethods.size() == 1) {
//筛选后方法唯一,找到了
return targetMethods.get(0);
}
if (targetMethods.isEmpty()) {
//没有符合条件的方法
throw new BuilderException("Cannot resolve the provider method because '"
+ context.getMapperMethod().getName() + "' does not return the CharSequence or its subclass in SqlProvider '"
+ getClass().getName() + "'.");
} else {
//有多个符合条件的方法
throw new BuilderException("Cannot resolve the provider method because '"
+ context.getMapperMethod().getName() + "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
}
}
由此可知,未指定method时,Provider实现ProviderMethodResolver接口后会优先寻找方法名相同的方法。