這篇文章將為大家詳細講解有關Spring與Mybatis整合的MapperScannerConfigurer怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創新互聯主營尖山網站建設的網絡公司,主營網站建設方案,重慶App定制開發,尖山h5小程序制作搭建,尖山網站營銷推廣歡迎尖山等地區企業咨詢
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下的所有接口,然后創建各自接口的動態代理類。
以以下代碼為示例進行講解(部分代碼,其他代碼及配置省略):
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的實現類到底是什么。
我們可以看到,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實現了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一個可以修改spring工長中已定義的bean的接口,該接口有個postProcessBeanDefinitionRegistry方法。
然后我們看下ClassPathMapperScanner中的關鍵是如何掃描對應package下的接口的。
其實MapperScannerConfigurer的作用也就是將對應的接口的類型改造為MapperFactoryBean,而這個MapperFactoryBean的屬性mapperInterface是原類型。MapperFactoryBean本文開頭已分析過。
所以最終我們還是要分析MapperFactoryBean的實現原理!
MapperFactoryBean繼承了SqlSessionDaoSupport類,SqlSessionDaoSupport類繼承DaoSupport抽象類,DaoSupport抽象類實現了InitializingBean接口,因此實例個MapperFactoryBean的時候,都會調用InitializingBean接口的afterPropertiesSet方法。
DaoSupport的afterPropertiesSet方法:
MapperFactoryBean重寫了checkDaoConfig方法:
然后通過spring工廠拿對應的bean的時候:
這里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:
Configuration的getMapper方法,會使用MapperRegistry的getMapper方法:
MapperRegistry的getMapper方法:
MapperProxyFactory構造MapperProxy:
沒錯! MapperProxyFactory就是使用了jdk組帶的Proxy完成動態代理。
MapperProxy本來一開始已經提到。MapperProxy內部使用了MapperMethod類完成方法的調用:
下面,我們以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怎么用
分享路徑:http://vcdvsql.cn/article28/pejscp.html
成都網站建設公司_創新互聯,為您提供ChatGPT、動態網站、定制網站、全網營銷推廣、微信公眾號、移動網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯