springboot 1.5.7 +mybatis多数据源配置

本文详细介绍了如何在Spring Boot项目中配置多个数据源,包括MyBatis的集成、Hikari连接池的设置以及Maven依赖的管理。通过具体代码示例,展示了不同数据源的配置方法和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置多个数据源示例如下:

DxMybatisConfig.java

@Configuration
@MapperScan("com.justplay1994.github.dao.dx")
public class DxMybatisConfig {

    @Bean(name = "dxDataSource") //作为一个bean对象并命名
    @ConfigurationProperties(prefix = "spring.dx") //配置文件中,该数据源的前缀
    @Primary   //用于标记主数据源,除了主数据源外,其余注入文件都不添加该注解
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dxSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dxDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/justplay1994/github/dao/dx/*.xml"));//对应mapper.xml的具体位置
        return bean.getObject();
    }

    @Bean(name = "dxTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("dxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "dxSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("dxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

FtMybatisConfig.java

@Configuration
@MapperScan(basePackages = "com.justplay1994.github.dao.ft", sqlSessionFactoryRef = "ftSqlSessionFactory")
public class FtMybatisConfig {

    @Bean(name = "ftDataSource") //作为一个bean对象并命名
    @ConfigurationProperties(prefix = "spring.ft") //配置文件中,该数据源的前缀
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "ftSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("ftDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/justplay1994/github/dao/ft/*.xml"));//对应mapper.xml的具体位置
        return bean.getObject();
    }

    @Bean(name = "ftTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("ftDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ftSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ftSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

配置:

spring:
  ft:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: oracle.jdbc.OracleDriver
    jdbc-url: jdbc:oracle:thin:@10.217.17.71:1521/orcl
    username: DAXING
    password: 123456
    hikari:
      minimum-idle: 5
      maximum-pool-size: 24
      pool-name: ${spring.application.name}-CP
      idle-timeout: 6000    #移除空闲连接的时间
      cachePrepStmts: true
      prepStmtCacheSize: 250
      prepStmtCacheSqlLimit: 2048
      leakDetectionThreshold: 2000
      validation-interval: 5000
      validation-query-timeout: 5000
      max-lifetime: 1800000
      test-while-idle: true
  dx:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://10.217.17.79:3306/moniwa?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: Grand_p0ss
    hikari:
      minimum-idle: 5
      maximum-pool-size: 24
      pool-name: ${spring.application.name}-CP
      idle-timeout: 6000    #移除空闲连接的时间
      cachePrepStmts: true
      prepStmtCacheSize: 250
      prepStmtCacheSqlLimit: 2048
      leakDetectionThreshold: 2000
      validation-interval: 5000
      validation-query-timeout: 5000
      max-lifetime: 1800000
      test-while-idle: true

maven依赖:

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.6</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--使用Hikari连接池,需要禁用springboot内置的tomcat的连接池,同时要引入Hikari连接池的依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.noraui</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>12.2.0.1</version>
    </dependency>

关键点:

  1. Application启动类增加注解:
    @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
  2. 其中一个数据源配置增加@Primay,不加的话会出现sqlsession found 2,spring不知道选哪个。
  3. 另一个数据源在@MapperScan上增加:
    @MapperScan(basePackages = “com.justplay1994.github.dao.ft”, sqlSessionFactoryRef = “ftSqlSessionFactory”)
  4. 数据源扫描不同的包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值