|
|
@@ -12,12 +12,17 @@ import com.template.model.pojo.SmartDataSource;
|
|
|
import com.template.model.result.PageUtils;
|
|
|
import com.template.services.SmartDataSourceService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 数据源配置 服务实现类
|
|
|
@@ -35,30 +40,36 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
|
|
|
@Autowired
|
|
|
private SmartDataClassMapper smartDataClassMapper;
|
|
|
|
|
|
+ private final static DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
+
|
|
|
@Override
|
|
|
public Map<String, Object> insertSmartDataSource(SmartDataSource smartDataSource) {
|
|
|
// 检测参数,还有是否存在重复记录
|
|
|
- Map<String, Object> stringStringMap = validateParams(smartDataSource, "insert");
|
|
|
- if (stringStringMap != null) {
|
|
|
- return stringStringMap;
|
|
|
+ if (smartDataSource.getDsClsId() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源类型id】不能为空!");
|
|
|
+ }
|
|
|
+ SmartDataClass smartDataClass = smartDataClassMapper.selectById(smartDataSource.getDsClsId());
|
|
|
+ if (smartDataClass == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "选择的【数据源类型】不存在!");
|
|
|
}
|
|
|
|
|
|
- // 必填的参数不为null,则进行新增操作
|
|
|
- int result = smartDataSourceMapper.insert(smartDataSource);
|
|
|
- if (result > 0) {
|
|
|
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加成功!");
|
|
|
- } else {
|
|
|
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加失败!");
|
|
|
+ if (smartDataSource.getDsName() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源名称】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataSource.getDsUrl() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源连接地址】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataSource.getDsUser() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源用户】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataSource.getDsPassword() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源密码】不能为空!");
|
|
|
+ }
|
|
|
+ if (smartDataSource.getDsDescrition() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源描述】不能为空!");
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 检测验证参数,和数据库中是否存在重复记录
|
|
|
- private Map<String, Object> validateParams(SmartDataSource smartDataSource, String action) {
|
|
|
- // 看是否有相同的记录
|
|
|
QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
|
|
|
- if ("update".equals(action)) {
|
|
|
- queryWrapper.eq(smartDataSource.getDsId() != null, "ds_id", smartDataSource.getDsId());
|
|
|
- }
|
|
|
queryWrapper.eq(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
|
|
|
queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
|
|
|
queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
|
|
|
@@ -68,27 +79,39 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
|
|
|
queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
|
|
|
List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
|
|
|
if (smartDataSources.size() > 0) {
|
|
|
- if ("update".equals(action)) {
|
|
|
- return CommonUtil.getReturnMap("0", "数据未修改,请修改后再提交!");
|
|
|
- } else {
|
|
|
- return CommonUtil.getReturnMap("0", "有重复记录!");
|
|
|
- }
|
|
|
+ return CommonUtil.getReturnMap("1", "有重复记录!");
|
|
|
}
|
|
|
- // 检测必要参数是否为null
|
|
|
- if ("update".equals(action)) {
|
|
|
- if (smartDataSource.getDsId() == null) {
|
|
|
- return CommonUtil.getReturnMap("0", "【数据源id】不能为空!");
|
|
|
- }
|
|
|
- SmartDataSource sdc = smartDataSourceMapper.selectById(smartDataSource.getDsId());
|
|
|
- if (sdc == null) {
|
|
|
- return CommonUtil.getReturnMap("0", "要修改的【数据源】不存在!");
|
|
|
- }
|
|
|
+
|
|
|
+ Map<String, Object> stringObjectMap = vcoSmartDataSource(smartDataSource);
|
|
|
+ if ("1".equals(stringObjectMap.get("code"))) {
|
|
|
+ smartDataSource.setDsStatus(0);
|
|
|
+ } else {
|
|
|
+ smartDataSource.setDsStatus(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 必填的参数不为null,则进行新增操作
|
|
|
+ int result = smartDataSourceMapper.insert(smartDataSource);
|
|
|
+ if (result > 0) {
|
|
|
+ return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加成功!");
|
|
|
+ } else {
|
|
|
+ return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> updateSmartDataSource(SmartDataSource smartDataSource) {
|
|
|
+ // 检测参数,还有是否存在重复记录
|
|
|
+ if (smartDataSource.getDsId() == null) {
|
|
|
+ return CommonUtil.getReturnMap("0", "【数据源id】不能为空!");
|
|
|
+ }
|
|
|
+ SmartDataSource sdc = smartDataSourceMapper.selectById(smartDataSource.getDsId());
|
|
|
+ if (sdc == null) {
|
|
|
+ return CommonUtil.getReturnMap("0", "要修改的【数据源】不存在!");
|
|
|
}
|
|
|
// 检测必要参数是否为null
|
|
|
if (smartDataSource.getDsClsId() == null) {
|
|
|
return CommonUtil.getReturnMap("0", "【数据源类型id】不能为空!");
|
|
|
}
|
|
|
-
|
|
|
SmartDataClass smartDataClass = smartDataClassMapper.selectById(smartDataSource.getDsClsId());
|
|
|
if (smartDataClass == null) {
|
|
|
return CommonUtil.getReturnMap("0", "选择的【数据源类型】不存在!");
|
|
|
@@ -109,15 +132,18 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
|
|
|
if (smartDataSource.getDsDescrition() == null) {
|
|
|
return CommonUtil.getReturnMap("0", "【数据源描述】不能为空!");
|
|
|
}
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Map<String, Object> updateSmartDataSource(SmartDataSource smartDataSource) {
|
|
|
- // 检测参数,还有是否存在重复记录
|
|
|
- Map<String, Object> stringStringMap = validateParams(smartDataSource, "insert");
|
|
|
- if (stringStringMap != null) {
|
|
|
- return stringStringMap;
|
|
|
+ QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq(smartDataSource.getDsId() != null, "ds_id", smartDataSource.getDsId());
|
|
|
+ queryWrapper.eq(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
|
|
|
+ queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
|
|
|
+ queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
|
|
|
+ queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsUser()), "ds_user", smartDataSource.getDsUser());
|
|
|
+ queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsPassword()), "ds_password", smartDataSource.getDsPassword());
|
|
|
+ queryWrapper.eq(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
|
|
|
+ queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
|
|
|
+ List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
|
|
|
+ if (smartDataSources.size() > 0) {
|
|
|
+ return CommonUtil.getReturnMap("0", "数据未修改,请修改后再提交!");
|
|
|
}
|
|
|
|
|
|
int result = smartDataSourceMapper.updateById(smartDataSource);
|
|
|
@@ -152,4 +178,40 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
|
|
|
SmartDataSource result = smartDataSourceMapper.selectById(id);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> vcoSmartDataSource(SmartDataSource smartDataSource) {
|
|
|
+ // 设置数据源信息
|
|
|
+ if (smartDataSource.getDsClsId() == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "【数据源类型id】不能为空!");
|
|
|
+ }
|
|
|
+ SmartDataClass smartDataClass = smartDataClassMapper.selectById(smartDataSource.getDsClsId());
|
|
|
+ if (smartDataClass == null) {
|
|
|
+ return CommonUtil.getReturnMap("1", "选择的【数据源类型】不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ String dsClsDriver = smartDataClass.getDsClsDriver();
|
|
|
+ dataSource.setDriverClassName(dsClsDriver);
|
|
|
+ dataSource.setUrl(smartDataSource.getDsUrl());
|
|
|
+ dataSource.setUsername(smartDataSource.getDsUser());
|
|
|
+ dataSource.setPassword(smartDataSource.getDsPassword());
|
|
|
+
|
|
|
+ // 尝试获取数据库连接
|
|
|
+ Connection connection = null;
|
|
|
+ try {
|
|
|
+ connection = dataSource.getConnection();
|
|
|
+ assertNotNull(connection);
|
|
|
+ return CommonUtil.getReturnMap(String.valueOf(0), "连接成功!");
|
|
|
+ } catch (SQLException e) {
|
|
|
+ return CommonUtil.getReturnMap(String.valueOf(1), "连接失败:" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|