zoukankan      html  css  js  c++  java
  • SpringBoot 多数据源配置

    SpringBoot 多数据源配置

    application.properties文件内容配置多数据源,如下

    #primary数据源配置
    spring.datasource.primary.url=jdbc:mysql://localhost:3306/joppay?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
    spring.datasource.primary.username=root
    spring.datasource.primary.password=test
    spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
    #primary数据源jpa配置,部分配置仅在此处生效
    spring.jpa.primary.show-sql=false
    spring.jpa.primary.generate-ddl=false
    spring.jpa.primary.hibernate.ddl-auto=none
    spring.jpa.primary.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
    #second数据源配置
    spring.datasource.second.url=jdbc:mysql://localhost:3306/joppay?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
    spring.datasource.second.username=root
    spring.datasource.second.password=test
    spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
    #second数据源jpa配置
    spring.jpa.second.generate-ddl=false
    spring.jpa.second.hibernate.ddl-auto=none
    spring.jpa.second.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

    在XXXApplication.java文件中代码:

     1 @SpringBootApplication
     2 @ComponentScan(basePackages = {"com.joppay.admin", "com.joppay.core.config", "com.joppay.core.service"})
     3 @EnableScheduling
     4 @EnableCaching
     5 @EnableJms
     6 public class AdminApplication extends SpringBootServletInitializer {
     7 
     8     public static void main(String[] args) {
     9         SpringApplication.run(AdminApplication.class, args);
    10     }
    11 
    12     @Override
    13     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    14         return builder.sources(AdminApplication.class);
    15     }
    16 }

    //@EnableJpaRepositories(basePackages = {"com.joppay.core.repository"})
    //@EntityScan(basePackages = {"com.joppay.core.entity"})

    其中这4个要取消扫描,在下面的文件中扫描

    1. PrimaryDataSourceConfig.java

     1 package com.joppay.admin.datasource;
     2 
     3 import org.springframework.beans.factory.annotation.Qualifier;
     4 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
     5 import org.springframework.boot.context.properties.ConfigurationProperties;
     6 import org.springframework.context.annotation.Bean;
     7 import org.springframework.context.annotation.Configuration;
     8 import org.springframework.context.annotation.Primary;
     9 import org.springframework.jdbc.core.JdbcTemplate;
    10 
    11 import javax.sql.DataSource;
    12 
    13 /**
    14  * 主数据源
    15  */
    16 @Configuration
    17 public class PrimaryDataSourceConfig {
    18     /**
    19      * 扫描spring.datasource.primary开头的配置信息
    20      *
    21      * @return 数据源配置信息
    22      */
    23     @Primary
    24     @Bean(name = "primaryDataSourceProperties")
    25     @ConfigurationProperties(prefix = "spring.datasource.primary")
    26     public DataSourceProperties dataSourceProperties() {
    27         return new DataSourceProperties();
    28     }
    29 
    30     /**
    31      * 获取主库数据源对象
    32      *
    33      * @param properties 注入名为primaryDataSourceProperties的bean
    34      * @return 数据源对象
    35      */
    36     @Primary
    37     @Bean(name = "primaryDataSource")
    38     public DataSource dataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties properties) {
    39         return properties.initializeDataSourceBuilder().build();
    40     }
    41 
    42     /**
    43      * 该方法仅在需要使用JdbcTemplate对象时选用
    44      *
    45      * @param dataSource 注入名为primaryDataSource的bean
    46      * @return 数据源JdbcTemplate对象
    47      */
    48     @Primary
    49     @Bean(name = "primaryJdbcTemplate")
    50     public JdbcTemplate jdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
    51         return new JdbcTemplate(dataSource);
    52     }
    53 }
    View Code

    2. PrimaryRepositoryConfig.java

     1 package com.joppay.admin.datasource;
     2 
     3 import org.springframework.beans.factory.annotation.Qualifier;
     4 import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
     5 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
     6 import org.springframework.boot.context.properties.ConfigurationProperties;
     7 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.context.annotation.Configuration;
    10 import org.springframework.context.annotation.Primary;
    11 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    12 import org.springframework.orm.jpa.JpaTransactionManager;
    13 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    14 import org.springframework.transaction.PlatformTransactionManager;
    15 import org.springframework.transaction.annotation.EnableTransactionManagement;
    16 
    17 import javax.persistence.EntityManager;
    18 import javax.persistence.EntityManagerFactory;
    19 import javax.sql.DataSource;
    20 
    21 @Configuration
    22 @EnableTransactionManagement
    23 @EnableJpaRepositories(
    24         // repository包名
    25         basePackages = "com.joppay.core.repository.primary",
    26         // 实体管理bean名称
    27         entityManagerFactoryRef = "primaryEntityManagerFactory",
    28         // 事务管理bean名称
    29         transactionManagerRef = "primaryTransactionManager"
    30 )
    31 public class PrimaryRepositoryConfig {
    32     /**
    33      * 扫描spring.jpa.primary开头的配置信息
    34      *
    35      * @return jpa配置信息
    36      */
    37     @Primary
    38     @Bean(name = "primaryJpaProperties")
    39     @ConfigurationProperties(prefix = "spring.jpa.primary")
    40     public JpaProperties jpaProperties() {
    41         return new JpaProperties();
    42     }
    43 
    44     /**
    45      * 获取主库实体管理工厂对象
    46      *
    47      * @param primaryDataSource 注入名为primaryDataSource的数据源
    48      * @param jpaProperties     注入名为primaryJpaProperties的jpa配置信息
    49      * @param builder           注入EntityManagerFactoryBuilder
    50      * @return 实体管理工厂对象
    51      */
    52     @Primary
    53     @Bean(name = "primaryEntityManagerFactory")
    54     public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource
    55             , @Qualifier("primaryJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) {
    56         return builder
    57                 // 设置数据源
    58                 .dataSource(primaryDataSource)
    59                 // 设置jpa配置
    60                 .properties(jpaProperties.getProperties())
    61                 // 设置hibernate配置
    62                 .properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
    63                 // 设置实体包名
    64                 .packages("com.joppay.core.entity.primary")
    65                 .build();
    66     }
    67 
    68     /**
    69      * 获取实体管理对象
    70      *
    71      * @param factory 注入名为primaryEntityManagerFactory的bean
    72      * @return 实体管理对象
    73      */
    74     @Primary
    75     @Bean(name = "primaryEntityManager")
    76     public EntityManager entityManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
    77         return factory.createEntityManager();
    78     }
    79 
    80     /**
    81      * 获取主库事务管理对象
    82      *
    83      * @param factory 注入名为primaryEntityManagerFactory的bean
    84      * @return 事务管理对象
    85      */
    86     @Primary
    87     @Bean(name = "primaryTransactionManager")
    88     public PlatformTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
    89         return new JpaTransactionManager(factory);
    90     }
    91 }
    View Code

    3. SecondDataSourceConfig.java

     1 package com.joppay.admin.datasource;
     2 
     3 import org.springframework.beans.factory.annotation.Qualifier;
     4 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
     5 import org.springframework.boot.context.properties.ConfigurationProperties;
     6 import org.springframework.context.annotation.Bean;
     7 import org.springframework.context.annotation.Configuration;
     8 import org.springframework.jdbc.core.JdbcTemplate;
     9 
    10 import javax.sql.DataSource;
    11 
    12 /**
    13  * 第二数据源
    14  * 生活缴费
    15  */
    16 @Configuration
    17 public class SecondDataSourceConfig {
    18     /**
    19      * 扫描spring.datasource.second开头的配置信息
    20      *
    21      * @return 数据源配置信息
    22      */
    23     @Bean(name = "secondDataSourceProperties")
    24     @ConfigurationProperties(prefix = "spring.datasource.second")
    25     public DataSourceProperties dataSourceProperties() {
    26         return new DataSourceProperties();
    27     }
    28 
    29     /**
    30      * 获取从库数据源对象
    31      *
    32      * @param properties 注入名为secondDataSourceProperties的bean
    33      * @return 数据源对象
    34      */
    35     @Bean(name = "secondDataSource")
    36     public DataSource dataSource(@Qualifier("secondDataSourceProperties") DataSourceProperties properties) {
    37         return properties.initializeDataSourceBuilder().build();
    38     }
    39 
    40     /**
    41      * 该方法仅在需要使用JdbcTemplate对象时选用
    42      *
    43      * @param dataSource 注入名为secondDataSource的bean
    44      * @return 数据源JdbcTemplate对象
    45      */
    46     @Bean(name = "secondJdbcTemplate")
    47     public JdbcTemplate jdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) {
    48         return new JdbcTemplate(dataSource);
    49     }
    50 }
    View Code

    4. SecondRepositoryConfig.java

     1 package com.joppay.admin.datasource;
     2 
     3 import org.springframework.beans.factory.annotation.Qualifier;
     4 import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
     5 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
     6 import org.springframework.boot.context.properties.ConfigurationProperties;
     7 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.context.annotation.Configuration;
    10 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    11 import org.springframework.orm.jpa.JpaTransactionManager;
    12 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    13 import org.springframework.transaction.PlatformTransactionManager;
    14 import org.springframework.transaction.annotation.EnableTransactionManagement;
    15 
    16 import javax.persistence.EntityManager;
    17 import javax.persistence.EntityManagerFactory;
    18 import javax.sql.DataSource;
    19 
    20 @Configuration
    21 @EnableTransactionManagement
    22 @EnableJpaRepositories(
    23         // repository包名
    24         basePackages = "com.joppay.core.repository.second",
    25         // 实体管理bean名称
    26         entityManagerFactoryRef = "secondEntityManagerFactory",
    27         // 事务管理bean名称
    28         transactionManagerRef = "secondTransactionManager"
    29 )
    30 public class SecondRepositoryConfig {
    31     /**
    32      * 扫描spring.jpa.second开头的配置信息
    33      *
    34      * @return jpa配置信息
    35      */
    36     @Bean(name = "secondJpaProperties")
    37     @ConfigurationProperties(prefix = "spring.jpa.second")
    38     public JpaProperties jpaProperties() {
    39         return new JpaProperties();
    40     }
    41 
    42     /**
    43      * 获取从库实体管理工厂对象
    44      *
    45      * @param secondDataSource 注入名为secondDataSource的数据源
    46      * @param jpaProperties    注入名为secondJpaProperties的jpa配置信息
    47      * @param builder          注入EntityManagerFactoryBuilder
    48      * @return 实体管理工厂对象
    49      */
    50     @Bean(name = "secondEntityManagerFactory")
    51     public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("secondDataSource") DataSource secondDataSource
    52             , @Qualifier("secondJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) {
    53         return builder
    54                 // 设置数据源
    55                 .dataSource(secondDataSource)
    56                 // 设置jpa配置
    57                 .properties(jpaProperties.getProperties())
    58                 // 设置hibernate配置
    59                 .properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
    60                 // 设置实体包名
    61                 .packages("com.joppay.core.entity.second")
    62                 .build();
    63     }
    64 
    65     /**
    66      * 获取实体管理对象
    67      *
    68      * @param factory 注入名为secondEntityManagerFactory的bean
    69      * @return 实体管理对象
    70      */
    71     @Bean(name = "secondEntityManager")
    72     public EntityManager entityManager(@Qualifier("secondEntityManagerFactory") EntityManagerFactory factory) {
    73         return factory.createEntityManager();
    74     }
    75 
    76     /**
    77      * 获取从库事务管理对象
    78      *
    79      * @param factory 注入名为secondEntityManagerFactory的bean
    80      * @return 事务管理对象
    81      */
    82     @Bean(name = "secondTransactionManager")
    83     public PlatformTransactionManager transactionManager(@Qualifier("secondEntityManagerFactory") EntityManagerFactory factory) {
    84         return new JpaTransactionManager(factory);
    85     }
    86 }
    View Code

    说明:

    1.使用JdbcTemplate的时候要指定要使用的数据源,(@Qualifier是指定用哪个实现类)如:

        @Autowired
        @Qualifier(value="primaryJdbcTemplate")
        private JdbcTemplate jdbcTemplate;

    2.在PrimaryDataSourceConfig.java文件中的

    @ConfigurationProperties(prefix = "spring.datasource.primary")
    这个prefix 要与配置文件application.properties文件的key对应


  • 相关阅读:
    Event 事件(最简单实用)
    Codeforces Beta Round #93_A题
    欧几里得算法扩展(extended gcd)解不定方程_初入门
    HDU2955_Robberies_01背包变种
    HDU2602_Bone Collector_很水的01背包
    USACO_2_1_3_Sorting a ThreeValued Sequence_交换环
    Codeforces Beta Round #93_B题
    中国剩余定理的_非互素同余模板
    HDU1114_DP_完全背包
    HDU3809_Decrypt coordinate_迭代法
  • 原文地址:https://www.cnblogs.com/dwb91/p/10585208.html
Copyright © 2011-2022 走看看