SpringBoot 配置多个JdbcTemplate的实现步骤
作者:忧伤夏天的风
本文介绍了在SpringBoot中配置多个JdbcTemplate的方法,包括创建项目、添加依赖、配置数据源和多个JdbcTemplate的使用,感兴趣的可以了解一下
前言
开发中使用多数据源配置是一个非常常见的需求。Spring和SpringBoot中,对此都有相应的解决方案。
多数据源的首选分布式数据库中间件MyCat或者Sharing-Jdbc去解决相关问题。使用MyCat,然后分表策略使用sharding-by-intfile。
本文我们只讨论如何在SpringBoot中简单配置多个JdbcTemplate。
一、创建一个SpringBoot 项目,并引入如下依赖
<!--web应用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>二、在application.properties中添加数据源配置
spring.datasource.one.url=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.one.jdbcUrl=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.one.username=*** spring.datasource.one.password=*** spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.two.url=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.two.username=*** spring.datasource.two.password=*** spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
三、新增DadaSourceConfig.java配置多个数据源以及JdbcTemplate,代码如下:
package com.bestoyc.jdbctemplatedemo;
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.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* @author oyc
* @Title: DataSourceConfig
* @ProjectName jdbctemplatedemo
* @Description: TODO
* @date 2019/9/22 0:47
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
@Qualifier("oneDataSource")
DataSource dsOne() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
@Qualifier("twoDataSource")
DataSource dsTwo() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oneJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("oneDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "twoJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("twoDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
四、多个JdbcTemplate使用
@Autowired
@Qualifier("oneJdbcTemplate")
private JdbcTemplate oneJdbcTemplate;
@Autowired
@Qualifier("twoJdbcTemplate")
private JdbcTemplate twoJdbcTemplate;
@RequestMapping("/createUser1")
public String createUser1() {
oneJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12);
return "success";
}
@RequestMapping("/createUser2")
public String createUser2() {
twoJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12);
return "success";
}这里只是简单使用,读者可以根据自己的业务需要添加相应的AOP用户数据源的切换。
到此这篇关于SpringBoot 配置多个JdbcTemplate的文章就介绍到这了,更多相关SpringBoot 配置多个JdbcTemplate内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- spring boot使用sharding jdbc的配置方式
- 详解springboot采用多数据源对JdbcTemplate配置的方法
- springboot2.0.0配置多数据源出现jdbcUrl is required with driverClassName的错误
- SpringBoot多数据源配置详细教程(JdbcTemplate、mybatis)
- 详解Springboot之整合JDBCTemplate配置多数据源
- springboot+springJdbc+postgresql 实现多数据源的配置
- springboot实现以代码的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 读写分离配置的实现
- SpringBoot+MybatisPlus+jdbc连接池配置多数据源的实现
- Spring JDBC配置与使用的实现
