本文将详细说明如何在若依(RuoYi)微服务架构中集成 MySQL 和达梦(DM)数据库,实现多数据源配置。通过配置多个数据源,可以灵活管理数据库资源,满足不同的数据存储需求。
首先,在项目的 pom.xml
文件中添加 MySQL
和达梦数据库的驱动依赖,以便项目能够连接这两个数据库。
<!-- MySQL 驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 达梦 (DM) 驱动包 --><dependency><groupId>com.dameng</groupId><artifactId>Dm7JdbcDriver18</artifactId><version>7.6.0.165</version></dependency>
添加上述依赖后,项目可以支持 MySQL
和达梦(DM
)数据库连接。
在 application-druid.yml
文件中配置主数据源和从数据源,分别指定 MySQL
和达梦数据库连接信息。此配置示例如下:
spring:datasource:type: com.alibaba.druid.pool.DruidDataSource
druid:# 主库数据源配置(MySQL)master:url: jdbc:mysql://localhost:3306/yourdb
username: yourusername
password: yourpassword
driverClassName: com.mysql.cj.jdbc.Driver
# 从库数据源配置(达梦 DM)slave:enabled:true# 启用达梦数据源url: jdbc:dm://localhost:5236/yourdb
username: yourusername
password: yourpassword
driverClassName: dm.jdbc.driver.DmDriver
# 数据源连接池通用配置initialSize:5# 初始连接数minIdle:10# 最小空闲连接数maxActive:20# 最大活动连接数maxWait:60000# 获取连接的最大等待时间
通过以上配置,MySQL
数据源被设为主数据源(master
),而达梦(DM
)被设为从数据源(slave
)。在从库数据源中,将 enabled
设置为 true
以启用该数据源。
在 DruidConfig
类中定义多数据源的配置信息,使应用能够识别并使用配置的 MySQL
和达梦数据库。以下为 DruidConfig
的配置代码示例:
importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;@ConfigurationpublicclassDruidConfig{@Bean@ConfigurationProperties("spring.datasource.druid.master")publicDataSourcemasterDataSource(DruidProperties druidProperties){DruidDataSource dataSource =DruidDataSourceBuilder.create().build();return druidProperties.dataSource(dataSource);}@Bean@ConfigurationProperties("spring.datasource.druid.slave")@ConditionalOnProperty(prefix ="spring.datasource.druid.slave", name ="enabled", havingValue ="true")publicDataSourceslaveDataSource(DruidProperties druidProperties){DruidDataSource dataSource =DruidDataSourceBuilder.create().build();return druidProperties.dataSource(dataSource);}}
在 DruidConfig
中,我们定义了 masterDataSource
和 slaveDataSource
。其中 @ConditionalOnProperty
注解确保从库(slave
)在配置 enabled
为 true
时才启用。通过这种方式,可以轻松启用或禁用从库数据源。
通过 @DataSource
注解,可以在 Service
层或具体方法上灵活地切换数据源。若不指定数据源,系统默认会使用主数据源(MySQL
)。
在整个 Service
类上添加 @DataSource
注解,以指定默认使用从库(达梦)数据源:
@Service@DataSource(value =DataSourceType.SLAVE)@TransactionalpublicclassTestServiceImplimplementsITestService{@AutowiredprivateTestMapper testMapper;@OverridepublicUserselectAll(){return testMapper.selectAll();}}
此示例中,@DataSource(value = DataSourceType.SLAVE)
指定 TestServiceImpl
使用达梦数据库作为默认数据源。
如果仅希望特定方法使用从库数据源,可以在方法上添加 @DataSource
注解,而类级别默认数据源依然为主库。
@Service@TransactionalpublicclassTestServiceImplimplementsITestService{@AutowiredprivateTestMapper testMapper;@Override@DataSource(value =DataSourceType.SLAVE)publicUserselectAll(){return testMapper.selectAll();}}
在这种情况下,selectAll
方法使用达梦(DM
)从库数据源,其余方法则默认使用主库 MySQL
数据源。
通过上述配置,若依项目即可支持 MySQL
和达梦(DM
)多数据源的灵活切换。如果未能成功切换数据源,请仔细检查每一步配置,并确保所需依赖项和配置文件正确无误。
提示:请勿发布广告垃圾评论,否则封号处理!!