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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以使用MyBatis-Plus实现多数据配置。多数据配置是指在一个应用中配置多个数据库连接,并能够根据需要选择使用哪个数据库。通过MyBatis-Plus,可以方便地配置和使用多个数据。 在配置多数据时,可以使用MyBatis-Plus提供的注解和配置类来实现。可以根据需要配置多个数据,每个数据对应一个数据库。例如,可以配置MySQL数据库、PostgreSQL数据库和Doris数据库作为不同的数据。 具体配置方法可以参考引用和引用中提到的文章。这些文章详细介绍了如何在SpringBoot中整合MyBatis-Plus并配置动态数据。你可以根据这些文章的参考来配置你的多数据。 在配置完成后,你可以根据需要在代码中选择使用哪个数据进行数据库操作。通过配置多数据,你可以在一个应用中同时操作多个数据库,方便实现复杂的业务逻辑。 总之,SpringBoot和MyBatis-Plus提供了方便的方式来配置和使用多数据,可以满足在一个应用中操作多个数据库的需求。你可以参考相关文档并按照需求进行配置和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【应用】SpringBoot -- 基于 MyBatis-Plus 实现多数据配置](https://blog.csdn.net/zqf787351070/article/details/127775519)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot整合MyBatisPlus配置动态数据的方法](https://download.csdn.net/download/weixin_38659646/12749115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值