欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot 中多数据源配置(通过JdbcTemplate操作数据库)

程序员文章站 2022-06-13 19:04:48
...

在工作中碰到SpringBoot项目中去配置多个数据库,特此记录。

1、在springboot的配置文件中加入数据库的配置信息:
# 数据源1
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name:com.mysql.jdbc.Driver
spring.datasource.url:jdbc:mysql://
spring.datasource.username:******
spring.datasource.password:******

# 数据源2
spring.datasource2.type=com.zaxxer.hikari.HikariDataSource
spring.datasource2.driver-class-name:com.mysql.jdbc.Driver
spring.datasource2.url:jdbc:mysql://
spring.datasource2.username:******
spring.datasource2.password:******


# 数据源3
spring.datasource3.type=com.zaxxer.hikari.HikariDataSource
spring.datasource3.driver-class-name:com.mysql.jdbc.Driver
spring.datasource3.url:jdbc:
spring.datasource3.username:******
spring.datasource3.password:******
2、写三个类分别去获取配置文件中的数据库配置信息
@Component
@Data
public class DataBase1Properties {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
}
@Component
@Data
public class DataBase2Properties {
    @Value("${spring.datasource2.url}")
    private String url;
    @Value("${spring.datasource2.username}")
    private String username;
    @Value("${spring.datasource2.password}")
    private String password;
    @Value("${spring.datasource2.driver-class-name}")
    private String driverClassName;
}
@Data
@Component
public class DataBase3Properties {
    @Value("${spring.datasource3.url}")
    private String url;
    @Value("${spring.datasource3.username}")
    private String username;
    @Value("${spring.datasource3.password}")
    private String password;
    @Value("${spring.datasource3.driver-class-name}")
    private String driverClassName;
}
3、写配置类根据数据库信息获取数据库连接
@Configuration
public class DataSourceConfig {

    @Autowired
    private DataBase1Properties dataBase1Properties;

    @Autowired
    private DataBase2Properties dataBase2Properties;

    @Autowired
    private DataBase3Properties dataBase3Properties;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    @Qualifier("oneDataSource")
    @Primary
    DataSource dsOne() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(dataBase1Properties.getDriverClassName());
        dataSource.setJdbcUrl(dataBase1Properties.getUrl());
        dataSource.setUsername(dataBase1Properties.getUsername());
        dataSource.setPassword(dataBase1Properties.getPassword());
        return dataSource;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource2")
    @Qualifier("twoDataSource")
    DataSource dsTwo() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(dataBase2Properties.getDriverClassName());
        dataSource.setJdbcUrl(dataBase2Properties.getUrl());
        dataSource.setUsername(dataBase2Properties.getUsername());
        dataSource.setPassword(dataBase2Properties.getPassword());
        return dataSource;
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource3")
    @Qualifier("threeDataSource")
    DataSource dsThree() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(dataBase3Properties.getDriverClassName());
        dataSource.setJdbcUrl(dataBase3Properties.getUrl());
        dataSource.setUsername(dataBase3Properties.getUsername());
        dataSource.setPassword(dataBase3Properties.getPassword());
        return dataSource;
    }

    @Bean(name = "oneJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("oneDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "twoJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("twoDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "threeJdbcTemplate")
    public JdbcTemplate threeJdbcTemplate(
            @Qualifier("threeDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
4、封装JdbcTemplate去操作数据库
@Repository
public class JdbcTemplateUtil {

    @Autowired
    @Qualifier("threeJdbcTemplate")
    private JdbcTemplate threeJdbcTemplate;


    public Integer insertBiCenter(String sql) {
        int i = threeJdbcTemplate.update(sql);
        return i;
    }

}
5、测试封装好的工具类
@RunWith(SpringRunner.class)
@SpringBootTest
public class BigDataApplicationTests {

    @Autowired
    private JdbcTemplateUtil jdbcTemplateUtil;

    
    // 部门邮件分析
    @Test
    public void contextLoads() {

        String sql = "delete from bi_center.e_order_data";

        Integer integer = jdbcTemplateUtil.insertBiCenter(sql);
        System.out.println("integer = " + integer);
    }

    @Test
    public void auto() {

        String sql = "ALTER table bi_center.e_order_data AUTO_INCREMENT=1";
        Integer integer = jdbcTemplateUtil.insertBiCenter(sql);
        System.out.println("integer = " + integer);
    }
}
6、自测通过,大家如果碰到其他问题欢迎评论留言~~~~

SpringBoot 中多数据源配置(通过JdbcTemplate操作数据库)