mirror of https://gitee.com/y_project/RuoYi.git
取消配置文件配置从数据源,改为直接从数据库中直接读取
parent
a87e1d019d
commit
cfcd48b6b2
|
@ -18,5 +18,5 @@ public @interface DataSource
|
||||||
/**
|
/**
|
||||||
* 切换数据源名称
|
* 切换数据源名称
|
||||||
*/
|
*/
|
||||||
public DataSourceType value() default DataSourceType.MASTER;
|
public String value() default "MASTER";
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,5 @@ public enum DataSourceType
|
||||||
/**
|
/**
|
||||||
* 主库
|
* 主库
|
||||||
*/
|
*/
|
||||||
MASTER,
|
MASTER
|
||||||
|
|
||||||
/**
|
|
||||||
* 从库
|
|
||||||
*/
|
|
||||||
SLAVE
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class DataSourceAspect
|
||||||
|
|
||||||
if (StringUtils.isNotNull(dataSource))
|
if (StringUtils.isNotNull(dataSource))
|
||||||
{
|
{
|
||||||
DynamicDataSourceContextHolder.setDateSoureType(dataSource.value().name());
|
DynamicDataSourceContextHolder.setDateSoureType(dataSource.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.framework.config;
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
@ -8,40 +9,64 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||||
import com.ruoyi.common.enums.DataSourceType;
|
import com.ruoyi.common.enums.DataSourceType;
|
||||||
import com.ruoyi.framework.datasource.DynamicDataSource;
|
import com.ruoyi.framework.datasource.DynamicDataSource;
|
||||||
|
import com.ruoyi.framework.datasource.SysDatasource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* druid 配置多数据源
|
* 数据源配置类
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author wangchl
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class DruidConfig
|
public class DruidConfig {
|
||||||
{
|
|
||||||
@Bean
|
|
||||||
@ConfigurationProperties("spring.datasource.druid.master")
|
|
||||||
public DataSource masterDataSource()
|
|
||||||
{
|
|
||||||
return DruidDataSourceBuilder.create().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties("spring.datasource.druid.slave")
|
@ConfigurationProperties("spring.datasource.druid.master")
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
public DataSource masterDataSource() {
|
||||||
public DataSource slaveDataSource()
|
return DruidDataSourceBuilder.create().build();
|
||||||
{
|
}
|
||||||
return DruidDataSourceBuilder.create().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(name = "dynamicDataSource")
|
@Bean
|
||||||
@Primary
|
@ConfigurationProperties("spring.datasource.druid.slave")
|
||||||
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource)
|
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
||||||
{
|
public DataSource slaveDataSource() {
|
||||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
return DruidDataSourceBuilder.create().build();
|
||||||
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
}
|
||||||
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
|
|
||||||
return new DynamicDataSource(masterDataSource, targetDataSources);
|
/**
|
||||||
}
|
*
|
||||||
|
* @param masterDataSource
|
||||||
|
* @param slaveDataSource
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean(name = "dynamicDataSource")
|
||||||
|
@Primary
|
||||||
|
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
|
||||||
|
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||||
|
// 设置数据源列表
|
||||||
|
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
||||||
|
// 从数据库中直接读取数据库
|
||||||
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(masterDataSource);
|
||||||
|
List<SysDatasource> dsList = jdbcTemplate.query("select * from sys_datasource", new Object[] {},
|
||||||
|
new BeanPropertyRowMapper<SysDatasource>(SysDatasource.class));
|
||||||
|
if (dsList != null) {
|
||||||
|
for (SysDatasource ds : dsList) {
|
||||||
|
DruidDataSource dds = new DruidDataSource();
|
||||||
|
dds.setUrl(ds.getUrl());
|
||||||
|
dds.setUsername(ds.getUser());
|
||||||
|
dds.setPassword(ds.getPwd());
|
||||||
|
dds.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||||
|
// dds.setDbType(dbType);
|
||||||
|
targetDataSources.put(ds.getName(), dds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new DynamicDataSource(masterDataSource, targetDataSources);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.ruoyi.framework.datasource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态数据源表 sys_datasource
|
||||||
|
*
|
||||||
|
* @author wangchl
|
||||||
|
* @date 2018-10-10
|
||||||
|
*/
|
||||||
|
public class SysDatasource {
|
||||||
|
|
||||||
|
/** ID */
|
||||||
|
private String id;
|
||||||
|
/** 密码 */
|
||||||
|
private String pwd;
|
||||||
|
/** 用户 */
|
||||||
|
private String user;
|
||||||
|
/** 地址 */
|
||||||
|
private String url;
|
||||||
|
/** 名称 */
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public void setId(String id)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public void setPwd(String pwd)
|
||||||
|
{
|
||||||
|
this.pwd = pwd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPwd()
|
||||||
|
{
|
||||||
|
return pwd;
|
||||||
|
}
|
||||||
|
public void setUser(String user)
|
||||||
|
{
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUser()
|
||||||
|
{
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
public void setUrl(String url)
|
||||||
|
{
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl()
|
||||||
|
{
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
public void setName(String name)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
Navicat Premium Data Transfer
|
||||||
|
|
||||||
|
Source Server : 127.0.0.1
|
||||||
|
Source Server Type : MySQL
|
||||||
|
Source Server Version : 50561
|
||||||
|
Source Host : 127.0.0.1:3306
|
||||||
|
Source Schema : ruoyi
|
||||||
|
|
||||||
|
Target Server Type : MySQL
|
||||||
|
Target Server Version : 50561
|
||||||
|
File Encoding : 65001
|
||||||
|
|
||||||
|
Date: 20/12/2018 11:48:21
|
||||||
|
*/
|
||||||
|
|
||||||
|
SET NAMES utf8mb4;
|
||||||
|
SET FOREIGN_KEY_CHECKS = 0;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Table structure for sys_datasource
|
||||||
|
-- ----------------------------
|
||||||
|
DROP TABLE IF EXISTS `sys_datasource`;
|
||||||
|
CREATE TABLE `sys_datasource` (
|
||||||
|
`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT 'ID',
|
||||||
|
`pwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
|
||||||
|
`user` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户',
|
||||||
|
`url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'IP地址',
|
||||||
|
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '库名称',
|
||||||
|
`remark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '备注'
|
||||||
|
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '动态数据源' ROW_FORMAT = Compact;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Records of sys_datasource
|
||||||
|
-- ----------------------------
|
||||||
|
INSERT INTO `sys_datasource` VALUES ('1', '123456', 'root', 'jdbc:mysql://127.0.0.1:3306/zwsd?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true', 'zwsd', '掌握时代');
|
||||||
|
|
||||||
|
SET FOREIGN_KEY_CHECKS = 1;
|
Loading…
Reference in New Issue