Spring Boot 怎样融合好几个数据库?-betway下载_必威体育下载_betway88必威

SpringBoot现在是许多许多公司肋骨疼使用的后端结构,由于它建立快,能更好、更快速的整合其他第三方。那么跟着事务的不断扩展,事务量的添加,这时分就会牵扯到分库分表,尽管这个词听起来Spring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 很熟悉,作为程序员也很简单了解,可是我想应该也有不少读者没触摸过分库分表,今日咱们不聊怎么分库分表,而是聊SpringBoot怎么整合多个数据源的工作。也便是怎么接入不同的男女交配(多个)数据库。

咱们Spring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 直接开端,咱们直接创立一个洁净的SpringBoot使用。


org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE



org.springframew江涛ork.boot
spring-boot-starter-web


org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2


mysql
mysql-connector-java
runtime


org.springframework.boot
spring-boot-starter-jdbc


org.projectlombok
lombok
1.18.8

引进需求的maven坐标,那么咱们这个工程就算建立起来了,接下来便是装备,怎么让SpringBoot整合两个Mysql数据源。庶女首要咱们在本地创立两个数据库test1和test2,一起在里面创立两个结构相同的表。

CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMME赛尔号柯尔霍德NT '用户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.Driver
jdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&character滑雪大冒险Encoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 1234
test2:
driver-class-name: com.mysql.cj.jdb莲花卷c.Driver
jdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 1234

接下来便是写咱们的装备类了,这也是整合多个数据源最为要害的部分。

import org.apache.ibatis.session.SqlSessionFactor张郦谋y;
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.DataSourceBuSpring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 ilder;
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.DaSpring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 taSource;
/**
* @ClassName DataSource2Config
* @De姐妹双收s美人上床cription 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")
@Primary
public DataSource test1DataSource() {
return DataS福彩3d灯谜总汇ourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
returnSpring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 bean.getObject();
Spring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 }
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager test1TransactionManager(@Qualifier("test1Spring Boot 怎样交融好几个数据库?-betway下载_必威体育下载_betway88必威 DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

第二个数据源的装备

import org.apache.ibatis.session.Sql逝世游戏SessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annot国牛通讯ation.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")
@Confi龙思雷gurationProperties(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类来测验一下,咱们整合的数据源是不是真的可以用呢?

@RestController
public class TestController {
@Autowired
private User1Service user1Service;
@Autowired
private User2Service user2Service;
@RequestMapping("/user1")
public Object user1Controller() {
List all = user1Service.findAll();
return all;
}
@RequestMapping("/user2")
public Object user2Controller() {
List all = user2Service.findAll侯勇();
return all;
}
}

我写了个两个Controller办法,别离拜访不同的接口,咱们来看下拜访成果。

当咱们拜访user1的太阳神云资讯时分回来如下:



当咱们拜访user2的时分拜访如下



看到这儿其实咱们的整个整合也就完成了, 尽管看起来很简单,可是你假如没写过的确会走许多坑,我刚整合的时分就遇到了许多坑,为了协助我们重复采坑,共享出来供我们参阅.

重视我,后续更多干货奉上!

评论(0)