spring boot mysql mybits 多資料庫
阿新 • • 發佈:2019-02-18
1、使用場景
在開發產品的過程中,經常會遇到需要配置多資料庫的情況,這篇文章記錄一下怎麼配置多mysql資料庫,mybits配置,手動連線mysql總是感覺開發效率不是很高。
2、新增maven依賴包
maven的pom檔案如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>dotamore</groupId>
<artifactId>mutidb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mutidb</name>
<description>Demo project for Spring Boot</description >
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、配置application.properties檔案
配置兩個資料庫連線
# test1庫
spring.datasource.db1.url=jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true
spring.datasource.db1.username=root
spring.datasource.db1.password=pwd
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
# test2庫
spring.datasource.db2.url=jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true
spring.datasource.db2.username=root
spring.datasource.db2.password=pwd
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
4、修改啟動類,關閉資料來源自動配置選項
其實就是將@SpringBootApplication修改為@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
啟動類如下:
package dotamore.mutidb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MutidbApplication {
public static void main(String[] args) {
SpringApplication.run(MutidbApplication.class, args);
}
}
5、資料來源配置
一共需要寫3個配置類,主要作用是將application.properties的資料庫稱對映成java bean物件,再製定bean對應的mybaits資料庫介面包的目錄。三個配置類程式碼如下
5.1、對映application.properties的資料庫為bean物件
DataSourceConfig類
package dotamore.mutidb;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
//被自動掃描
@Configuration
public class DataSourceConfig {
@Bean(name = "db1")//對應配置檔案的spring.datasource.db1
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2")//對應配置檔案的spring.datasource.db2
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
}
MybatisDbAConfig類
package dotamore.mutidb;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
//配置mybatis的介面所在的目錄
@Configuration
@MapperScan(basePackages = {"dotamore.mutidb.db1"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
@Autowired
@Qualifier("db1")
private DataSource ds1;
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
return template;
}
}
MybatisDbBConfig類
package dotamore.mutidb;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"dotamore.mutidb.db2"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
@Autowired
@Qualifier("db2")
private DataSource ds2;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds2);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}
6、測試案例
經過上面5個步驟已經配置完成了,測試測試db1使用能正常使用即可,在test1資料庫中有一張match_video_queue的表格,包含一個int和一個long欄位,其對應的java 實體類物件如下:
MatchVideoQueue類
package dotamore.mutidb.entity;
public class MatchVideoQueue {
public int id;
public long match_id;
@Override
public String toString() {
return "MatchVideoQueue{" +
"id=" + id +
", match_id=" + match_id +
'}';
}
}
在包dotamore.mutidb.db1下寫一個介面MVMapper,查詢資料
package dotamore.mutidb.db1;
import dotamore.mutidb.entity.MatchVideoQueue;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.ArrayList;
@Mapper
public interface MVMapper {
@Select("select id ,match_id from match_video_queue")
ArrayList<MatchVideoQueue> getMatchInfo();
}
最後寫一個測試類,TestConfig
package dotamore.mutidb;
import dotamore.mutidb.entity.MatchVideoQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import dotamore.mutidb.db1.MVMapper;
import java.util.ArrayList;
@RestController
public class TestConfig {
@Autowired
MVMapper mvMapper;
@RequestMapping("/xiao")
public String xiao(){
ArrayList<MatchVideoQueue> amv = mvMapper.getMatchInfo();
System.out.println(amv.size());
return "hehe";
}
}
測試結果如下圖所示,最終成功地獲取了資料: