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: 1234test2: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地址:項目地址