SpringBoot-JdbcTemplate multi-data source configuration for database operations

The so-called multi-data source refers to the use of multiple libraries in different database instances in a Java EE project, or multiple different libraries in the same database instance. For example, the realization of database read-write separation, database sub-table, backup and other operations. The configuration of JdbcTemplate multiple data sources is relatively simple, because one JdbcTemplate corresponds to one DataSource , developers only need to manually provide multiple DataSources , and then manually configure the JdbcTemplate .

1. Add dependencies

For adding specific dependencies, please refer to the introduction in the previous chapter .

2. Create two databases

3.  Configure multiple data sources in  application.properties

# 第一个数据源 first
spring.datasource.first.username=root
spring.datasource.first.password=root
spring.datasource.first.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.first.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.first.driverClassName=com.mysql.cj.jdbc.Driver

# 第二个数据源 second
spring.datasource.second.username=root
spring.datasource.second.password=root
spring.datasource.second.jdbcUrl=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.second.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.second.driverClassName=com.mysql.cj.jdbc.Driver

4. Data source configuration

@Configuration
public class DatasourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public DataSource firstDataSource(){
       return  new HikariDataSource();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource(){
        return  new HikariDataSource();
    }
}

5. Configure JdbcTemplate

(1) As you can see from the previous example, as long as the project introduces the spring-jdbc dependency, if the developer does not provide an instance of JdbcTemplate, Spring Boot will provide an instance of JdbcTemplate by default.
(2) If multiple data sources are configured, the developer provides an instance of JdbcTemplate, the code is as follows:

@Configuration
public class JdbcTemplateConfig {

    @Bean
    public JdbcTemplate firstJdbcTemplate(@Qualifier("firstDataSource")DataSource dataSource){

        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource")DataSource dataSource){

        return new JdbcTemplate(dataSource);
    }
}

6. Create an entity

public class User {

    private Integer id;

    private String name;

    private String address;

    // 省略 getter 和 setter 方法
}


public class Book {
 
    private Integer id;
 
    private String name;
 
    private String author;
 
 
    // 省略 getter 和 setter 方法
}

7. Create Controller

  For the convenience of demonstration, the Dao  layer and the  Service  layer are no longer added here  , but two JdbcTemplates are  directly  injected into the  Controller  , and then the data in the two databases are queried separately.


@RestController
public class TowDataSourceController {

    @Resource(name = "firstJdbcTemplate")
    private JdbcTemplate first;

    @Resource(name = "secondJdbcTemplate")
    private JdbcTemplate second;

    @PostMapping("/testSave")
    public String test(){


        first.update("insert into `book`(`name`, `author`) values(?, ?)",
                "金瓶梅", "兰陵笑笑生");

        second.update("insert into `user`(`name`, `address`) values(?, ?)",
                "兰陵笑笑生", "清朝");

        return "保存成功";
    }
}

8. Restart the test

Guess you like

Origin blog.csdn.net/small_love/article/details/111822955