MyBatis 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="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
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="org.mybatis.spring.sample.mapper" />
</bean>
这段配置会扫描org.mybatis.spring.sample.mapper下的所有接口,然后创建各自接口的动态代理类。
 
MapperScannerConfigurer底层代码分析
 
以以下代码为示例进行讲解(部分代码,其他代码及配置省略):
 
1 package org.format.dynamicproxy.mybatis.dao;
2 public interface UserDao {
3     public User getById(int id);
4     public int add(User user);    
5     public int update(User user);    
6     public int delete(User user);    
7     public List<User> getAll();    
8 }
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.format.dynamicproxy.mybatis.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
我们先通过测试用例debug查看userDao的实现类到底是什么。
 
我们可以看到,userDao是1个MapperProxy类的实例。
看下MapperProxy的源码,没错,实现了InvocationHandler,说明使用了jdk自带的动态代理。
 
 1 public class MapperProxy<T> implements InvocationHandler, Serializable {
 2  
 3   private static final long serialVersionUID = -6424540398559729838L;
 4   private final SqlSession sqlSession;
 5   private final Class<T> mapperInterface;
 6   private final Map<Method, MapperMethod> methodCache;
 7  
 8   public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
 9     this.sqlSession = sqlSession;
10     this.mapperInterface = mapperInterface;
11     this.methodCache = methodCache;
12   }
13  
14   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
15     if (Object.class.equals(method.getDeclaringClass())) {
16       try {
17         return method.invoke(this, args);
18       } catch (Throwable t) {
19         throw ExceptionUtil.unwrapThrowable(t);
20       }
21     }
22     final MapperMethod mapperMethod = cachedMapperMethod(method);
23     return mapperMethod.execute(sqlSession, args);
24   }
25  
26   private MapperMethod cachedMapperMethod(Method method) {
27     MapperMethod mapperMethod = methodCache.get(method);
28     if (mapperMethod == null) {
29       mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
30       methodCache.put(method, mapperMethod);
31     }
32     return mapperMethod;
33   }

 

 
posted @ 2016-03-10 13:02  赫尔修斯  阅读(341)  评论(0编辑  收藏  举报