SpringBoot-データベース操作用のJdbcTemplateマルチデータソース構成

いわゆるマルチデータソースとJava EEプロジェクトの異なるデータベースインスタンスで複数のライブラリを使用すること、または同じデータベースインスタンスで複数の異なるライブラリを使用すること指します。たとえば、データベースの読み取りと書き込みの分離、データベースのサブテーブル、バックアップ、およびその他の操作の実現。構成JdbcTemplate 1つのため、複数のデータソースは、比較的単純であるJdbcTemplateの一つに対応するデータソース開発者は手動でのみ複数用意する必要があり、データソースを、次いで手動で設定JdbcTemplateを

1.依存関係を追加します

特定の依存関係を追加するには、前の章の概要を参照しください

2.2つのデータベースを作成します

3.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.データソースの構成

@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.JdbcTemplateを構成します

(1)前の例からわかるように、プロジェクトがspring-jdbc依存関係を導入している限り、開発者がJdbcTemplateインスタンスを提供しない場合、SpringBootはデフォルトでJdbcTemplateインスタンスを提供します。
(2)複数のデータソースが構成されている場合、開発者はJdbcTemplateのインスタンスを提供します。コードは次のとおりです。

@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.エンティティを作成します

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.コントローラーを作成します

  デモンストレーションの便宜のために、Dao レイヤーと Service レイヤーはここ追加されなくなりました が、2つのJdbcTemplatesが コントローラーに 直接 挿入さ れ、2つのデータベースのデータが別々にクエリされます。


@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.テストを再開します

おすすめ

転載: blog.csdn.net/small_love/article/details/111822955