dep_wangsr
RuoYi 6 years ago committed by Limy
parent 4693f736f0
commit a7b3d22ce7

@ -18,5 +18,5 @@ public @interface DataSource
/**
*
*/
public String value() default "MASTER";
public DataSourceType value() default DataSourceType.MASTER;
}

@ -10,5 +10,10 @@ public enum DataSourceType
/**
*
*/
MASTER
MASTER,
/**
*
*/
SLAVE
}

@ -43,7 +43,7 @@ public class DataSourceAspect
if (StringUtils.isNotNull(dataSource))
{
DynamicDataSourceContextHolder.setDateSoureType(dataSource.value());
DynamicDataSourceContextHolder.setDateSoureType(dataSource.value().name());
}
try

@ -1,7 +1,6 @@
package com.ruoyi.framework.config;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -9,64 +8,40 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.datasource.DynamicDataSource;
import com.ruoyi.framework.datasource.SysDatasource;
/**
*
*
* @author wangchl
* druid
*
* @author ruoyi
*/
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
public class DruidConfig
{
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource()
{
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource()
{
return DruidDataSourceBuilder.create().build();
}
/**
*
* @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);
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
}

@ -1,68 +0,0 @@
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;
}
}

@ -1,38 +0,0 @@
/*
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…
Cancel
Save