网站做优化好还是推广好,wordpress百家号模版,成武网站建设,做交友类网站适合什么cms文章目录 1、导入 MySQL 和达梦#xff08;DM#xff09;依赖2、在 application-druid.yml 中配置达梦#xff08;DM#xff09;数据源3、在 DruidConfig 类中配置多数据源信息4、在 Service 层或方法级别切换数据源4.1 在 Service 类上切换到从库数据源4.2 在方法级别切换… 文章目录 1、导入 MySQL 和达梦DM依赖2、在 application-druid.yml 中配置达梦DM数据源3、在 DruidConfig 类中配置多数据源信息4、在 Service 层或方法级别切换数据源4.1 在 Service 类上切换到从库数据源4.2 在方法级别切换数据源 本文将详细说明如何在若依RuoYi微服务架构中集成 MySQL 和达梦DM数据库实现多数据源配置。通过配置多个数据源可以灵活管理数据库资源满足不同的数据存储需求。 1、导入 MySQL 和达梦DM依赖
首先在项目的 pom.xml 文件中添加 MySQL 和达梦数据库的驱动依赖以便项目能够连接这两个数据库。
!-- MySQL 驱动包 --
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId
/dependency!-- 达梦 (DM) 驱动包 --
dependencygroupIdcom.dameng/groupIdartifactIdDm7JdbcDriver18/artifactIdversion7.6.0.165/version
/dependency添加上述依赖后项目可以支持 MySQL 和达梦DM数据库连接。
2、在 application-druid.yml 中配置达梦DM数据源
在 application-druid.yml 文件中配置主数据源和从数据源分别指定 MySQL 和达梦数据库连接信息。此配置示例如下
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:# 主库数据源配置MySQLmaster:url: jdbc:mysql://localhost:3306/yourdbusername: yourusernamepassword: yourpassworddriverClassName: com.mysql.cj.jdbc.Driver# 从库数据源配置达梦 DMslave:enabled: true # 启用达梦数据源url: jdbc:dm://localhost:5236/yourdbusername: yourusernamepassword: yourpassworddriverClassName: dm.jdbc.driver.DmDriver# 数据源连接池通用配置initialSize: 5 # 初始连接数minIdle: 10 # 最小空闲连接数maxActive: 20 # 最大活动连接数maxWait: 60000 # 获取连接的最大等待时间通过以上配置MySQL 数据源被设为主数据源master而达梦DM被设为从数据源slave。在从库数据源中将 enabled 设置为 true 以启用该数据源。
3、在 DruidConfig 类中配置多数据源信息
在 DruidConfig 类中定义多数据源的配置信息使应用能够识别并使用配置的 MySQL 和达梦数据库。以下为 DruidConfig 的配置代码示例
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;Configuration
public class DruidConfig {BeanConfigurationProperties(spring.datasource.druid.master)public DataSource masterDataSource(DruidProperties druidProperties) {DruidDataSource dataSource DruidDataSourceBuilder.create().build();return druidProperties.dataSource(dataSource);}BeanConfigurationProperties(spring.datasource.druid.slave)ConditionalOnProperty(prefix spring.datasource.druid.slave, name enabled, havingValue true)public DataSource slaveDataSource(DruidProperties druidProperties) {DruidDataSource dataSource DruidDataSourceBuilder.create().build();return druidProperties.dataSource(dataSource);}
}在 DruidConfig 中我们定义了 masterDataSource 和 slaveDataSource。其中 ConditionalOnProperty 注解确保从库slave在配置 enabled 为 true 时才启用。通过这种方式可以轻松启用或禁用从库数据源。
4、在 Service 层或方法级别切换数据源
通过 DataSource 注解可以在 Service 层或具体方法上灵活地切换数据源。若不指定数据源系统默认会使用主数据源MySQL。
4.1 在 Service 类上切换到从库数据源
在整个 Service 类上添加 DataSource 注解以指定默认使用从库达梦数据源
Service
DataSource(value DataSourceType.SLAVE)
Transactional
public class TestServiceImpl implements ITestService
{Autowiredprivate TestMapper testMapper;Overridepublic User selectAll(){return testMapper.selectAll();}
}此示例中DataSource(value DataSourceType.SLAVE) 指定 TestServiceImpl 使用达梦数据库作为默认数据源。
4.2 在方法级别切换数据源
如果仅希望特定方法使用从库数据源可以在方法上添加 DataSource 注解而类级别默认数据源依然为主库。
Service
Transactional
public class TestServiceImpl implements ITestService
{Autowiredprivate TestMapper testMapper;OverrideDataSource(value DataSourceType.SLAVE)public User selectAll(){return testMapper.selectAll();}
}在这种情况下selectAll 方法使用达梦DM从库数据源其余方法则默认使用主库 MySQL 数据源。
通过上述配置若依项目即可支持 MySQL 和达梦DM多数据源的灵活切换。如果未能成功切换数据源请仔细检查每一步配置并确保所需依赖项和配置文件正确无误。