您的当前位置:首页正文

使用Spring多数据源配置

2024-11-02 来源:个人技术集锦

Spring多数据源配置

Spring多数据源配置,这里持久层框架使用mybatis,集成Mybatis多数据源有两种方式:

1、创建多个SqlSessionFactory,扫描每个SqlSessionFactoryBean对应的包,形成了每个Factory对应一个数据源。

2、创建一个SqlSessionFactory,通过动态切换数据源对象,达到多数据源操作功能。

方式一:创建多个SqlSessionFactory

  • 整合mysql+clickhouse双数据源
<!--mybatis + mysql + druid-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.6</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.23</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
    </dependency>
    <!--clickhouse-->
    <dependency>
      <groupId>ru.yandex.clickhouse</groupId>
      <artifactId>clickhouse-jdbc</artifactId>
      <version>0.1.53</version>
    </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配置文件中配置多个数据源
<!--加载mysql+clickhouse配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <context:property-placeholder location="classpath:clickhouse.properties"/>

    <!--=======================mysql数据源配置=======================-->
    <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>
                <!--配置pageHelper 分页插件-->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <!--方言:-->
                            <prop key="helperDialect">mysql</prop>
                        </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" />

    <!--=======================clickhouse数据源配置=======================-->
    <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>
                <!--配置pageHelper 分页插件-->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <!--方言:-->
                            <prop key="helperDialect">mysql</prop>
                        </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<MybatisDataSource> dataSourceTypes = new ThreadLocal<MybatisDataSource>() {
		 
		@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);
	}
 
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:
Top