分兩種方式,手動和自動切換,mysql
前提:使用mybatis自動生成工具生成相關xml,實體類,接口等,spring框架,依賴的jar包都已引入spring
1.spring基礎配置以下:sql
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--6 容器自動掃描IOC組件 --> <context:component-scan base-package="com.vip"></context:component-scan> <!--1 引入屬性文件,在配置中佔位使用 --> <context:property-placeholder location="classpath*:dbt.properties" /> <!-- 配置數據源Master --> <bean name="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.uid}" /> <property name="password" value="${jdbc.pwd}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 鏈接池最大空閒 --> <property name="maxIdle" value="20" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> </bean> <!-- 配置數據源Slave --> <bean name="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc2.url}" /> <property name="username" value="${jdbc2.uid}" /> <property name="password" value="${jdbc2.pwd}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 鏈接池最大空閒 --> <property name="maxIdle" value="20" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> </bean> <bean id="dataSource" class="com.vip.inventory.mybaisc.ThreadLocalRountingDataSource"> <property name="defaultTargetDataSource" ref="dataSourceMaster" /> <property name="targetDataSources"> <map key-type="com.vip.inventory.mybaisc.DataSources"> <entry key="MASTER" value-ref="dataSourceMaster" /> <entry key="SLAVE" value-ref="dataSourceSlave" /> <!-- 這裏還能夠加多個dataSource --> </map> </property> </bean> <!--3 會話工廠bean sqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 實體類別名 --> <property name="typeAliasesPackage" value="com/vip/inventory/entity"></property> <!-- sql映射文件路徑 --> <property name="mapperLocations" value="classpath*:mapper/**/*Mapper.xml"></property> </bean> <!--4 自動掃描對象關係映射 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定會話工廠,若是當前上下文中只定義了一個則該屬性可省去 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 指定要自動掃描接口的基礎包,實現接口 --> <property name="basePackage" value="com.vip"></property> </bean> <!--5 聲明式事務管理 --> <!--定義事物管理器,由spring管理事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--支持註解驅動的事務管理,指定事務管理器 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!--7 aspectj支持自動代理實現AOP功能 --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
2.數據源配置文件:數據庫
#mysql jdbc jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://ip/test1?useUnicode=true&characterEncoding=UTF-8 jdbc.uid=uid jdbc.pwd=pa #galaxy jdbc jdbc2.driver=com.mysql.jdbc.Driver jdbc2.url=jdbc:mysql://ip/test2?useUnicode=true&characterEncoding=UTF-8 jdbc2.uid=uid jdbc2.pwd=pa
3.新建枚舉類:mybatis
package com.inventory.mybaisc;
public enum DataSources {
MASTER, SLAVE
}
注意此枚舉值必須對應spring配置文件各數據源的別名app
4.新建DataSourceTypeManager類,具體執行數據源切換框架
public class DataSourceTypeManager { private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){ @Override protected DataSources initialValue(){ return DataSources.MASTER; } }; public static DataSources get(){ return dataSourceTypes.get(); } public static void set(DataSources dataSourceType){ dataSourceTypes.set(dataSourceType); } public static void reset(){ dataSourceTypes.set(DataSources.MASTER); } }
5.新建ThreadLocalRountingDataSource繼承AbstractRoutingDataSource,實現切換ide
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DataSourceTypeManager.get(); } }
一:執行具體mybatis接口時手動切換方式:工具
在service層具體調用insert接口進行數據庫操做前加入:ui
DataSourceTypeManager.set(DataSources.MASTER);
則,在執行insert時會切換數據源到master
insert方法爲mybatic自動生成的接口,具體以下:
public interface InventoryMapper { int deleteByPrimaryKey(Long id); int deleteByItemKey(String item); int insert(Inventory record); int insertSelective(Inventory record); Inventory selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(Inventory record); int updateByPrimaryKey(Inventory record); }
二:動態切換mybatic數據源
經過增長一個切面去攔截servcie層在調用mybatis生成的接口時,來切換數據源,從而省去每次調用mybatis生成的接口時都要手動註明數據源
AOP代碼以下:
@Aspect // for aop @Component // for auto scan @Order(0) // execute before @Transactional public class DynamicChangeDbSource { @Pointcut("execution(public * com.inventory.ferrari.service.data.ferrari..*.*(..))") public void invanyMethod() { }; @Pointcut("execution(public * com.inventory.ferrari.service.data.galaxy..*.*(..))") public void galaxyanyMethod() { }; @Before("invanyMethod()") public void beforeinv(JoinPoint jp) { Object[] args = jp.getArgs(); if(args==null){ DataSourceTypeManager.set(DataSources.MASTER); //return; } //System.out.println("-------------" + args[0]); DataSourceTypeManager.set(DataSources.MASTER); } @Before("galaxyanyMethod()") public void beforegalaxy(JoinPoint jp) { DataSourceTypeManager.set(DataSources.SLAVE); } }
@Order(0) 註解表示在執行sql前就切換數據源