• Spring多数据源配置


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

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

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

    方式一:创建多个SqlSessionFactory

    整合mysql+clickhouse双数据源

    1. <dependency>
    2. <groupId>org.mybatisgroupId>
    3. <artifactId>mybatis-springartifactId>
    4. <version>2.0.6version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibabagroupId>
    8. <artifactId>druidartifactId>
    9. <version>1.1.23version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.mybatisgroupId>
    13. <artifactId>mybatisartifactId>
    14. <version>3.4.5version>
    15. dependency>
    16. <dependency>
    17. <groupId>mysqlgroupId>
    18. <artifactId>mysql-connector-javaartifactId>
    19. <version>5.1.49version>
    20. dependency>
    21. <dependency>
    22. <groupId>ru.yandex.clickhousegroupId>
    23. <artifactId>clickhouse-jdbcartifactId>
    24. <version>0.1.53version>
    25. dependency>

    jdbc.properties

    1. jdbc.driverClassName=com.mysql.jdbc.Driver
    2. jdbc.master.url=jdbc:mysql://localhost:3306/test
    3. jdbc.master.username=root
    4. jdbc.master.password=root
    5. jdbc.master.maxActive=10
    6. jdbc.master.minIdle=5
    7. jdbc.master.maxWait=10000

    clickhouse.properties

    1. clickhouse.driverClassName=ru.yandex.clickhouse.ClickHouseDriver
    2. clickhouse.url=jdbc:clickhouse://localhost:8123/test
    3. clickhouse.username=root
    4. clickhouse.password=root
    5. clickhouse.maxActive=10
    6. clickhouse.minIdle=5
    7. clickhouse.maxWait=10000

    spring配置文件中配置多个数据源

    1. <context:property-placeholder location="classpath:jdbc.properties"/>
    2. <context:property-placeholder location="classpath:clickhouse.properties"/>
    3. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    4. <property name="driverClassName" value="${jdbc.driverClassName}"/>
    5. <property name="url" value="${jdbc.master.url}"/>
    6. <property name="password" value="${jdbc.master.password}"/>
    7. <property name="username" value="${jdbc.master.username}"/>
    8. <property name="maxActive" value="${jdbc.master.maxActive}"/>
    9. <property name="maxWait" value="${jdbc.master.maxWait}"/>
    10. <property name="minIdle" value="${jdbc.master.minIdle}"/>
    11. bean>
    12. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    13. <property name="configLocation" value="classpath:mybatis-config.xml"/>
    14. <property name="dataSource" ref="dataSource"/>
    15. <property name="typeAliasesPackage" value="com.demo.flash.price.redpot.entity"/>
    16. <property name="mapperLocations" value="classpath:mysql_mapper/*.xml"/>
    17. <property name="plugins">
    18. <set>
    19. <bean class="com.github.pagehelper.PageInterceptor">
    20. <property name="properties">
    21. <props>
    22. <prop key="helperDialect">mysqlprop>
    23. props>
    24. property>
    25. bean>
    26. set>
    27. property>
    28. bean>
    29. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    30. <property name="basePackage" value="com.demo.flash.price.redpot.mapperMysql"/>
    31. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    32. bean>
    33. <bean id="transactionManager"
    34. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    35. <property name="dataSource" ref="dataSource">property>
    36. bean>
    37. <tx:annotation-driven transaction-manager="transactionManager" />
    38. <bean id="dataSourceClickhouse" class="com.alibaba.druid.pool.DruidDataSource">
    39. <property name="driverClassName" value="${clickhouse.driverClassName}"/>
    40. <property name="url" value="${clickhouse.url}"/>
    41. <property name="password" value="${clickhouse.password}"/>
    42. <property name="username" value="${clickhouse.username}"/>
    43. <property name="maxActive" value="${clickhouse.maxActive}"/>
    44. <property name="maxWait" value="${clickhouse.maxWait}"/>
    45. <property name="minIdle" value="${clickhouse.minIdle}"/>
    46. bean>
    47. <bean id="sqlSessionFactoryClickhouse" class="org.mybatis.spring.SqlSessionFactoryBean">
    48. <property name="configLocation" value="classpath:mybatis-config.xml"/>
    49. <property name="dataSource" ref="dataSourceClickhouse"/>
    50. <property name="typeAliasesPackage" value="com.demo.flash.price.redpot.entity"/>
    51. <property name="mapperLocations" value="classpath:clickhouse_mapper/*.xml"/>
    52. <property name="plugins">
    53. <set>
    54. <bean class="com.github.pagehelper.PageInterceptor">
    55. <property name="properties">
    56. <props>
    57. <prop key="helperDialect">mysqlprop>
    58. props>
    59. property>
    60. bean>
    61. set>
    62. property>
    63. bean>
    64. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    65. <property name="basePackage" value="com.demo.flash.price.redpot.mapperClickhouse"/>
    66. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryClickhouse"/>
    67. bean>
    68. <bean id="transactionManagerClickhouse"
    69. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    70. <property name="dataSource" ref="dataSourceClickhouse">property>
    71. bean>
    72. <tx:annotation-driven transaction-manager="transactionManagerClickhouse" />

    方式二:动态切换数据源对象

    创建单个SqlSessionFactory,动态切换数据源对象 ,在Spring的jdbc包下有个支持动态数据源切换的抽象类AbstractRoutingDataSource,其中的抽象方法determineCurrentLookupKey需要我们自己来实现,到底返回哪个数据源

    1. public class DynamicDataSource extends AbstractRoutingDataSource {
    2. @Override
    3. protected Object determineCurrentLookupKey() {
    4. //在这里做数据源切换
    5. return DataSourceTypeManager.get();
    6. }
    7. }
    1. public class DataSourceTypeManager {
    2. //数据源保存
    3. private static final ThreadLocal dataSourceTypes = new ThreadLocal() {
    4. @Override
    5. protected MybatisDataSource initialValue() {
    6. return MybatisDataSource.JKDSJ;
    7. }
    8. };
    9. public static MybatisDataSource get() {
    10. return dataSourceTypes.get();
    11. }
    12. public static void set(MybatisDataSource dataSourceType) {
    13. dataSourceTypes.set(dataSourceType);
    14. }
    15. public static void reset() {
    16. dataSourceTypes.set(MybatisDataSource.JKDSJ);
    17. }
    18. }

    【SpringBoot DB 系列】Mybatis 基于 AbstractRoutingDataSource 与 AOP 实现多数据源切换_一灰灰blog的博客-CSDN博客

  • 相关阅读:
    JavaScript 里三个点 ...,可不是省略号啊···
    简单两步实现离线部署ChatGPT,ChatGPT平替版,无需GPU离线搭建ChatGPT
    AIRIOT答疑第6期|如何使用二次开发引擎?
    redis缓存失效问题
    DASCTF NOV X联合出题人2022年度积分榜争夺赛 RE-部分题解
    【Markdown】 Markdown 操作备忘录
    vscode使用code runner乱码
    【表面缺陷检测】钢轨表面缺陷检测数据集介绍(2类,含xml标签文件)
    git相关知识收藏
    ElasticSearch从入门到精通--第六话(补充篇:Docker启动es、Kibana、IK分词器使用、地理位置、分数查询设置、聚合)
  • 原文地址:https://blog.csdn.net/qq_34491508/article/details/126600121