~
This commit is contained in:
30
mjkf-xinke-plugin/mjkf-xinke-plugin-dbs/pom.xml
Normal file
30
mjkf-xinke-plugin/mjkf-xinke-plugin-dbs/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>mjkf.xinke</groupId>
|
||||
<artifactId>mjkf-xinke-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>mjkf-xinke-plugin-dbs</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<description>多数据源插件</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- 每个插件都要引入自己的对外接口 -->
|
||||
<dependency>
|
||||
<groupId>mjkf.xinke</groupId>
|
||||
<artifactId>mjkf-xinke-plugin-dbs-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus多数据源插件 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,155 @@
|
||||
|
||||
package mjkf.xinke.dbs.config;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.EnumUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbUtil;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.db.handler.EntityListHandler;
|
||||
import cn.hutool.db.sql.SqlExecutor;
|
||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.event.EncDataSourceInitEvent;
|
||||
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
|
||||
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import mjkf.xinke.common.enums.CommonDeleteFlagEnum;
|
||||
import mjkf.xinke.common.pojo.CommonResult;
|
||||
import mjkf.xinke.dbs.modular.entity.DbsStorage;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据源相关配置
|
||||
*
|
||||
*
|
||||
* @date 2022/1/6 23:10
|
||||
*/
|
||||
@Configuration
|
||||
public class DbsConfigure {
|
||||
|
||||
@Resource
|
||||
private OpenApiExtensionResolver openApiExtensionResolver;
|
||||
|
||||
/**
|
||||
* 自定义数据源来源
|
||||
*
|
||||
*
|
||||
* @date 2022/3/8 18:57
|
||||
**/
|
||||
@AllArgsConstructor
|
||||
@Component
|
||||
@Order
|
||||
public static class DynamicDataSourceProvider extends AbstractDataSourceProvider {
|
||||
|
||||
private final DynamicDataSourceProperties dynamicDataSourceProperties;
|
||||
|
||||
@Resource
|
||||
private final MybatisPlusProperties mybatisPlusProperties;
|
||||
|
||||
@Resource
|
||||
private final DefaultDataSourceCreator defaultDataSourceCreator;
|
||||
|
||||
@Override
|
||||
public Map<String, DataSource> loadDataSources() {
|
||||
HashMap<String, DataSource> dataSourceHashMap = MapUtil.newHashMap();
|
||||
Map<String, DataSourceProperty> dataSourcePropertyMap = dynamicDataSourceProperties.getDatasource();
|
||||
if(ObjectUtil.isNotEmpty(dataSourcePropertyMap)) {
|
||||
String primaryDsName = new DynamicDataSourceProperties().getPrimary();
|
||||
DataSourceProperty masterDataSourceProperty = dataSourcePropertyMap.get(primaryDsName);
|
||||
if(ObjectUtil.isNotEmpty(masterDataSourceProperty)) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
if (ObjectUtil.isEmpty(masterDataSourceProperty.getPublicKey())) {
|
||||
masterDataSourceProperty.setPublicKey(dynamicDataSourceProperties.getPublicKey());
|
||||
}
|
||||
EncDataSourceInitEvent encDataSourceInitEvent = new EncDataSourceInitEvent();
|
||||
encDataSourceInitEvent.beforeCreate(masterDataSourceProperty);
|
||||
conn = DriverManager.getConnection(masterDataSourceProperty.getUrl(), masterDataSourceProperty.getUsername(),
|
||||
masterDataSourceProperty.getPassword());
|
||||
String dbsTableName;
|
||||
Object annotationValue = AnnotationUtil.getAnnotationValue(DbsStorage.class, TableName.class);
|
||||
if(ObjectUtil.isNotEmpty(annotationValue)) {
|
||||
dbsTableName = Convert.toStr(annotationValue);
|
||||
} else {
|
||||
dbsTableName = StrUtil.toUnderlineCase(DbsStorage.class.getSimpleName());
|
||||
}
|
||||
GlobalConfig.DbConfig dbConfig = mybatisPlusProperties.getGlobalConfig().getDbConfig();
|
||||
String logicDeleteField = dbConfig.getLogicDeleteField();
|
||||
if(ObjectUtil.isEmpty(logicDeleteField)) {
|
||||
logicDeleteField = "DELETE_FLAG";
|
||||
}
|
||||
String logicNotDeleteValue = dbConfig.getLogicNotDeleteValue();
|
||||
if(ObjectUtil.isEmpty(logicNotDeleteValue)) {
|
||||
logicNotDeleteValue = EnumUtil.toString(CommonDeleteFlagEnum.NOT_DELETE);
|
||||
}
|
||||
List<Entity> entityList = SqlExecutor.query(conn, "SELECT * FROM " + dbsTableName + " WHERE " + logicDeleteField + " = \"" + logicNotDeleteValue + "\"", new EntityListHandler());
|
||||
entityList.forEach(entity -> {
|
||||
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
||||
BeanUtil.copyProperties(entity, dataSourceProperty, true);
|
||||
dataSourceHashMap.put(dataSourceProperty.getPoolName(), defaultDataSourceCreator.createDataSource(dataSourceProperty));
|
||||
});
|
||||
} catch (SQLException ignored) {
|
||||
} finally {
|
||||
DbUtil.close(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dataSourceHashMap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API文档分组配置
|
||||
*
|
||||
*
|
||||
* @date 2022/7/7 16:18
|
||||
**/
|
||||
@Bean(value = "dbsDocApi")
|
||||
public Docket dbsDocApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(new ApiInfoBuilder()
|
||||
.title("多数据源DBS")
|
||||
.description("多数据源DBS")
|
||||
.termsOfServiceUrl("https://www.xiaonuo.vip")
|
||||
.contact(new Contact("SNOWY_TEAM","https://www.xiaonuo.vip", "xuyuxiang29@foxmail.com"))
|
||||
.version("2.0.0")
|
||||
.build())
|
||||
.globalResponseMessage(RequestMethod.GET, CommonResult.responseList())
|
||||
.globalResponseMessage(RequestMethod.POST, CommonResult.responseList())
|
||||
.groupName("多数据源DBS")
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
||||
.apis(RequestHandlerSelectors.basePackage("mjkf.xinke.dbs"))
|
||||
.paths(PathSelectors.any())
|
||||
.build().extensions(openApiExtensionResolver.buildExtensions("多数据源DBS"));
|
||||
}
|
||||
}
|
@@ -0,0 +1,142 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import mjkf.xinke.common.annotation.CommonLog;
|
||||
import mjkf.xinke.common.pojo.CommonResult;
|
||||
import mjkf.xinke.common.pojo.CommonValidList;
|
||||
import mjkf.xinke.dbs.modular.entity.DbsStorage;
|
||||
import mjkf.xinke.dbs.modular.param.*;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableColumnResult;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableResult;
|
||||
import mjkf.xinke.dbs.modular.service.DbsService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据源控制器
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:33
|
||||
**/
|
||||
@Api(tags = "数据源控制器")
|
||||
@ApiSupport(author = "SNOWY_TEAM", order = 1)
|
||||
@RestController
|
||||
@Validated
|
||||
public class DbsController {
|
||||
|
||||
@Resource
|
||||
private DbsService dbsStorageService;
|
||||
|
||||
/**
|
||||
* 获取数据源分页
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:00
|
||||
*/
|
||||
@ApiOperationSupport(order = 1)
|
||||
@ApiOperation("获取数据源分页")
|
||||
@GetMapping("/dbs/storage/page")
|
||||
public CommonResult<Page<DbsStorage>> page(DbsStoragePageParam dbsStoragePageParam) {
|
||||
return CommonResult.data(dbsStorageService.page(dbsStoragePageParam));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:47
|
||||
*/
|
||||
@ApiOperationSupport(order = 2)
|
||||
@ApiOperation("添加数据源")
|
||||
@CommonLog("添加数据源")
|
||||
@PostMapping("/dbs/storage/add")
|
||||
public CommonResult<String> add(@RequestBody @Valid DbsStorageAddParam dbsStorageAddParam) {
|
||||
dbsStorageService.add(dbsStorageAddParam);
|
||||
return CommonResult.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:47
|
||||
*/
|
||||
@ApiOperationSupport(order = 3)
|
||||
@ApiOperation("编辑数据源")
|
||||
@CommonLog("编辑数据源")
|
||||
@PostMapping("/dbs/storage/edit")
|
||||
public CommonResult<String> edit(@RequestBody @Valid DbsStorageEditParam dbsStorageEditParam) {
|
||||
dbsStorageService.edit(dbsStorageEditParam);
|
||||
return CommonResult.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:00
|
||||
*/
|
||||
@ApiOperationSupport(order = 4)
|
||||
@ApiOperation("删除数据源")
|
||||
@CommonLog("删除数据源")
|
||||
@PostMapping("/dbs/storage/delete")
|
||||
public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
|
||||
CommonValidList<DbsStorageIdParam> dbsStorageIdParamList) {
|
||||
dbsStorageService.delete(dbsStorageIdParamList);
|
||||
return CommonResult.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据源详情
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:00
|
||||
*/
|
||||
@ApiOperationSupport(order = 5)
|
||||
@ApiOperation("获取数据源详情")
|
||||
@GetMapping("/dbs/storage/detail")
|
||||
public CommonResult<DbsStorage> detail(@Valid DbsStorageIdParam dbsStorageIdParam) {
|
||||
return CommonResult.data(dbsStorageService.detail(dbsStorageIdParam));
|
||||
}
|
||||
|
||||
/* ====数据源部分所需要用到的选择器==== */
|
||||
|
||||
/**
|
||||
* 获取数据库中所有表
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:35
|
||||
**/
|
||||
@ApiOperationSupport(order = 6)
|
||||
@ApiOperation("获取数据库中所有表")
|
||||
@GetMapping("/dbs/tables")
|
||||
public CommonResult<List<DbsTableResult>> tables() {
|
||||
return CommonResult.data(dbsStorageService.tables());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库表中所有字段
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:35
|
||||
**/
|
||||
@ApiOperationSupport(order = 7)
|
||||
@ApiOperation("获取数据库表中所有字段")
|
||||
@GetMapping("/dbs/tableColumns")
|
||||
public CommonResult<List<DbsTableColumnResult>> tableColumns(@Valid DbsStorageTableColumnParam dbsStorageTableColumnParam) {
|
||||
return CommonResult.data(dbsStorageService.tableColumns(dbsStorageTableColumnParam));
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mjkf.xinke.common.pojo.CommonEntity;
|
||||
|
||||
/**
|
||||
* 数据源实体
|
||||
*
|
||||
*
|
||||
* @date 2022/3/9 9:13
|
||||
**/
|
||||
@Getter
|
||||
@Setter
|
||||
@TableName("EXT_DATABASE")
|
||||
public class DbsStorage extends CommonEntity {
|
||||
|
||||
/** id */
|
||||
@ApiModelProperty(value = "id", position = 1)
|
||||
private String id;
|
||||
|
||||
/** 租户id */
|
||||
@ApiModelProperty(value = "租户id", position = 2)
|
||||
private String tenantId;
|
||||
|
||||
/** 名称 */
|
||||
@ApiModelProperty(value = "名称", position = 3)
|
||||
private String poolName;
|
||||
|
||||
/** 连接URL */
|
||||
@ApiModelProperty(value = "连接URL", position = 4)
|
||||
private String url;
|
||||
|
||||
/** 用户名 */
|
||||
@ApiModelProperty(value = "用户名", position = 5)
|
||||
private String username;
|
||||
|
||||
/** 密码 */
|
||||
@ApiModelProperty(value = "密码", position = 6)
|
||||
private String password;
|
||||
|
||||
/** 驱动名称 */
|
||||
@ApiModelProperty(value = "驱动名称", position = 7)
|
||||
private String driverName;
|
||||
|
||||
/** 分类 */
|
||||
@ApiModelProperty(value = "分类", position = 8)
|
||||
private String category;
|
||||
|
||||
/** 排序码 */
|
||||
@ApiModelProperty(value = "排序码", position = 9)
|
||||
private Integer sortCode;
|
||||
|
||||
/** 扩展信息 */
|
||||
@ApiModelProperty(value = "扩展信息", position = 10)
|
||||
@TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)
|
||||
private String extJson;
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
import mjkf.xinke.common.exception.CommonException;
|
||||
|
||||
/**
|
||||
* 数据源分类枚举
|
||||
*
|
||||
*
|
||||
* @date 2022/7/6 22:21
|
||||
*/
|
||||
@Getter
|
||||
public enum DbsCategoryEnum {
|
||||
|
||||
/**
|
||||
* 主租户的数据源
|
||||
*/
|
||||
MASTER("MASTER"),
|
||||
|
||||
/**
|
||||
* 子租户的数据源
|
||||
*/
|
||||
SLAVE("SLAVE");
|
||||
|
||||
private final String value;
|
||||
|
||||
DbsCategoryEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static void validate(String value) {
|
||||
boolean flag = MASTER.getValue().equals(value) || SLAVE.getValue().equals(value);
|
||||
if(!flag) {
|
||||
throw new CommonException("不支持的数据源分类:{}", value);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import mjkf.xinke.dbs.modular.entity.DbsStorage;
|
||||
|
||||
/**
|
||||
* 数据源Mapper接口
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:42
|
||||
**/
|
||||
public interface DbsMapper extends BaseMapper<DbsStorage> {
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="mjkf.xinke.dbs.modular.mapper.DbsMapper">
|
||||
|
||||
</mapper>
|
@@ -0,0 +1,59 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.param;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 数据源添加参数
|
||||
*
|
||||
*
|
||||
* @date 2022/7/29 9:59
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsStorageAddParam {
|
||||
|
||||
/** 名称 */
|
||||
@ApiModelProperty(value = "名称", required = true, position = 1)
|
||||
@NotBlank(message = "poolName不能为空")
|
||||
private String poolName;
|
||||
|
||||
/** 连接URL */
|
||||
@ApiModelProperty(value = "连接URL", required = true, position = 2)
|
||||
@NotBlank(message = "url不能为空")
|
||||
private String url;
|
||||
|
||||
/** 用户名 */
|
||||
@ApiModelProperty(value = "用户名", required = true, position = 3)
|
||||
@NotBlank(message = "username不能为空")
|
||||
private String username;
|
||||
|
||||
/** 密码 */
|
||||
@ApiModelProperty(value = "密码", required = true, position = 4)
|
||||
@NotBlank(message = "password不能为空")
|
||||
private String password;
|
||||
|
||||
/** 驱动名称 */
|
||||
@ApiModelProperty(value = "驱动名称", required = true, position = 5)
|
||||
@NotBlank(message = "driverName不能为空")
|
||||
private String driverName;
|
||||
|
||||
/** 分类 */
|
||||
@ApiModelProperty(value = "分类", required = true, position = 6)
|
||||
@NotBlank(message = "category不能为空")
|
||||
private String category;
|
||||
|
||||
/** 排序码 */
|
||||
@ApiModelProperty(value = "排序码", required = true, position = 7)
|
||||
@NotNull(message = "sortCode不能为空")
|
||||
private Integer sortCode;
|
||||
|
||||
/** 扩展信息 */
|
||||
@ApiModelProperty(value = "扩展信息", position = 8)
|
||||
private String extJson;
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.param;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 数据源编辑参数
|
||||
*
|
||||
*
|
||||
* @date 2022/7/29 9:59
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsStorageEditParam {
|
||||
|
||||
/** id */
|
||||
@ApiModelProperty(value = "id", required = true, position = 1)
|
||||
@NotBlank(message = "id不能为空")
|
||||
private String id;
|
||||
|
||||
/** 连接URL */
|
||||
@ApiModelProperty(value = "连接URL", required = true, position = 3)
|
||||
@NotBlank(message = "url不能为空")
|
||||
private String url;
|
||||
|
||||
/** 用户名 */
|
||||
@ApiModelProperty(value = "用户名", required = true, position = 4)
|
||||
@NotBlank(message = "username不能为空")
|
||||
private String username;
|
||||
|
||||
/** 密码 */
|
||||
@ApiModelProperty(value = "密码", required = true, position = 5)
|
||||
@NotBlank(message = "password不能为空")
|
||||
private String password;
|
||||
|
||||
/** 排序码 */
|
||||
@ApiModelProperty(value = "排序码", required = true, position = 6)
|
||||
@NotNull(message = "sortCode不能为空")
|
||||
private Integer sortCode;
|
||||
|
||||
/** 扩展信息 */
|
||||
@ApiModelProperty(value = "扩展信息", position = 7)
|
||||
private String extJson;
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.param;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 数据源Id参数
|
||||
*
|
||||
*
|
||||
* @date 2022/7/29 9:59
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsStorageIdParam {
|
||||
|
||||
/** id */
|
||||
@ApiModelProperty(value = "id", required = true)
|
||||
@NotBlank(message = "id不能为空")
|
||||
private String id;
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.param;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 数据源查询参数
|
||||
*
|
||||
*
|
||||
* @date 2022/7/29 10:00
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsStoragePageParam {
|
||||
|
||||
/** 当前页 */
|
||||
@ApiModelProperty(value = "当前页码")
|
||||
private Integer current;
|
||||
|
||||
/** 每页条数 */
|
||||
@ApiModelProperty(value = "每页条数")
|
||||
private Integer size;
|
||||
|
||||
/** 排序字段 */
|
||||
@ApiModelProperty(value = "排序字段,字段驼峰名称,如:userName")
|
||||
private String sortField;
|
||||
|
||||
/** 排序方式 */
|
||||
@ApiModelProperty(value = "排序方式,升序:ASCEND;降序:DESCEND")
|
||||
private String sortOrder;
|
||||
|
||||
/** 数据源分类 */
|
||||
@ApiModelProperty(value = "数据源分类")
|
||||
private String category;
|
||||
|
||||
/** 名称关键词 */
|
||||
@ApiModelProperty(value = "名称关键词")
|
||||
private String searchKey;
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.param;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 数据源库字段列表参数
|
||||
*
|
||||
*
|
||||
* @date 2022/7/29 9:59
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsStorageTableColumnParam {
|
||||
|
||||
/** 表名称 */
|
||||
@ApiModelProperty(value = "表名称", required = true)
|
||||
@NotBlank(message = "表名称不能为空")
|
||||
private String tableName;
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.provider;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import org.springframework.stereotype.Service;
|
||||
import mjkf.xinke.dbs.api.DbsApi;
|
||||
import mjkf.xinke.dbs.modular.param.DbsStorageIdParam;
|
||||
import mjkf.xinke.dbs.modular.param.DbsStorageTableColumnParam;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableColumnResult;
|
||||
import mjkf.xinke.dbs.modular.service.DbsService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据源API接口提供者
|
||||
*
|
||||
*
|
||||
* @date 2022/3/8 16:34
|
||||
**/
|
||||
@Service
|
||||
public class DbsApiProvider implements DbsApi {
|
||||
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
|
||||
@Resource
|
||||
private DbsService dbsStorageService;
|
||||
|
||||
@Override
|
||||
public String getDefaultDataSourceName() {
|
||||
return new DynamicDataSourceProperties().getPrimary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentDataSourceName() {
|
||||
return dbsStorageService.getCurrentDataSourceName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentDataSourceId() {
|
||||
return dbsStorageService.getCurrentDataSourceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getCurrentDataSource() {
|
||||
return dbsStorageService.getCurrentDataSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeDataSource(String name) {
|
||||
dbsStorageService.changeDataSource(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject dbsDetail(String dbsId) {
|
||||
DbsStorageIdParam dbsStorageIdParam = new DbsStorageIdParam();
|
||||
dbsStorageIdParam.setId(dbsId);
|
||||
return JSONUtil.parseObj(dbsStorageService.detail(dbsStorageIdParam));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JSONObject> dbsSelector() {
|
||||
return dbsStorageService.dbsSelector();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JSONObject> tenDbsSelector() {
|
||||
return dbsStorageService.tenDbsSelector();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tableColumns(String tableName) {
|
||||
DbsStorageTableColumnParam dbsStorageTableColumnParam = new DbsStorageTableColumnParam();
|
||||
dbsStorageTableColumnParam.setTableName(tableName);
|
||||
return dbsStorageService.tableColumns(dbsStorageTableColumnParam).stream().map(DbsTableColumnResult::getColumnName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.result;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 数据库表字段结果
|
||||
*
|
||||
*
|
||||
* @date 2022/7/19 19:06
|
||||
**/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsTableColumnResult {
|
||||
|
||||
/** 字段名称 */
|
||||
@ApiModelProperty(value = "字段名称", position = 1)
|
||||
private String columnName;
|
||||
|
||||
/** 字段注释 */
|
||||
@ApiModelProperty(value = "字段注释", position = 2)
|
||||
private String columnRemark;
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.result;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 数据库表结果
|
||||
*
|
||||
*
|
||||
* @date 2022/7/19 19:06
|
||||
**/
|
||||
@Getter
|
||||
@Setter
|
||||
public class DbsTableResult {
|
||||
|
||||
/** 表名称 */
|
||||
@ApiModelProperty(value = "表名称", position = 1)
|
||||
private String tableName;
|
||||
|
||||
/** 表注释 */
|
||||
@ApiModelProperty(value = "表注释", position = 2)
|
||||
private String tableRemark;
|
||||
}
|
@@ -0,0 +1,149 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.service;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import mjkf.xinke.dbs.modular.entity.DbsStorage;
|
||||
import mjkf.xinke.dbs.modular.param.*;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableColumnResult;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableResult;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据源Service接口
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:40
|
||||
**/
|
||||
public interface DbsService extends IService<DbsStorage> {
|
||||
|
||||
/**
|
||||
* 获取数据源分页
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:08
|
||||
*/
|
||||
Page<DbsStorage> page(DbsStoragePageParam dbsStoragePageParam);
|
||||
|
||||
/**
|
||||
* 添加数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 20:48
|
||||
*/
|
||||
void add(DbsStorageAddParam dbsStorageAddParam);
|
||||
|
||||
/**
|
||||
* 编辑数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 21:13
|
||||
*/
|
||||
void edit(DbsStorageEditParam dbsStorageEditParam);
|
||||
|
||||
/**
|
||||
* 删除数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 21:18
|
||||
*/
|
||||
void delete(List<DbsStorageIdParam> dbsStorageIdParamList);
|
||||
|
||||
/**
|
||||
* 获取数据源详情
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 21:18
|
||||
*/
|
||||
DbsStorage detail(DbsStorageIdParam dbsStorageIdParam);
|
||||
|
||||
/**
|
||||
* 获取数据源详情
|
||||
*
|
||||
*
|
||||
* @date 2022/4/24 21:18
|
||||
*/
|
||||
DbsStorage queryEntity(String id);
|
||||
|
||||
/* ====数据源部分所需要用到的选择器==== */
|
||||
|
||||
/**
|
||||
* 获取数据库中所有表
|
||||
*
|
||||
* 返回结果:tableName:表名称,tableRemark:表注释
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:46
|
||||
**/
|
||||
List<DbsTableResult> tables();
|
||||
|
||||
/**
|
||||
* 获取数据库表中所有字段
|
||||
*
|
||||
* 返回结果:columnName:字段名称,columnRemark:字段注释
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:46
|
||||
**/
|
||||
List<DbsTableColumnResult> tableColumns(DbsStorageTableColumnParam dbsStorageTableColumnParam);
|
||||
|
||||
/**
|
||||
* 获取全部数据源列表
|
||||
*
|
||||
*
|
||||
* @date 2022/7/19 18:49
|
||||
**/
|
||||
List<JSONObject> dbsSelector();
|
||||
|
||||
/**
|
||||
* 获取数据源列表,只查询租户类型数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/7/19 18:49
|
||||
**/
|
||||
List<JSONObject> tenDbsSelector();
|
||||
|
||||
/**
|
||||
* 获取默认的数据源名称
|
||||
*
|
||||
*
|
||||
* @date 2022/3/11 14:25
|
||||
**/
|
||||
String getDefaultDataSourceName();
|
||||
|
||||
/**
|
||||
* 获取当前正在使用的数据源名称
|
||||
*
|
||||
*
|
||||
* @date 2022/3/8 16:31
|
||||
**/
|
||||
String getCurrentDataSourceName();
|
||||
|
||||
/**
|
||||
* 获取当前正在使用的数据源ID
|
||||
*
|
||||
*
|
||||
* @date 2022/8/5 14:01
|
||||
**/
|
||||
String getCurrentDataSourceId();
|
||||
|
||||
/**
|
||||
* 获取当前正在使用的数据源
|
||||
*
|
||||
*
|
||||
* @date 2022/3/8 16:31
|
||||
**/
|
||||
DataSource getCurrentDataSource();
|
||||
|
||||
/**
|
||||
* 切换数据源
|
||||
*
|
||||
* @param name 数据源名称
|
||||
*
|
||||
* @date 2022/3/8 16:31
|
||||
**/
|
||||
void changeDataSource(String name);
|
||||
}
|
@@ -0,0 +1,287 @@
|
||||
|
||||
package mjkf.xinke.dbs.modular.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollStreamUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.jdbc.support.JdbcUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import mjkf.xinke.common.enums.CommonSortOrderEnum;
|
||||
import mjkf.xinke.common.exception.CommonException;
|
||||
import mjkf.xinke.common.page.CommonPageRequest;
|
||||
import mjkf.xinke.dbs.modular.entity.DbsStorage;
|
||||
import mjkf.xinke.dbs.modular.enums.DbsCategoryEnum;
|
||||
import mjkf.xinke.dbs.modular.mapper.DbsMapper;
|
||||
import mjkf.xinke.dbs.modular.param.*;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableColumnResult;
|
||||
import mjkf.xinke.dbs.modular.result.DbsTableResult;
|
||||
import mjkf.xinke.dbs.modular.service.DbsService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据源Service接口实现类
|
||||
*
|
||||
*
|
||||
* @date 2022/6/8 19:47
|
||||
**/
|
||||
@Service
|
||||
public class DbsServiceImpl extends ServiceImpl<DbsMapper, DbsStorage> implements DbsService {
|
||||
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
|
||||
@Resource
|
||||
private DefaultDataSourceCreator defaultDataSourceCreator;
|
||||
|
||||
@Override
|
||||
public Page<DbsStorage> page(DbsStoragePageParam dbsStoragePageParam) {
|
||||
QueryWrapper<DbsStorage> queryWrapper = new QueryWrapper<>();
|
||||
// 查询部分字段
|
||||
queryWrapper.lambda().select(DbsStorage::getId, DbsStorage::getPoolName, DbsStorage::getUrl,
|
||||
DbsStorage::getDriverName, DbsStorage::getCategory, DbsStorage::getSortCode);
|
||||
if(ObjectUtil.isNotEmpty(dbsStoragePageParam.getCategory())) {
|
||||
queryWrapper.lambda().eq(DbsStorage::getCategory, dbsStoragePageParam.getCategory());
|
||||
}
|
||||
if(ObjectUtil.isNotEmpty(dbsStoragePageParam.getSearchKey())) {
|
||||
queryWrapper.lambda().like(DbsStorage::getPoolName, dbsStoragePageParam.getSearchKey());
|
||||
}
|
||||
if(ObjectUtil.isAllNotEmpty(dbsStoragePageParam.getSortField(), dbsStoragePageParam.getSortOrder())) {
|
||||
CommonSortOrderEnum.validate(dbsStoragePageParam.getSortOrder());
|
||||
queryWrapper.orderBy(true, dbsStoragePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
|
||||
StrUtil.toUnderlineCase(dbsStoragePageParam.getSortField()));
|
||||
} else {
|
||||
queryWrapper.lambda().orderByAsc(DbsStorage::getSortCode);
|
||||
}
|
||||
return this.page(CommonPageRequest.defaultPage(), queryWrapper);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void add(DbsStorageAddParam dbsStorageAddParam) {
|
||||
checkParam(dbsStorageAddParam);
|
||||
DbsStorage dbsStorage = BeanUtil.toBean(dbsStorageAddParam, DbsStorage.class);
|
||||
// 执行保存
|
||||
this.save(dbsStorage);
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
||||
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
||||
DataSource newDataSource;
|
||||
try {
|
||||
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
||||
BeanUtil.copyProperties(dbsStorage, dataSourceProperty, true);
|
||||
newDataSource = defaultDataSourceCreator.createDataSource(dataSourceProperty);
|
||||
} catch (Exception e) {
|
||||
throw new CommonException("无法连接该数据源");
|
||||
}
|
||||
// 将数据源添加到动态数据源
|
||||
dataSources.put(dbsStorageAddParam.getPoolName(), newDataSource);
|
||||
}
|
||||
|
||||
private void checkParam(DbsStorageAddParam dbsStorageAddParam) {
|
||||
DbsCategoryEnum.validate(dbsStorageAddParam.getCategory());
|
||||
boolean hasSameDbs = this.count(new LambdaQueryWrapper<DbsStorage>()
|
||||
.eq(DbsStorage::getPoolName, dbsStorageAddParam.getPoolName())) > 0;
|
||||
if (hasSameDbs) {
|
||||
throw new CommonException("存在重复的数据源,名称为:{}", dbsStorageAddParam.getPoolName());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void edit(DbsStorageEditParam dbsStorageEditParam) {
|
||||
DbsStorage dbsStorage = this.queryEntity(dbsStorageEditParam.getId());
|
||||
BeanUtil.copyProperties(dbsStorageEditParam, dbsStorage);
|
||||
// 执行更新
|
||||
this.updateById(dbsStorage);
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
||||
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
||||
DataSource newDataSource;
|
||||
try {
|
||||
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
||||
BeanUtil.copyProperties(dbsStorage, dataSourceProperty, true);
|
||||
newDataSource = defaultDataSourceCreator.createDataSource(dataSourceProperty);
|
||||
} catch (Exception e) {
|
||||
throw new CommonException("无法连接该数据源");
|
||||
}
|
||||
// 将数据源添加到动态数据源
|
||||
dataSources.put(dbsStorage.getPoolName(), newDataSource);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void delete(List<DbsStorageIdParam> dbsStorageIdParamList) {
|
||||
List<String> dbsStorageIdList = CollStreamUtil.toList(dbsStorageIdParamList, DbsStorageIdParam::getId);
|
||||
if(ObjectUtil.isNotEmpty(dbsStorageIdList)) {
|
||||
// 获取数据源名称列表
|
||||
List<String> poolNameList = this.listByIds(dbsStorageIdList).stream().map(DbsStorage::getPoolName).collect(Collectors.toList());
|
||||
// 删除
|
||||
this.removeBatchByIds(dbsStorageIdList);
|
||||
poolNameList.forEach(poolName -> {
|
||||
// 移除对应的数据源
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
||||
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
||||
dataSources.remove(poolName);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbsStorage detail(DbsStorageIdParam dbsStorageIdParam) {
|
||||
return this.queryEntity(dbsStorageIdParam.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbsStorage queryEntity(String id) {
|
||||
DbsStorage dbsStorage = this.getById(id);
|
||||
if (ObjectUtil.isEmpty(dbsStorage)) {
|
||||
throw new CommonException("数据源不存在,id值为:{}", id);
|
||||
}
|
||||
return dbsStorage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DbsTableResult> tables() {
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getCurrentDataSource().getConnection();
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String url = metaData.getURL();
|
||||
String schema = null;
|
||||
if (url.toLowerCase().contains("jdbc:oracle")) {
|
||||
schema = metaData.getUserName();
|
||||
}
|
||||
List<DbsTableResult> tables = new ArrayList<>();
|
||||
rs = metaData.getTables(null, schema, "%", new String[]{"TABLE"});
|
||||
while (rs.next()) {
|
||||
String tableName = rs.getString("TABLE_NAME").toUpperCase();
|
||||
if (!tableName.startsWith("ACT_")) {
|
||||
DbsTableResult dbsTableResult = new DbsTableResult();
|
||||
dbsTableResult.setTableName(tableName);
|
||||
String remarks = rs.getString("REMARKS");
|
||||
if(ObjectUtil.isEmpty(remarks)) {
|
||||
dbsTableResult.setTableRemark(tableName);
|
||||
} else {
|
||||
dbsTableResult.setTableRemark(remarks);
|
||||
}
|
||||
tables.add(dbsTableResult);
|
||||
}
|
||||
}
|
||||
return tables;
|
||||
} catch (SQLException sqlException) {
|
||||
sqlException.printStackTrace();
|
||||
throw new CommonException("获取数据库表失败");
|
||||
} finally {
|
||||
JdbcUtils.closeResultSet(rs);
|
||||
JdbcUtils.closeConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DbsTableColumnResult> tableColumns(DbsStorageTableColumnParam dbsStorageTableColumnParam) {
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getCurrentDataSource().getConnection();
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String url = metaData.getURL();
|
||||
String schema = null;
|
||||
if (url.toLowerCase().contains("jdbc:oracle")) {
|
||||
schema = metaData.getUserName();
|
||||
}
|
||||
List<DbsTableColumnResult> columns = new ArrayList<>();
|
||||
rs = metaData.getColumns(null, schema, dbsStorageTableColumnParam.getTableName(), "%");
|
||||
while (rs.next()) {
|
||||
String columnName = rs.getString("COLUMN_NAME").toUpperCase();
|
||||
DbsTableColumnResult dbsTableColumnResult = new DbsTableColumnResult();
|
||||
dbsTableColumnResult.setColumnName(columnName);
|
||||
String remarks = rs.getString("REMARKS");
|
||||
if(ObjectUtil.isEmpty(remarks)) {
|
||||
dbsTableColumnResult.setColumnRemark(columnName);
|
||||
} else {
|
||||
dbsTableColumnResult.setColumnRemark(remarks);
|
||||
}
|
||||
columns.add(dbsTableColumnResult);
|
||||
}
|
||||
return columns;
|
||||
} catch (SQLException sqlException) {
|
||||
sqlException.printStackTrace();
|
||||
throw new CommonException("获取数据库表字段失败,表名称:{}", dbsStorageTableColumnParam.getTableName());
|
||||
} finally {
|
||||
JdbcUtils.closeResultSet(rs);
|
||||
JdbcUtils.closeConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JSONObject> dbsSelector() {
|
||||
return this.list().stream().map(item -> JSONUtil.createObj().set("id", item.getId()).set("poolName", item.getPoolName()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JSONObject> tenDbsSelector() {
|
||||
// 只查询租户类型数据源
|
||||
return this.list(new LambdaQueryWrapper<DbsStorage>().eq(DbsStorage::getCategory, DbsCategoryEnum.SLAVE.getValue())).stream()
|
||||
.map(item -> JSONUtil.createObj().set("id", item.getId()).set("poolName", item.getPoolName())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefaultDataSourceName() {
|
||||
return new DynamicDataSourceProperties().getPrimary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentDataSourceName() {
|
||||
String dataSourceName = DynamicDataSourceContextHolder.peek();
|
||||
if (ObjectUtil.isEmpty(dataSourceName)) {
|
||||
dataSourceName = new DynamicDataSourceProperties().getPrimary();
|
||||
}
|
||||
return dataSourceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentDataSourceId() {
|
||||
String currentDataSourceName = this.getCurrentDataSourceName();
|
||||
if("master".equals(currentDataSourceName)){
|
||||
return "master";
|
||||
}
|
||||
QueryWrapper<DbsStorage> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda().eq(DbsStorage::getPoolName, currentDataSourceName);
|
||||
return this.getOne(queryWrapper).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getCurrentDataSource() {
|
||||
String currentDataSourceName = this.getCurrentDataSourceName();
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
||||
return dynamicRoutingDataSource.getDataSource(currentDataSourceName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeDataSource(String name) {
|
||||
DynamicDataSourceContextHolder.push(name);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user