mybatis学习记录(一)

TestMapper mapper1 = sqlSession.getMapper(TestMapper.class);
TestMapper mapper2 = sqlSession.getMapper(TestMapper.class);
System.out.println(mapper1 == mapper2);

读mybatis源码过程中,产生了一个想法,sqlSession作为一个会话,也可以说说一个门面,其中集成了很多的针对数据库的操作接口,也就是很多方法,以及在创建过程中,将Configuration传入了进来。
那么产生了这么一个疑问,在同一个会话中,如果我对同一个mapper接口获取两次mapper代理类对象,那么两次获取的代理类对象是同一个吗?
手动做了一下实验,发现为上图的运行结果为false,随即对mapper代理对象的构建过程进行了跟踪,其实过程也很简单:

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

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



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


  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

从mybatis的源码可以看出:
1、sqlsession调用了configuration的getMapper方法
2、configuration调用了MapperRegistry的getMapper方法
3、MapperRegistry中存放了MapperProxyFactory的缓存
4、最终通过MapperProxyFactory的newInstance方法来获取一个mapper代理对象,获取的最终过程还是通过java的动态代理,并且每次获取都是重新生成了一个新的代理对象。
5、sqlSession中并没有缓存生成的mapper代理对象,为什么不考虑在会话中设置一个缓存mapper代理对象的缓存呢?也许是觉得没有必要吧,毕竟一个会话中多次获取同一个mapper代理对象的机会也不多。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值