多数据源配置
- mybatis多数据源配置(依赖引入省略)
- 配置文件
- springboot DB1 config 多文件形式
- springboot DB2 config文件
mybatis多数据源配置(依赖引入省略)
配置文件
spring:
datasource:
db1:
url: jdbc:mysql://127.0.0.1:3306/db?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root1
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
db2:
url: jdbc:mysql://127.0.0.2:3306/db?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root2
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置输出到日志文件--生产环境下还是需要谨慎考虑一下是否开启sql日志,因为日志记录是会进行频繁的IO,会有一定的性能损耗。
#log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
mapper-locations: classpath:mapper
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor()
{
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
springboot DB2 config文件
复制上述文件更改bean 名称以及文件名即可
对应创建mapper 下文件 目录 db1 以及 db2