springBoot+多数据源

当我们的数据量很大的时候我们会对数据进行分表分库,而在项目中,我们可能不得不使用跨库操作。

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数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值