传统配置多数据源,静态配置和动态配置。步骤忒多啊,代码量巨大啊,当你需要新增数据源时,牵扯面大大的(yml文件要改吧,datasource类要加吧,等等),不信你看看(下边没有)。
来看看使用别人的劳动成果的快乐:
一、创建工程(Maven工程)【加这一步都多余,谁敲代码不建工程】
这里使用的是SpringBoot3以前的版本,要使用SpringBoot3以后的版本,其余包也需要对应升版本。
二、引入包(SpringBoot、dynamic-datasource、Druid以及数据库的驱动包)
<!--SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.18</version>
</dependency>
<!--动态数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.3.1</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.20</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-autoconfigure</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
三、yml配置(application.yml)
server:
port: 18888
spring:
application:
name: dy-db
datasource:
dynamic:
# 设置默认库
primary: master
# 是否启动严格模式(true启动:不指明数据源则报错;false不启动:不指明数据源则使用默认数据源,即primary对应的数据源)
strict: false
datasource:
master:
url: jdbc:oracle:thin:@localhost:1521:ORCl
username: username
password: 'passwd'
driver-class-name: oracle.jdbc.OracleDriver
druid:
# 初始化建立物理连接个数
initial-size: 5
# 最小连接池数量
min-idle: 5
# 最大连接池数量
max-active: 10
# 获取连接时最大等待时间,单位毫秒
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
# 既作为检测的间隔时间又作为testWhileIdel执行的依据
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
# 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接(配置连接在池中的最小生存时间)
min-evictable-idle-time-millis: 300000
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 获取连接时执行检测,建议关闭,影响性能
test-on-borrow: false
# 归还连接时执行检测,建议关闭,影响性能
test-on-return: false
# 用来检测数据库连接是否有效的sql 必须是一个查询语句(oracle中为 select 1 from dual)
validation-query: SELECT 1 FROM DUAL
# PSCache是否缓存预编译的SQL语句,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭
pool-prepared-statements: true
# 每个连接可缓存的预编译语句的最大数量,开启poolPreparedStatements后生效
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
max-pool-prepared-statement-per-connection-size: 20
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
# 连接属性,这里设置了合并SQL和慢查询的阈值(毫秒)
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
slave:
url: jdbc:mysql://localhost:3306/target_db?useUnicode=true&characterEncoding=UTF-8
username: username
password: 'passwd'
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 5
min-idle: 5
max-active: 10
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
validation-query: SELECT 1
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 去除druid配置
# 根据druid连接池的类型判断是否需要排除:如果druid连接池为starter类型,则需要排除;否则,不需要。
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
四、编码下入口类
package com.cxtd.dy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author fanxiaofeng
* @date 2025/3/10 18:17
* @description
*/
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
启动(运行main函数),报错信息如下:
java.lang.NoClassDefFoundError: org/springframework/boot/bind/RelaxedPropertyResolver
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getExcludeAutoConfigurationsProperty(AutoConfigurationImportSelector.java:205) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getExclusions(AutoConfigurationImportSelector.java:199) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DefaultDeferredImportSelectorGroup.process(ConfigurationClassParser.java:907) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:882) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:812) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:783) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:192) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:756) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:572) ~[spring-context-5.3.31.jar:5.3.31]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.18.jar:2.7.18]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.7.18.jar:2.7.18]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) [spring-boot-2.7.18.jar:2.7.18]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.18.jar:2.7.18]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.7.18.jar:2.7.18]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) [spring-boot-2.7.18.jar:2.7.18]
at com.cxtd.dy.Main.main(Main.java:15) [classes/:na]
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_221]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_221]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_221]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_221]
... 21 common frames omitted
根据报错信息我们查看下pom文件的依赖分析器(没有这玩意的,强制安装下该插件,插件名称Maven Helper),发现包冲突了,导致程序找不到org/springframework/boot/bind/RelaxedPropertyResolver;
依赖包向上对应,那就把spring-boot-autoconfigure-1.5.22.RELEASE给Exclude处理,
或者直接在pom文件里边,添加(上边示例图的Exclude操作可以直接得到下边的pom文件代码);
二次启动 (运行main函数),控制台看到如下信息,说明你已经配置成功了。
问题来了配置成功了,如何用?
在你的Dao层(Hibernate,如果使用的ORM框架是Mybatis,也可以叫Mapper层),进行类注解,dynamic包提供了注解类DS(是不是很像以前配置动态数据源时,自行编码的注解),如使用上边的MySQL库,则注解@DS("slave")。
插叙:在(IDEA的)File->Settings->Plugins->Marketplace的搜索框搜索MyBatisX进行插件安装,它可以帮助你将表转换成实体类、Mapper接口以及XML(*Mapper.xml)文件。