springboot mysql多数据库_springboot连接多个数据库

yml 配置:

spring:

profiles:

include: DBhnkyyy,DBLogApi

resource 下新增两个文件:

application-DBhnkyyy.yml(这个和上面的 DBhnkyyy 对应)

spring:

datasource:

hnkyyy:

name: hnkyyy

jdbc-url: jdbc:oracle:thin:@localhost:hnkyyy

username: username

password: password

# 使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: oracle.jdbc.driver.OracleDriver

filters: stat

maxActive: 20

initialSize: 1

maxWait: 60000

minIdle: 1

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: select 'x'

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

maxOpenPreparedStatements: 20

这里 hnkyyy 一定不能大写!反正我一大写就报错

application-DBLogApi.yml(这个和上面的 DBLogApi 对应)

spring:

datasource:

logapi:

name: logapi

jdbc-url: jdbc:mysql://localhost:3306/logapi?&characterEncoding=utf-8&serverTimezone=UTC

username: username

password: password

# 使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.cj.jdbc.Driver

filters: stat

maxActive: 20

initialSize: 1

maxWait: 60000

minIdle: 1

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: select 'x'

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

maxOpenPreparedStatements: 20

java 文件:

DataSourceConfig :

@Configuration

public class DataSourceConfig {

// Orcale 数据库

// 这里的 bean 名字要和上面自定义的名字一样

@Bean(name = "hnkyyy")

@ConfigurationProperties(prefix = "spring.datasource.hnkyyy") // application.properteis中对应属性的前缀

public DataSource dataSourceCrm() {

return DataSourceBuilder.create().build();

}

// mysql 数据库

@Bean(name = "logapi")

@ConfigurationProperties(prefix = "spring.datasource.logapi") // application.properteis中对应属性的前缀

public DataSource dataSourcelogapi() {

return DataSourceBuilder.create().build();

}

}

每添加一个数据库,就在这里给一个 DataSource

MybatisDbhnkyyConfig:

@Configuration

@MapperScan(basePackages = {"com.ky.mapper.hnkyyy"}, sqlSessionFactoryRef = "sqlSessionFactoryCrm")

public class MybatisDbhnkyyConfig {

@Autowired

@Qualifier("hnkyyy")

private DataSource hnkyyy;

@Bean

public SqlSessionFactory sqlSessionFactoryCrm() throws Exception {

SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

factoryBean.setDataSource(hnkyyy); // 连接 hnkyyy 库

return factoryBean.getObject();

}

@Bean

public SqlSessionTemplate sqlSessionTemplateCrm() throws Exception {

SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryCrm()); // 使用上面配置的Factory

return template;

}

}

MybatisDbLogApiConfig:

@Configuration

@MapperScan(basePackages = {"com.ky.mapper.logapi"}, sqlSessionFactoryRef = "sqlSessionFactorylogapi")

public class MybatisDbLogApiConfig {

@Autowired

@Qualifier("logapi")

private DataSource logapi;

@Bean

public SqlSessionFactory sqlSessionFactorylogapi() throws Exception {

SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

factoryBean.setDataSource(logapi); // 连接 logapi 库

return factoryBean.getObject();

}

@Bean

public SqlSessionTemplate sqlSessionTemplatelogapi() throws Exception {

SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactorylogapi()); // 使用上面配置的Factory

return template;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值