Spring多数据源配置,这里持久层框架使用mybatis,集成Mybatis多数据源有两种方式:
1、创建多个SqlSessionFactory,扫描每个SqlSessionFactoryBean对应的包,形成了每个Factory对应一个数据源。
2、创建一个SqlSessionFactory,通过动态切换数据源对象,达到多数据源操作功能。
整合mysql+clickhouse双数据源
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.6version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.23version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.4.5version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.49version>
- dependency>
-
- <dependency>
- <groupId>ru.yandex.clickhousegroupId>
- <artifactId>clickhouse-jdbcartifactId>
- <version>0.1.53version>
- dependency>
jdbc.properties
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.master.url=jdbc:mysql://localhost:3306/test
- jdbc.master.username=root
- jdbc.master.password=root
- jdbc.master.maxActive=10
- jdbc.master.minIdle=5
- jdbc.master.maxWait=10000
clickhouse.properties
- clickhouse.driverClassName=ru.yandex.clickhouse.ClickHouseDriver
- clickhouse.url=jdbc:clickhouse://localhost:8123/test
- clickhouse.username=root
- clickhouse.password=root
- clickhouse.maxActive=10
- clickhouse.minIdle=5
- clickhouse.maxWait=10000
spring配置文件中配置多个数据源
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <context:property-placeholder location="classpath:clickhouse.properties"/>
-
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}"/>
- <property name="url" value="${jdbc.master.url}"/>
- <property name="password" value="${jdbc.master.password}"/>
- <property name="username" value="${jdbc.master.username}"/>
- <property name="maxActive" value="${jdbc.master.maxActive}"/>
- <property name="maxWait" value="${jdbc.master.maxWait}"/>
- <property name="minIdle" value="${jdbc.master.minIdle}"/>
- bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- <property name="dataSource" ref="dataSource"/>
- <property name="typeAliasesPackage" value="com.demo.flash.price.redpot.entity"/>
- <property name="mapperLocations" value="classpath:mysql_mapper/*.xml"/>
- <property name="plugins">
- <set>
-
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <props>
-
- <prop key="helperDialect">mysqlprop>
- props>
- property>
- bean>
- set>
- property>
- bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.demo.flash.price.redpot.mapperMysql"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
- bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
-
-
- <bean id="dataSourceClickhouse" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${clickhouse.driverClassName}"/>
- <property name="url" value="${clickhouse.url}"/>
- <property name="password" value="${clickhouse.password}"/>
- <property name="username" value="${clickhouse.username}"/>
- <property name="maxActive" value="${clickhouse.maxActive}"/>
- <property name="maxWait" value="${clickhouse.maxWait}"/>
- <property name="minIdle" value="${clickhouse.minIdle}"/>
- bean>
- <bean id="sqlSessionFactoryClickhouse" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- <property name="dataSource" ref="dataSourceClickhouse"/>
- <property name="typeAliasesPackage" value="com.demo.flash.price.redpot.entity"/>
- <property name="mapperLocations" value="classpath:clickhouse_mapper/*.xml"/>
- <property name="plugins">
- <set>
-
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <props>
-
- <prop key="helperDialect">mysqlprop>
- props>
- property>
- bean>
- set>
- property>
- bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.demo.flash.price.redpot.mapperClickhouse"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryClickhouse"/>
- bean>
- <bean id="transactionManagerClickhouse"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSourceClickhouse">property>
- bean>
- <tx:annotation-driven transaction-manager="transactionManagerClickhouse" />
创建单个SqlSessionFactory,动态切换数据源对象 ,在Spring的jdbc包下有个支持动态数据源切换的抽象类AbstractRoutingDataSource,其中的抽象方法determineCurrentLookupKey
需要我们自己来实现,到底返回哪个数据源
- public class DynamicDataSource extends AbstractRoutingDataSource {
-
- @Override
- protected Object determineCurrentLookupKey() {
- //在这里做数据源切换
- return DataSourceTypeManager.get();
- }
-
- }
- public class DataSourceTypeManager {
- //数据源保存
- private static final ThreadLocal
dataSourceTypes = new ThreadLocal() { -
- @Override
- protected MybatisDataSource initialValue() {
- return MybatisDataSource.JKDSJ;
- }
- };
-
- public static MybatisDataSource get() {
- return dataSourceTypes.get();
- }
-
- public static void set(MybatisDataSource dataSourceType) {
- dataSourceTypes.set(dataSourceType);
- }
-
- public static void reset() {
- dataSourceTypes.set(MybatisDataSource.JKDSJ);
- }
-
- }
【SpringBoot DB 系列】Mybatis 基于 AbstractRoutingDataSource 与 AOP 实现多数据源切换_一灰灰blog的博客-CSDN博客