Mybatis Provider注解中的method属性详解

环境

MyBatis 3.5.1

结论

  1. 当声明了method属性时,会调用method属性值对应的方法
@DeleteProvider(type = TestProvider.class, method = "abc")
int delete(int id);

class TestProvider {
	public String abc(int id) {
		//...
	}
}
  1. 如果未声明method属性,且type属性声明的类未实现ProviderMethodResolver接口,则会调用Provider中名为provideSql的方法
@DeleteProvider(type = TestProvider.class)
int delete(int id);

class TestProvider {
	public String provideSql(int id) {
		//...
	}
}
  1. 如果未声明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接口后会优先寻找方法名相同的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值