jdbctemplate configure multiple sources of data

First, create a project related to the import dependence

   

1.1

   

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

   

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

<version>5.1.27</version>

</dependency>

   

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.10</version>

</dependency>

   

   

Second, create a User entity class, UserService, UserController

   

2.1

   

In the file application .properties provided multi data source file

   

spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.one.url=jdbc:mysql://127.0.0.1:3306/jdbctemplates?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

spring.datasource.one.username=root

spring.datasource.one.password=123

   

spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.two.url=jdbc:mysql://127.0.0.1:3306/jdbctemplates2?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

spring.datasource.two.username=root

spring.datasource.two.password=123

   

2.2

   

Since we configure multiple data sources, provides automated configuration springboot will fail, we need to be manually configured.

   

Creating DataSourceConfig class configuration and jdbctemplateConfig

   

   

   

@Qualifier

作用告诉他使用哪个DataSource

@ConfigurationProperties

加载application.perteries的哪个数据源

   

   

   

2.2

创建UserService

   

   

3.3

创建Controller

   

   

Guess you like

Origin www.cnblogs.com/fernfei/p/12112026.html