温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring与Mybatis整合的MapperScannerConfigurer怎么用

发布时间:2021-12-30 09:30:03 来源:亿速云 阅读:330 作者:小新 栏目:云计算

这篇文章将为大家详细讲解有关Spring与Mybatis整合的MapperScannerConfigurer怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

MapperScannerConfigurer介绍

MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一个类。

想要了解该类的作用,就得先了解MapperFactoryBean。

MapperFactoryBean的出现为了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate编写数据访问对象(DAO)的代码,使用动态代理实现。

比如下面这个官方文档中的配置:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value=http://www.cnblogs.com/fangjian0423/p/"org.mybatis.spring.sample.mapper.UserMapper" />

org.mybatis.spring.sample.mapper.UserMapper是一个接口,我们创建一个MapperFactoryBean实例,然后注入这个接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean会使用SqlSessionFactory创建SqlSession)这两个属性。

之后想使用这个UserMapper接口的话,直接通过spring注入这个bean,然后就可以直接使用了,spring内部会创建一个这个接口的动态代理。

当发现要使用多个MapperFactoryBean的时候,一个一个定义肯定非常麻烦,于是mybatis-spring提供了MapperScannerConfigurer这个类,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value=http://www.cnblogs.com/fangjian0423/p/"org.mybatis.spring.sample.mapper" />

这段配置会扫描org.mybatis.spring.sample.mapper下的所有接口,然后创建各自接口的动态代理类。

MapperScannerConfigurer底层代码分析

以以下代码为示例进行讲解(部分代码,其他代码及配置省略):

package org.format.dynamicproxy.mybatis.dao;
public interface UserDao {
    public User getById(int id);
    public int add(User user);    
    public int update(User user);    
    public int delete(User user);    
    public List<User> getAll();    
}

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value=http://www.cnblogs.com/fangjian0423/p/"org.format.dynamicproxy.mybatis.dao"/>

我们先通过测试用例debug查看userDao的实现类到底是什么。
Spring与Mybatis整合的MapperScannerConfigurer怎么用
我们可以看到,userDao是1个MapperProxy类的实例。
看下MapperProxy的源码,没错,实现了InvocationHandler,说明使用了jdk自带的动态代理。

public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialversionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperinterface;
  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;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

}

下面开始分析MapperScannerConfigurer的源码

MapperScannerConfigurer实现了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一个可以修改spring工长中已定义的bean的接口,该接口有个postProcessBeanDefinitionRegistry方法。
Spring与Mybatis整合的MapperScannerConfigurer怎么用

然后我们看下ClassPathMapperScanner中的关键是如何扫描对应package下的接口的。
Spring与Mybatis整合的MapperScannerConfigurer怎么用

其实MapperScannerConfigurer的作用也就是将对应的接口的类型改造为MapperFactoryBean,而这个MapperFactoryBean的属性mapperInterface是原类型。MapperFactoryBean本文开头已分析过。

所以最终我们还是要分析MapperFactoryBean的实现原理!

MapperFactoryBean继承了SqlSessionDaoSupport类,SqlSessionDaoSupport类继承DaoSupport抽象类,DaoSupport抽象类实现了InitializingBean接口,因此实例个MapperFactoryBean的时候,都会调用InitializingBean接口的afterPropertiesSet方法。

DaoSupport的afterPropertiesSet方法:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
MapperFactoryBean重写了checkDaoConfig方法:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
然后通过spring工厂拿对应的bean的时候:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
这里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
Configuration的getMapper方法,会使用MapperRegistry的getMapper方法:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
MapperRegistry的getMapper方法:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
MapperProxyFactory构造MapperProxy:
Spring与Mybatis整合的MapperScannerConfigurer怎么用
没错! MapperProxyFactory就是使用了jdk组带的Proxy完成动态代理。
MapperProxy本来一开始已经提到。MapperProxy内部使用了MapperMethod类完成方法的调用:
Spring与Mybatis整合的MapperScannerConfigurer怎么用

下面,我们以UserDao的getById方法来debug看看MapperMethod的execute方法是如何走的。

@Test
public void testGet() {
    int id = 1; system.out.println(userDao.getById(id));
}
<select id="getById">

Spring与Mybatis整合的MapperScannerConfigurer怎么用
Spring与Mybatis整合的MapperScannerConfigurer怎么用

关于“Spring与Mybatis整合的MapperScannerConfigurer怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI