spring集成mybatis,配置多個數據源並自動切換。
spring-mybatis.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" > <list> <!--<value>classpath:db.properties </value>--> <value>classpath:generator.properties </value> <value>classpath:db-test.properties</value> </list> </property> </bean> <!--添加多個數據源--> <bean id="vbaseDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${baseUrl}" /> <property name="username" value="${baseUser}" /> <property name="password" value="${basePassword}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <bean id="vso1DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${vso1Url}" /> <property name="username" value="${vso1User}" /> <property name="password" value="${vso1Password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!--添加多個數據源--> <bean id="multipleDataSource" class="com.cmsz.crm.db.MultipleDataSource"> <property name="defaultTargetDataSource" ref="vso1DataSource"/> <property name="targetDataSources"> <map> <entry key="vbaseDataSource" value-ref="vbaseDataSource"/> <entry key="vso1DataSource" value-ref="vso1DataSource"/> </map> </property> </bean> <!-- spring和MyBatis整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/cmsz/crm/mapper/*.xml"/> <!-- 配置分頁插件 --> <property name="plugins"> <array> <!--pagehelper5.0版本需要設置成PageInterceptor,dialect改成helperDialect--> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true </value> </property> </bean> </array> </property> </bean> <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!--新建MapperFactoryBean實例,繼承自baseMapper,指定dao層的接口--> <bean id="workOrderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" parent="baseMapper"> <property name="mapperInterface" value="com.cmsz.crm.dao.WorkOrderDao" /> </bean> <bean id="esbTraceMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" parent="baseMapper"> <property name="mapperInterface" value="com.cmsz.crm.dao.EsbTraceDao" /> </bean> <!-- (事務管理)transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="multipleDataSource" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類,並將其定義為一個Spring Bean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cmsz.crm.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
spring-mvc配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!--處理靜態資源--> <mvc:default-servlet-handler/> <!--掃描目錄--> <context:component-scan base-package="com.cmsz.crm" /> <!--注解驅動--> <mvc:annotation-driven/> <!--aop相關--> <aop:aspectj-autoproxy /> <!--避免IE執行AJAX時,返回JSON出現下載文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --> </list> </property> </bean> <!-- 定義跳轉的文件的前后綴 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這里的配置我的理解是自動給后面action的方法return的字符串加上前綴和后綴,變成一個 可用的url地址 --> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當然如果不配,那么配置文件中也不必引入上傳組件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認編碼 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 內存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
Dao層如下:
EsbTraceDao如下:
public interface EsbTraceDao { List<EsbTrace> getTraceListByTime( @Param("startTime") String startTime,@Param("endTime") String endTime); }
WorkOrderDao如下:
public interface WorkOrderDao { WorkOrder selectByPrimaryKey(@Param("id") Long id); }
Service層的如下:
EsbTraceServiceImpl.java如下:
注意,要在@Service注解后方寫上Service名稱,這樣方便在測試代碼中調用。
@Service("esbTraceService") public class EsbTraceServiceImpl implements EsbTraceService { @Autowired private EsbTraceDao esbTraceDao; @Override public List<EsbTrace> getTraceListByTime( String startTime, String endTime) { return esbTraceDao.getTraceListByTime( startTime, endTime); } }
WorkServiceImpl如下:
@Service("workOrderService") public class WorkOrderServiceImpl implements WorkOrderService{ @Autowired private WorkOrderDao workOrderDao; @Override public WorkOrder selectByPrimaryKey(Long id) { return workOrderDao.selectByPrimaryKey(id); } }
以上是基本的文件,接下來的是數據源相關的設置。
通過繼承AbstractRoutingDataSource,來設置DataSourceKey。
public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> threadLocaldataSource = new InheritableThreadLocal<String>(); public static void setDataSourceKey(String dataSource) { threadLocaldataSource.set(dataSource); } @Override protected Object determineCurrentLookupKey() { return threadLocaldataSource.get(); } }
通過aop自動切換數據源,如下:
aop依賴包的導入,aop注解,以及表達式的理解,可以參考博客:
https://blog.csdn.net/yerenyuan_pku/article/details/52865330
@Component @Aspect @Order(0) //使aop的啟動順序先於事務。這樣能在事務之前切換數據源。此處未經驗證,存疑。 public class MultipleDataSourceAspectAdvice { public MultipleDataSourceAspectAdvice(){ System.out.println("==========================>MultipleDataSourceAspectAdvice構造函數。"); } @Pointcut("execution (* com.cmsz.crm.dao.*.*(..))") private void cutMethod() {} // 聲明一個切入點,cutMethod為切入點名稱 //攔截對應的切入點 @Around("cutMethod()") public Object doAround(ProceedingJoinPoint jp) throws Throwable { if (jp.getTarget() instanceof EsbTraceDao) { MultipleDataSource.setDataSourceKey("vbaseDataSource"); } else if (jp.getTarget() instanceof WorkOrderDao) { MultipleDataSource.setDataSourceKey("vso1DataSource"); }else { System.out.println("==================>proceedingJoinPoint.getTarget():"+jp.getTarget()); } return jp.proceed(); } }
通過Service層操作的測試代碼如下:
public class SpringTest { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"spring-mvc.xml","spring-mybatis.xml"}); //獲取Servicen層的對象 WorkOrderService workOrderService=(WorkOrderService) applicationContext.getBean("workOrderService"); EsbTraceService esbTraceService=(EsbTraceService)applicationContext.getBean("esbTraceService"); //下面這行用來手動切換數據源,注釋后通過MultipleDataSourceAspectAdvice中的aop自動切換數據源 // MultipleDataSource.setDataSourceKey("vso1DataSource"); WorkOrder workOrder= workOrderService.selectByPrimaryKey(100000004220380L); System.out.println("===================>BillId:"+workOrder.getBillId()); System.out.println("===================>自動切換數據源查詢."); List<EsbTrace> esbTraceList= esbTraceService.getTraceListByTime("ESB_TRACE_20180528", "2018-05-28 09:00:00","2018-05-28 23:59:59"); for(EsbTrace esbTrace: esbTraceList) { System.out.println( esbTrace.getMondId()+" "+esbTrace.getInterfaceCode()); } } }
注意:
以上測試代碼中是通過Service層去操作,如果想直接通過dao層接口去操作,
那么mybatis.xml的配置中,MapperFactoryBean類的實例bean的id要和Dao層接口名一致。 如下示:
<bean id="workOrderDao" class="org.mybatis.spring.mapper.MapperFactoryBean" parent="baseMapper"> <property name="mapperInterface" value="com.cmsz.crm.dao.WorkOrderDao" /> </bean>
通過Dao層直接訪問的測試代碼如下:
public class WorkOrderTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"spring-mvc.xml","spring-mybatis.xml"});
WorkOrderDao workOrderDao= applicationContext.getBean(WorkOrderDao.class);
WorkOrder workOrder= workOrderDao.selectByPrimaryKey(100000004220380L);
System.out.println("===================>BillId:"+ workOrder.getBillId());
}
}
參考博客:
https://www.cnblogs.com/lzrabbit/p/3750803.html
https://blog.csdn.net/yizhenn/article/details/52384607
https://blog.csdn.net/yerenyuan_pku/article/details/52865330