若依项目多数据源
最近在二开公司的Wip项目,公司原项目是用C#写的,现在要换成Java,方便后期维护。而后台页面就采用 若依
的前后端分离项目
RuoYi。
一、原项目
在原项目(C#开发)中要连接多个类型不同的数据库:
1 2 3 4 5 6
| if (DBType == 1) db = Helper.GetDbConnection("hkg"); else db = Helper.GetDbConnection("");
|
指定数据源后,去运行定义好的存储过程
1 2 3 4 5
| string sql = string.Format(" 存储过程名 2, '{0}','{1}','{2}' ", code, PpNum, Rev);
DataTable dt = db.Ado.GetDataTable(sql);
|
二、Java项目
这是C#代码,而我二开用的是Java,并且使用了若依
,所以我的配置为:
2.1 修改 application-druid.yml
修改在 ruoyi-admin 模块中 resources 目录下的application-druid.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: master: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://ip:port/数据库名 username: 用户名 password: 密码 live: enabled: true driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://ip:port;Databasename=数据库名 username: 用户名 password: 密码 hkg: enabled: true driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://ip:port;Databasename=数据库名 username: 用户名 password: 密码
|
注意我这里使用了两种数据库,主库为MySql,其他两个从库是SQLServer,你用什么数据库就修改成什么数据库的driverClassName
2.2 修改 DataSourceType 枚举
修改在 ruoyi-common 模块中 enums 包下的 DataSourceType 枚举。修改成这样(具体枚举值是你在application-druid.yml文件中自己定义的数据源名称)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.ruoyi.common.enums;
public enum DataSourceType {
MASTER,
LIVE,
HKG }
|
2.3 修改 DruidConfig 类
修改在 ruoyi-framework 模块中 properties 包下的 DruidConfig 类。在这个类中添加代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @Configuration public class DruidConfig { @Bean @ConfigurationProperties("spring.datasource.druid.master") public DataSource masterDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); }
@Bean @ConfigurationProperties("spring.datasource.druid.live") @ConditionalOnProperty(prefix = "spring.datasource.druid.live", name = "enabled", havingValue = "true") public DataSource slaveDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); }
@Bean @ConfigurationProperties("spring.datasource.druid.hkg") @ConditionalOnProperty(prefix = "spring.datasource.druid.hkg", name = "enabled", havingValue = "true") public DataSource omsDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); }
@Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource masterDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); setDataSource(targetDataSources, DataSourceType.LIVE.name(), "slaveDataSource"); setDataSource(targetDataSources, DataSourceType.HKG.name(), "slaveDataSource"); return new DynamicDataSource(masterDataSource, targetDataSources); }
其他代码...
}
|
2.4 指明是哪个数据源
在编写 ServiceImpl 类时,需要指明是哪个数据源,也可在接口 XXXMapper 类中指明:
方法一、编写 ServiceImpl 类时指明
1 2 3 4 5 6 7 8 9 10 11 12
|
@DataSource(DataSourceType.LIVE) @Transactional(rollbackFor = Exception.class) public Integer UpdateWipData(类型1 参数1) { return xxxMapper.UpdateData(参数1); }
|
对应的 Mapper 类
1
| Integer UpdateData(@Param("别名1") 类型1 参数1);
|
方法二、在接口 XXXMapper 中指明
1 2 3 4 5 6 7 8
|
@DataSource(DataSourceType.SLAVE) List<Map> LIVE_XXX(@Param("type") int type, @Param("code") String code);
|
2.5 XML文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 这是写SQL <select id="UpdateData" resultType="int"> SELECT * FROM sys_user WHERE user_id = #{别名1}; </select>
这是使用存储过程 <select id="LIVE_XXX" statementType="CALLABLE" resultType="map"> { call 存储过程名( #{type,mode=IN,jdbcType=VARCHAR}, #{code,mode=IN,jdbcType=VARCHAR} ) } </select>
|