动态多数据源配置
This commit is contained in:
parent
5f1438bc21
commit
5269f55d6d
|
@ -0,0 +1,75 @@
|
||||||
|
package org.jeecg.modules.data.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 多数据源管理
|
||||||
|
* @Author: caolei
|
||||||
|
* @Date: 2025-04-01
|
||||||
|
* @Version: V1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DataSourceEntity {
|
||||||
|
|
||||||
|
public DataSourceEntity(String id, String code, String name, String dbType, String dbDriver, String dbUrl, String dbName, String dbUsername, String dbPassword, String sysOrgCode){
|
||||||
|
this.id = id;
|
||||||
|
this.code = code;
|
||||||
|
this.name = name;
|
||||||
|
this.dbType = dbType;
|
||||||
|
this.dbDriver = dbDriver;
|
||||||
|
this.dbUrl = dbUrl;
|
||||||
|
this.dbName = dbName;
|
||||||
|
this.dbUsername = dbUsername;
|
||||||
|
this.dbPassword = dbPassword;
|
||||||
|
this.sysOrgCode = sysOrgCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* id
|
||||||
|
*/
|
||||||
|
private String id;
|
||||||
|
/**
|
||||||
|
* 数据源编码
|
||||||
|
*/
|
||||||
|
private String code;
|
||||||
|
/**
|
||||||
|
* 数据源名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String remark;
|
||||||
|
/**
|
||||||
|
* 数据库类型
|
||||||
|
*/
|
||||||
|
private String dbType;
|
||||||
|
/**
|
||||||
|
* 驱动类
|
||||||
|
*/
|
||||||
|
private String dbDriver;
|
||||||
|
/**
|
||||||
|
* 数据源地址
|
||||||
|
*/
|
||||||
|
private String dbUrl;
|
||||||
|
/**
|
||||||
|
* 数据库名称
|
||||||
|
*/
|
||||||
|
private String dbName;
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String dbUsername;
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String dbPassword;
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*/
|
||||||
|
private String createBy;
|
||||||
|
/**
|
||||||
|
* 所属部门
|
||||||
|
*/
|
||||||
|
private String sysOrgCode;
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package org.jeecg.modules.data.loader;
|
||||||
|
|
||||||
|
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||||
|
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
||||||
|
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jeecg.modules.data.util.SecurityUtil;
|
||||||
|
import org.jeecg.modules.data.entity.DataSourceEntity;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class DataSourceLoader {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DruidDataSourceCreator dataSourceCreator;
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
refreshDataSources();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refreshDataSources() {
|
||||||
|
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
|
||||||
|
DataSource mainDataSource = ds.getDataSource("");
|
||||||
|
|
||||||
|
// 从主数据源读取配置
|
||||||
|
List<DataSourceEntity> configs = new JdbcTemplate(mainDataSource).query(
|
||||||
|
"SELECT id, code, name, db_type, db_driver, db_url, db_name, db_username ,db_password ,sys_org_code FROM sys_data_source",
|
||||||
|
(rs, rowNum) -> new DataSourceEntity(
|
||||||
|
rs.getString("id"),
|
||||||
|
rs.getString("code"),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("db_type"),
|
||||||
|
rs.getString("db_driver"),
|
||||||
|
rs.getString("db_url"),
|
||||||
|
rs.getString("db_name"),
|
||||||
|
rs.getString("db_username"),
|
||||||
|
rs.getString("db_password"),
|
||||||
|
rs.getString("sys_org_code")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 创建并添加数据源
|
||||||
|
configs.forEach(config -> {
|
||||||
|
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
||||||
|
dataSourceProperty.setUrl(config.getDbUrl());
|
||||||
|
dataSourceProperty.setUsername(config.getDbUsername());
|
||||||
|
String dbPassword = SecurityUtil.jiemi(config.getDbPassword());
|
||||||
|
dataSourceProperty.setPassword(dbPassword);
|
||||||
|
dataSourceProperty.setDriverClassName(config.getDbDriver());
|
||||||
|
DataSource dynamicDataSource = dataSourceCreator.createDataSource(dataSourceProperty);
|
||||||
|
ds.addDataSource(config.getCode(), dynamicDataSource);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package org.jeecg.modules.data.util;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
||||||
|
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 密码加密解密
|
||||||
|
* @author: lsq
|
||||||
|
* @date: 2020年09月07日 14:26
|
||||||
|
*/
|
||||||
|
public class SecurityUtil {
|
||||||
|
/**加密key*/
|
||||||
|
private static String key = "JEECGBOOT1423670";
|
||||||
|
|
||||||
|
//---AES加密---------begin---------
|
||||||
|
/**加密
|
||||||
|
* @param content
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String jiami(String content) {
|
||||||
|
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
|
||||||
|
String encryptResultStr = aes.encryptHex(content);
|
||||||
|
return encryptResultStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**解密
|
||||||
|
* @param encryptResultStr
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String jiemi(String encryptResultStr){
|
||||||
|
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
|
||||||
|
//解密为字符串
|
||||||
|
String decryptResult = aes.decryptStr(encryptResultStr, CharsetUtil.CHARSET_UTF_8);
|
||||||
|
return decryptResult;
|
||||||
|
}
|
||||||
|
//---AES加密---------end---------
|
||||||
|
/**
|
||||||
|
* 主函数
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String content="test1111";
|
||||||
|
String encrypt = jiami(content);
|
||||||
|
System.out.println(encrypt);
|
||||||
|
//构建
|
||||||
|
String decrypt = jiemi(encrypt);
|
||||||
|
//解密为字符串
|
||||||
|
System.out.println(decrypt);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue