mybatis动态代理

1.用户使用

1.提供 xxMapper接口

public interface UserMapper {
    @Select(value = "select * from user where id=#{id}")//(1)注解方式
    public User getUser(Long id);
}

2.在xml里配置(可选配置)(2)


    

2.mybatis实现

在调用sqlSession.getMapper(Class)发生了一些神奇的事情

DefaultSqlSession.getMapper(type)
    Configuration.getMapper(type,sqlSession)
        MapperRegistry.getMapper(type,sqlSession) //存放键值对type(接口)-MapperProxyFactory
            MapperProxyFactory.newInstance(sqlSession)
                mapperProxy = new MapperProxy(sqlSession, mapperInterface, methodCache);//代理对象
                MapperProxyFactory.newInstance(mapperProxy)
                    Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy)//生成了一个代理实例,返回

1.实现了对应的接口
mapperProxy = new MapperProxy(sqlSession, mapperInterface, methodCache);
2.使用Proxy生成了一个代理实例
Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy)

动态代理实现

MapperProxy 实现InvocationHandler
在invoke方法中从sqlSession的configuration中获取sql语句,
根据sql语句生成方法(jdbc的prepareStatement)

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);//从sqlSession的configuration中获取sql语句,根据sql语句生成方法(jdbc的prepareStatement)
    return mapperMethod.execute(sqlSession, args);//执行映射成的方法
  }

你可能感兴趣的:(mybatis动态代理)