zoukankan      html  css  js  c++  java
  • spring boot多数据源配置示例

    1. application.properties

    #u4E3Bu5E93u914Du7F6E
    spring.datasource.primary.url=jdbc:mysql://mysql.test.xxxxx.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.primary.username=root
    spring.datasource.primary.password=root
    spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource
    
    
    spring.datasource.secondary.url=jdbc:mysql://mysql.log.test.xxxx.com:3306/test_log?useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=root
    spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource

    2. 配置类

    2.1 datasource配置

    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    @Configuration
    public class DataSourceConfig {
        @Bean
        @Qualifier("primaryDataSourceProperties")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.primary")
        public DataSourceProperties primaryDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Autowired
        @Qualifier("primaryDataSourceProperties")
        private DataSourceProperties primaryDataSourceProperties;
    
        @Bean
        @Qualifier("primaryDataSource")
        @Primary
        public DataSource primaryDataSource() {
            return primaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        }
    
        //////////////////////
    
        @Bean
        @Qualifier("secondaryDataSourceProperties")
        @ConfigurationProperties(prefix = "spring.datasource.secondary")
        public DataSourceProperties secondaryDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Autowired
        @Qualifier("secondaryDataSourceProperties")
        private DataSourceProperties secondaryDataSourceProperties;
    
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        public DataSource secondaryDataSource() {
            return secondaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        }
    
    
    }

    2.2 主库配置

    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    /*
     * 主库的配置
     */
    @Configuration
    @MapperScan(basePackages = "com.xxxx.xxxx.mapper",sqlSessionFactoryRef = "primarySqlSessionFactory")
    public class DatabaseConfig {
        @Autowired
        @Qualifier("primaryDataSource") 
        private DataSource primaryDataSource;
        
        @Bean("primarySqlSessionFactory")
        @Qualifier("primarySqlSessionFactory")
        @Primary
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(primaryDataSource);
            return sessionFactoryBean.getObject();
        }
    } // class DatabaseConfig

    2.3 日志库配置

    import javax.sql.DataSource;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    /*
     * 主库的配置
     */
    @Configuration
    @MapperScan(basePackages = "com.xxxx.xxxx.logmapper",sqlSessionFactoryRef = "secondarySqlSessionFactory")
    public class LogDatabaseConfig{
        
        @Autowired
        @Qualifier("secondaryDataSource") 
        private DataSource secondaryDataSource;
        
        @Bean(name = "secondarySqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(secondaryDataSource);
            return sessionFactoryBean.getObject();
        }
    } // class LogDatabaseConfig

    3. 使用

  • 相关阅读:
    【Elasticsearch 技术分享】—— ES 常用名词及结构
    【Elasticsearch 技术分享】—— Elasticsearch ?倒排索引?这都是什么?
    除了读写锁,JUC 下面还有个 StampedLock!还不过来了解一下么?
    小伙伴想写个 IDEA 插件么?这些 API 了解一下!
    部署Microsoft.ReportViewe
    关于TFS强制undo他人check out
    几段查看数据库表占用硬盘空间的tsql
    How to perform validation on sumbit only
    TFS 2012 Disable Multiple Check-out
    在Chrome Console中加载jQuery
  • 原文地址:https://www.cnblogs.com/davidwang456/p/7246969.html
Copyright © 2011-2022 走看看