mybatis动态代理

1.用户使用

1.提供 xxMapper接口

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

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

<mapper namespace="org.wangying.dao.UserDao">
    <select id="getUser" resultType="org.wangying.entity.User" parameterType="Long">
        select * from user where id=#{id}
    </select>
</mapper>

2.mybatis实现

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

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

1.实现了对应的接口
mapperProxy = new MapperProxy<T>(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);//执行映射成的方法
  }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容