SpringBoot(四)JdbcTemplate配置多数据源
SpringBoot(四)JdbcTemplate配置多数据源
多数据源
在实际开发中需要配置多个数据源的情况、说的通俗一点就是一个项目中使用多个数据库或者一个数据库实例中多个不同的库的情况;
比如项目需要使用 业务数据库(businessdb)、和 日志数据库(logdb)等多个数据库的情况,或者需要使用种类型数据库的情况;
就需要配置多数据源;
配置 JdbcTemplate 多数据源
1 2 3 4
| 1、JdbcTemplate Maven 依赖引入 2、修改 application.properties 配置文件 3、配置 JDBC 初始化 4、使用多数据源
|
1、JdbcTemplate Maven 依赖引入
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
|
2、修改 application.properties 配置文件
1 2 3 4 5 6 7 8 9 10 11
| # 业务数据库 spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/business-db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSl=true spring.datasource.primary.username=root spring.datasource.primary.password=123456 spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
# 日志数据库 spring.datasource.log.jdbc-url=jdbc:mysql://localhost:3306/log-db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSl=true spring.datasource.log.username=root spring.datasource.log.password=123456 spring.datasource.log.driver-class-name=com.mysql.cj.jdbc.Driver
|
3、配置 JDBC 初始化
创建 DataSourceConfig 类,在项目启动时读取配置文件中的数据库信息,并对 JDBC 进行初始化;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.hqd8080.config;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration public class DataSourceConfig {
@Primary @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); }
@Bean(name = "logyDataSource") @Qualifier("logDataSource") @ConfigurationProperties(prefix = "spring.datasource.log") public DataSource logDataSource() { return DataSourceBuilder.create().build(); }
@Bean(name = "primaryJdbcTemplate") public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); }
@Bean(name = "logJdbcTemplate") public JdbcTemplate logJdbcTemplate(@Qualifier("logDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); }
}
|
4、使用多数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hqd8080;
import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootTest public class JdbcTemplateTest {
@Autowired private JdbcTemplate primaryJdbcTemplate; @Autowired private JdbcTemplate logJdbcTemplate;
@Test public void dataSourceTest() { Student student = new Student("hqd8080", 0 , 20); primaryJdbcTemplate.update("INSERT INTO student(name, sex, age) VALUES(?, ?, ?)", student.getName(), student.getSex(), student.getAge());
logJdbcTemplate.update("INSERT INTO user_login_log(user_id, username, login_time) VALUES(?, ?, ?)", student.getUserId(),student.getUsername(), Now()); } }
|