springboot 使用多数据源

1:引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

2:引入配置文件

#主数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db1
spring.datasource.username=root
spring.datasource.password=root

#从数据源
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.second.url=jdbc:mysql://127.0.0.1:3306/db2
spring.datasource.second.username=root
spring.datasource.second.password=root

3:生成配置类文件注入

@Configuration
public class JdbcDataSourceConfig {
    
    

//    存在多数据源,需指定一个优先级最高的实例
    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties db1DataSourceProperties() {
    
    
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSourceProperties db2DataSourceProperties() {
    
    
        return new DataSourceProperties();
    }

    @Primary
    @Bean
    public DataSource db1DataSource() {
    
    
        return db1DataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    public DataSource db2DataSource() {
    
    
        return db2DataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean(name = "db1JdbcTemplate")
    public JdbcTemplate db1JdbcTemplate() {
    
    
        return new JdbcTemplate(db1DataSource());
    }

    @Bean(name = "db2JdbcTemplate")
    public JdbcTemplate db2JdbcTemplate() {
    
    
        return new JdbcTemplate(db2DataSource());
    }
    
}

4:测试用例调用

@Autowired
    JdbcTemplate db1JdbcTemplate;

    @Autowired
    JdbcTemplate db2JdbcTemplate;

    @Test
    public void testDB(){
    
    
        String sql = "insert into user (name ,age) values ('rt1',18)";

        db1JdbcTemplate.execute(sql);
        db2JdbcTemplate.execute(sql);

    }

ps:如果不使用@Primary实现,也可使用下列实现

@SpringBootApplication(exclude = {
    
    
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class
})

在主类入口去除数据库相关的核心类

猜你喜欢

转载自blog.csdn.net/weixin_44971379/article/details/118656684

相关文章