当我们的数据量很大的时候我们会对数据进行分表分库,而在项目中,我们可能不得不使用跨库操作。
springboot+单数据源
- pom中加入相关依赖
如sqlserver+mybatis
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
- yml中配置数据库信息
server:
port: 30015
spring:
cloud:
config:
discovery:
enabled: true
service-id: front-configserver
inetutils:
preferred-networks:
- ^192\.168
datasource:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: ***
password: ***
url: jdbc:sqlserver://10.30.30.37:9006;DatabaseName=PKGameStat
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://dev.microservice.com:8700/eureka/
logging:
level: {com.fuyun.front: debug}
- 在springboot+mybatis时直接使用mapper即可
springboot+多数据源
- pom中加入你需要的依赖,同上
- yml中加如多数据源配置
datasource:
pkgamestat:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: ***
password: ***
jdbc-url: jdbc:sqlserver://10.30.30.37:9006;DatabaseName=PKGameStat
qptreasuredb:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: ***
password:***
jdbc-url: jdbc:sqlserver://10.30.30.37:9006;DatabaseName=QPTreasureDB
fund:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: ***
password: ***
jdbc-url: jdbc:sqlserver://10.30.30.37:9006;DatabaseName=FUND
- 在application启动项中加入exclude = {DataSourceAutoConfiguration.class},即注释默认数据库注释,springboot默认为单数据源
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class})
public class RichapiApllication {
public static void main(String[] args) {
SpringApplication.run(RichapiApllication.class, args);
}
}
- 增加配置
/**
* @author: huiliuliu
* @date:2019-01-01
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "pkgamestat")
@ConfigurationProperties(prefix="spring.datasource.pkgamestat")
public DataSource pKGameStatSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "qptreasuredb")
@ConfigurationProperties(prefix="spring.datasource.qptreasuredb")
public DataSource qPTreasureDBDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "fund")
@ConfigurationProperties(prefix="spring.datasource.fund")
public DataSource fundDataSource() {
return DataSourceBuilder.create().build();
}
}
/**
* @author: huiliuliu
* @date:2019-01-01
*/
@Configuration
@MapperScan(basePackages = {"com.fuyun.front.richapi.mapper.fund"}, sqlSessionFactoryRef = "sqlSessionFactory3")
public class FundConfig {
@Autowired
@Qualifier("fund")
private DataSource fund;
@Bean
public SqlSessionFactory sqlSessionFactory3() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(fund);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate3() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory3());
return template;
}
}
其他库配置与上相同
配置完成后再
com.fuyun.front.richapi.mapper.fund目录下的mapper即fund数据库