• Spring Boot项目配置多个数据源


    一、yml配置两个自定义数据源

    master:
      dataSource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/androids?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: 123456
        password: 123456
    
    # 配置从库的相关配置
    second:
      dataSource:
        driver-class-name: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@1234:1521/pdbwer
        username: 123456
        password: 123546
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    二、根据两个数据源创建两个Config

    1、MasterDataSourceConfig

    basePackages 对应@Mapper修饰的mapper包路径
    
    • 1
    package com.dbxq.androids.config;
    
    import com.zaxxer.hikari.HikariDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.TransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * @author fangqm
     * @date 2022/11/14 19:10
     */
    @Configuration
    @MapperScan(basePackages = "com.dbxq.androids.mapper",sqlSessionFactoryRef = "masterSqlSessionFactory")
    public class MasterDataSourceConfig {
    
        private static final String MAPPER_LOCATION = "classpath:mapping/*.xml";
    
        @Value("${master.dataSource.url}")
        private String url;
    
        @Value("${master.dataSource.username}")
        private String username;
    
        @Value("${master.dataSource.password}")
        private String password;
    
        @Value("${master.dataSource.driver-class-name}")
        private String driverClassName;
    
    
        @Primary
        @Bean("masterDataSource")
        public DataSource getMasterDataSource(){
            //创建一个数据源对象,springboot默认为Hikari数据源,我们也用该数据源
            HikariDataSource dataSource = new HikariDataSource();
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setJdbcUrl(url);
            return dataSource;
        }
    
        @Bean("masterTransactionManager")
        @Primary
        public TransactionManager getMasterTransactionManager(){
            return new DataSourceTransactionManager
                    ( getMasterDataSource());
        }
    
        @Bean("masterSqlSessionFactory")
        @Primary
        public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(masterDataSource);
            sqlSessionFactory.setMapperLocations(
                    new PathMatchingResourcePatternResolver()
                            .getResources(MAPPER_LOCATION)
    
            );
            return sqlSessionFactory.getObject();
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    2、SecondDataSourceConfig

    package com.dbxq.androids.config;
    
    
    import com.zaxxer.hikari.HikariDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.TransactionManager;
    
    import javax.sql.DataSource;
    /**
     * @author fangqm
     * @date 2022/11/14 19:15
     */
    @Configuration
    @MapperScan(basePackages = "com.dbxq.androids.second",sqlSessionFactoryRef = "secondSqlSessionFactory")
    public class SecondDataSourceConfig {
    
        private static final String MAPPER_LOCATION = "classpath:mapping/second/*.xml";
    
        @Value("${second.dataSource.url}")
        private String url;
    
        @Value("${second.dataSource.username}")
        private String username;
    
        @Value("${second.dataSource.password}")
        private String password;
    
        @Value("${second.dataSource.driver-class-name}")
        private String driverClassName;
    
    
    
        @Bean("secondDataSource")
        public DataSource getSecondDataSource(){
            //创建一个数据源对象,springboot默认为Hikari数据源,我们也用该数据源
            HikariDataSource dataSource = new HikariDataSource();
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setJdbcUrl(url);
            return dataSource;
    
    
    
        }
    
        @Bean("secondTransactionManager")
        public TransactionManager getSecondTransactionManager(){
            return new DataSourceTransactionManager
                    ( getSecondDataSource());
        }
    
        @Bean("secondSqlSessionFactory")
        public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource secondDataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(secondDataSource);
            sqlSessionFactory.setMapperLocations(
                    new PathMatchingResourcePatternResolver()
                            .getResources(MAPPER_LOCATION)
    
            );
            return sqlSessionFactory.getObject();
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    C语言为什么for不执行?
    一幅长文细学GaussDB(一)——一幅长文系列
    [附源码]SSM计算机毕业设计音乐网站JAVA
    PAT 1174 Left-View of Binary Tree 题干不知所云
    “遥感新纪元:GPT技术引领地球观测的智慧革新“
    ESP8266发送WOL幻数据包实现电脑远程唤醒
    Dubbo学习
    log4js node日志插件
    基于JAVA热门股票推荐系统计算机毕业设计源码+数据库+lw文档+系统+部署
    可变形注意力转换器综述
  • 原文地址:https://blog.csdn.net/qq_42261895/article/details/127861732