(源码解析)mybatis调用链之获取对应mapper动态代理对象

调用SqlSession的getMapper方法,获取对应mapper。

这里以SqlSession实现类DefaultSqlSession为例,

DefaultSqlSession

......

@Override
  public <T> T getMapper(Class<T> type) {
    return configuration.<T>getMapper(type, this);
  }

......

调用Configuration的getMapper方法,

Configuration

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

从Configuration的mapper接口的动态代理注册中心MapperRegistry中获取,调用MapperRegistry的getMapper方法。

MapperRegistry

......

//记录了mapper接口与对应MapperProxyFactory之间的关系
  private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
  
......

@SuppressWarnings("unchecked")
  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    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);
    }
  }
 ......

可以看出,getMapper方法从knownMappers中获取到MapperProxyFactory对象,调用MapperProxyFactory的newInstance方法。

MapperProxyFactory

//mapper接口的class对象
  private final Class<T> mapperInterface;
//key是mapper接口中的某个方法的method对象,value是对应的MapperMethod,MapperMethod对象不记录任何状态信息,所以它可以在多个代理对象之间共享
  private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>();

......

public T newInstance(SqlSession sqlSession) {
     //每次调用都会创建新的MapperProxy对象
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

初始化MapperProxy实例对象,MapperProxy类实现了InvocationHandler接口,它是增强mapper接口的实现

MapperProxy


  private final SqlSession sqlSession;//记录关联的sqlsession对象
  private final Class<T> mapperInterface;//mapper接口对应的class对象;
//key是mapper接口中的某个方法的method对象,value是对应的MapperMethod,MapperMethod对象不记录任何状态信息,所以它可以在多个代理对象之间共享
  private final Map<Method, MapperMethod> methodCache;

  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }
  
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {//如果是Object本身的方法不增强
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    //从缓存中获取mapperMethod对象,如果缓存中没有,则创建一个,并添加到缓存中
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //调用execute方法执行sql
    return mapperMethod.execute(sqlSession, args);
  }
  
  ......

最终调用MapperProxyFactory 的 protected T newInstance(MapperProxy mapperProxy)方法,创建mapper接口的动态代理对象

MapperProxyFactory

@SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    //创建实现了mapper接口的动态代理对象
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过两种方式来获取MyBatisMapper对象。一种方式是通过@Autowired注解来自动装配Mapper对象,另一种方式是通过静态方法来手动获取Mapper对象。 首先,使用@Autowired注解的方法可以实现自动装配,简化了获取Mapper对象的过程,代码如下: ```java @Autowired private MyMapper myMapper; // 使用myMapper对象进行数据库操作 ``` 在上述代码中,通过@Autowired注解将MyMapper接口的实现类自动注入到myMapper对象中,然后就可以直接使用myMapper对象进行数据库操作,无需手动获取。 另一种方式是通过静态方法来手动获取Mapper对象,代码如下: ```java @Configuration @MapperScan("com.example.mapper") // 指定Mapper接口所在的包 public class MyMapperConfig { private static MyMapper myMapper; @Autowired public void setMyMapper(MyMapper myMapper) { MyMapperConfig.myMapper = myMapper; } public static MyMapper getMyMapper() { return myMapper; } } ``` 在上述代码中,通过@Configuration注解将该类标记为配置类,并通过@MapperScan注解指定Mapper接口所在的包。然后通过@Autowired注解将MyMapper接口的实现类自动注入到静态的myMapper对象中,在静态的getMyMapper方法中返回该对象。 这样,在其他地方调用MyMapperConfig.getMyMapper()方法即可获取Mapper对象,然后就可以使用Mapper对象进行数据库操作。 以上就是通过静态方法获取MyBatis Mapper对象的方式,可以根据具体需求选择适合的方式来获取Mapper对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值