Mybatis之方法如何映射到XML

本文详细探讨了Mybatis中方法与XML的映射关系,包括方法名映射、方法签名及参数映射。方法名通过接口名+方法名的形式与XML中的namespace+statementID对应。方法签名涉及返回值类型、参数等信息。参数映射中,Mybatis提供了多种key值选择,如注解、实际参数名或下标,以便灵活地在XML中配置。
摘要由CSDN通过智能技术生成

上文Mybatis之Mapper接口如何执行SQL中了解到,Mapper通过动态代理的方式执行SQL,但是并没有详细的介绍方法是如何做映射的,方法包括:方法名,返回值,参数等;这些都是如何同xxMapper.xml进行关联的。
方法名映射
上文中提到缓存MapperMethod的目的是因为需要实例化SqlCommand和MethodSignature两个类,而这两个类实例化需要花费一些时间;而方法名的映射就在实例化SqlCommand的时候,具体可以看构造方法:
private final String name;
private final SqlCommandType type;

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);
    }
  }
}

复制代码首先获取了方法的名称和定义此方法的类,一般此类都是xxxMapper类;注此处的declaringClass类和mapperInterface是有区别的,主要是因为此方法可以是父类里面的方法,而mapperInterface是子类;所以如果定义了父xxxMapper,同样也能进行映射,所以可以看相关代码:
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
String statementId = mapperInterface.getName() + “.” + methodName;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
return null;
}
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
if (declaringClass.isAssignableFr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值