Spring Boot integrate multiple data sources JdbcTemplate

Create Project

First is to create a project, and as previously, create projects, but also select Web, Jdbc and MySQL driven, as shown below:

Dependence follows Pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.28</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

Note: druid here using alibaba's druid-spring-boot-starter, rather than the traditional druid, druid tradition of no datasource instance, you need to configure our own, is not recommended. Because the druid-spring-boot-starterdependence is provided DruidDataSourceBuilder classes, this example can be used to construct a DataSource

Data source configuration

spring.datasource.one.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.two.url=jdbc:mysql:///test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

Here by one data source and two distinction, but after the addition of one and two, the configuration here would not be automatically loaded SpringBoot (because the previous key is changed), we need to load their DataSource, this time , you need to configure yourself a DataSourceConfig, to provide two DataSource Bean, as follows:
 

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.one")
    DataSource dsOne() {
        return DruidDataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.two")
    DataSource dsTwo() {
        return DruidDataSourceBuilder.create().build();
    }
}

Here are two Bean, which @ConfigurationProperties is type-safe property is bound Spring Boot provided to the first Bean, for example, @ConfigurationProperties (prefix = "spring.datasource.one") indication spring.datasource.one database configuration prefix to create a DataSource, after this configuration, we have two different DataSource, then followed two different DataSource to create two different JdbcTemplate.

JdbcTemplate configuration examples

Create JdbcTemplateConfig class, to provide two different JdbcTemplate example, as follows

@Configuration
public class JdbcTemplateConfig {
    @Bean
    JdbcTemplate jdbcTemplateOne(@Qualifier("dsOne") DataSource dsOne) {
        return new JdbcTemplate(dsOne);
    }
    @Bean
    JdbcTemplate jdbcTemplateTwo(@Qualifier("dsTwo") DataSource dsTwo) {
        return new JdbcTemplate(dsTwo);
    }
}

Each JdbcTemplate the need to create a DataSource, because the Spring container now there are two DataSource, use the default lookup type, will complain, so add @Qualifier comment, means the search by name. Here JdbcTemplate creates two instances, respectively corresponding to the two DataSource. Then go directly to the use of this JdbcTemplate it.

test

@RestController
public class HelloController {
    @Autowired
    @Qualifier("jdbcTemplateOne")
    JdbcTemplate jdbcTemplateOne;
    @Resource(name = "jdbcTemplateTwo")
    JdbcTemplate jdbcTemplateTwo;

    @GetMapping("/user")
    public List<User> getAllUser() {
        List<User> list = jdbcTemplateOne.query("select * from t_user", new BeanPropertyRowMapper<>(User.class));
        return list;
    }
    @GetMapping("/user2")
    public List<User> getAllUser2() {
        List<User> list = jdbcTemplateTwo.query("select * from t_user", new BeanPropertyRowMapper<>(User.class));
        return list;
    }
}

And DataSource -, Spring container also has two JdbcTemplate, can not by way of injection byType come to everyone here provides two injection idea is to use @Resource annotations directly injected in through byName manner, additional One is @Autowired notes plus @Qualifier annotation, both together, actually byName. After the injection JdbcTemplate come, jdbcTemplateOne and jdbcTemplateTwo case represent different operation on source data, different data sources using JdbcTemplate operation, to achieve a multi data source configuration.

to sum up

If the actual development needs is relatively simple and can use this multi data source configuration, if the recommended choice for distributed database distributed middleware MyCat to solve related problems, when several multiple data sources, use MyCat use, as well as sub-table strategies using sharding-by-intfile than boot using multiple data sources easier.

Reference Address: https://blog.csdn.net/u012702547/article/details/102968669

Published 260 original articles · won praise 112 · views 260 000 +

Guess you like

Origin blog.csdn.net/qq_34491508/article/details/103854512
Recommended