SpringBoot如何整合多个数据源,看这篇就够了

SpringBoot如今是不少不少公司应用的后端框架,由于它搭建快,能更好、更快速的整合其余第三方。那么随着业务的不断扩展,业务量的增长,这时候就会牵扯到分库分表,虽然这个词听起来很熟悉,做为程序员也很容易理解,可是我想应该也有很多读者没接触过度库分表,今天咱们不聊如何分库分表,而是聊SpringBoot如何整合多个数据源的事情。也就是如何接入不一样的(多个)数据库。java

 

咱们直接开始,咱们直接建立一个干净的SpringBoot应用。mysql

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent>

 

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>​<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency>​<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>​<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>​<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.8</version></dependency>

引入须要的maven坐标,那么咱们这个工程就算搭建起来了,接下来就是配置,如何让SpringBoot整合两个Mysql数据源。首先咱们在本地建立两个数据库test1和test2,同时在里面建立两个结构同样的表。git

CREATE TABLE `user` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',`username` varchar(100) CHARACTER SET utf8 NOT NULL COMMENT '用户名',`password` varchar(100) NOT NULL COMMENT '密码',`create_time` datetime DEFAULT NULL COMMENT '建立时间',PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

在咱们的工程中配置application.yml文件,将数据库的信息配置进去程序员

spring:datasource:test1:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCusername: rootpassword: 1234​test2:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCusername: rootpassword: 1234

接下来就是写咱们的配置类了,这也是整合多个数据源最为关键的部分。github

import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;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.datasource.DataSourceTransactionManager;​import javax.sql.DataSource;​/*** @ClassName DataSource2Config* @Description TODO* @Auther lbt* @Date 2019/6/28/028 10:07* @Version 1.0*/@Configuration@MapperScan(basePackages = "com.example.mapper.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")public class DataSource1Config {​​@Bean(name = "test1DataSource")@ConfigurationProperties(prefix = "spring.datasource.test1")@Primarypublic DataSource test1DataSource() {return DataSourceBuilder.create().build();}​@Bean(name = "test1SqlSessionFactory")@Primarypublic SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}​@Bean(name = "test1TransactionManager")@Primarypublic DataSourceTransactionManager test1TransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}​@Bean(name = "test1SqlSessionTemplate")@Primarypublic SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}}

第二个数据源的配置web

import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;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.datasource.DataSourceTransactionManager;​import javax.sql.DataSource;​@Configuration@MapperScan(basePackages = "com.example.mapper.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")public class DataSource2Config {​​@Bean(name = "test2DataSource")@ConfigurationProperties(prefix = "spring.datasource.test2")public DataSource test2DataSource() {return DataSourceBuilder.create().build();}​@Bean(name = "test2SqlSessionFactory")public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}​@Bean(name = "test2TransactionManager")public DataSourceTransactionManager test2TransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}​@Bean(name = "test2SqlSessionTemplate")public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}}

这样咱们整个的配置其实就算好了,咱们接下来写一个Controller类来测试一下,咱们整合的数据源是否是真的能够用呢?spring

@RestControllerpublic class TestController {​@Autowiredprivate User1Service user1Service;​@Autowiredprivate User2Service user2Service;​@RequestMapping("/user1")public Object user1Controller() {​List<UserPo> all = user1Service.findAll();​return all;}​@RequestMapping("/user2")public Object user2Controller() {​List<UserPo> all = user2Service.findAll();​return all;}}

我写了个两个Controller方法,分别访问不一样的接口,咱们来看下访问结果。sql

当咱们访问user1的时候返回以下:数据库

 

当咱们访问user2的时候访问以下apache

看到这里其实咱们的整个整合也就完成了, 虽然看起来很简单,可是你若是没写过确实会走不少坑,我刚整合的时候就遇到了不少坑,为了帮助你们重复采坑,分享出来供你们参考,另外我已经上传GitHub,你们能够直接拉下来跑。

 

下篇给你们分享一下,这几天在对接微信支付时,遇到的坑,已经微信支付的业务流程。

 

GitHub地址:项目地址

相关文章
相关标签/搜索