新增table操作功能
This commit is contained in:
parent
337b2a7ebb
commit
bcc29e2cba
@ -4,12 +4,15 @@ import com.cm.common.annotation.CheckRequestBodyAnnotation;
|
||||
import com.cm.common.base.AbstractController;
|
||||
import com.cm.common.constants.ISystemConstant;
|
||||
import com.cm.common.exception.SearchException;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
|
||||
import com.cm.common.plugin.service.database.table.ITableService;
|
||||
import com.cm.common.result.SuccessResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -32,7 +35,12 @@ public class TableController extends AbstractController {
|
||||
@PostMapping("savetable")
|
||||
@CheckRequestBodyAnnotation
|
||||
public SuccessResult saveTable(@RequestBody TableVO tableVO) {
|
||||
return new SuccessResult();
|
||||
return tableService.saveTable(tableVO);
|
||||
}
|
||||
|
||||
@GetMapping("getTableByTableName/{tableName}")
|
||||
public TableDTO getTableByTableName(@PathVariable("tableName") String tableName) {
|
||||
return tableService.getTableByTableName(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,7 +49,7 @@ public class TableController extends AbstractController {
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("listtables")
|
||||
public SuccessResult listTables() throws SearchException {
|
||||
public List<TableDTO> listTables() throws SearchException {
|
||||
Map<String, Object> params = requestParams();
|
||||
return tableService.listTables(params);
|
||||
}
|
||||
@ -53,7 +61,7 @@ public class TableController extends AbstractController {
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("listtablecolumns/{tableName}")
|
||||
public SuccessResult listTableColumns(@PathVariable("tableName") String tableName) throws SearchException {
|
||||
public List<TableColumnDTO> listTableColumns(@PathVariable("tableName") String tableName) throws SearchException {
|
||||
Map<String, Object> params = requestParams();
|
||||
params.put("tableName", tableName);
|
||||
return tableService.listTableColumns(params);
|
||||
|
@ -0,0 +1,62 @@
|
||||
package com.cm.common.plugin.dao.database.table;
|
||||
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: ITableDao
|
||||
* @Description: //TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 10:22
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public interface ITableDao {
|
||||
|
||||
/**
|
||||
* 建表
|
||||
*
|
||||
* @param connection
|
||||
* @param databaseName
|
||||
* @param tableName
|
||||
* @param tableColumns
|
||||
*/
|
||||
void saveTable(Connection connection, String databaseName, String tableName, List<TableColumnVO> tableColumns);
|
||||
|
||||
/**
|
||||
* 表列列表
|
||||
*
|
||||
* @param connection
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
List<TableColumnDTO> listTableColumns(Connection connection, Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 表list
|
||||
*
|
||||
* @param connection
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
List<TableDTO> listTables(Connection connection, Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 通过表名获取表
|
||||
*
|
||||
* @param connection
|
||||
* @param databaseName
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
TableDTO getTableByTableName(Connection connection, String databaseName, String tableName);
|
||||
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package com.cm.common.plugin.dao.database.table.impl;
|
||||
|
||||
import com.cm.common.exception.SaveException;
|
||||
import com.cm.common.exception.SearchException;
|
||||
import com.cm.common.plugin.dao.database.table.ITableDao;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO;
|
||||
import com.cm.common.plugin.service.database.IDatabaseService;
|
||||
import com.cm.common.utils.HashMapUtil;
|
||||
import com.cm.common.utils.WStringUtil;
|
||||
import com.cm.common.utils.jdbc.JdbcUtil;
|
||||
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: TableDaoImpl
|
||||
* @Description: //TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 15:39
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@Repository
|
||||
public class TableDaoImpl implements ITableDao {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TableDaoImpl.class);
|
||||
|
||||
@Override
|
||||
public void saveTable(Connection connection, String databaseName, String tableName, List<TableColumnVO> tableColumns) {
|
||||
// try {
|
||||
// String sql = "";
|
||||
// } catch (SQLException e) {
|
||||
// LOG.error(e.getMessage(), e);
|
||||
// throw new SaveException("建表失败");
|
||||
// }
|
||||
}
|
||||
|
||||
private String getSaveTableSQL(String databaseName, String tableName, List<TableColumnVO> tableColumns) {
|
||||
StringBuilder sql = new StringBuilder("CREATE TABLE `").append(databaseName).append("`.`").append(tableName).append("`");
|
||||
for (TableColumnVO tableColumnVO : tableColumns) {
|
||||
|
||||
}
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableColumnDTO> listTableColumns(Connection connection, Map<String, Object> params) {
|
||||
try {
|
||||
String sql = "SELECT COLUMN_NAME columnName,COLUMN_DEFAULT columnDefault,IS_NULLABLE isNullable,DATA_TYPE dataType,CHARACTER_MAXIMUM_LENGTH characterMaximumLength,NUMERIC_PRECISION numericPrecision,NUMERIC_SCALE numericScale,DATETIME_PRECISION datetimePrecision,COLUMN_TYPE columnType,COLUMN_KEY columnKey,COLUMN_COMMENT columnComment,EXTRA extra FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA`= ? AND `TABLE_NAME`= ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, params.get("databaseName").toString());
|
||||
preparedStatement.setString(2, params.get("tableName").toString());
|
||||
preparedStatement.execute();
|
||||
return HashMapUtil.mapListToBeanList(JdbcUtil.listResult(preparedStatement.getResultSet()), TableColumnDTO.class);
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new SearchException("获取表列失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableDTO> listTables(Connection connection, Map<String, Object> params) {
|
||||
try {
|
||||
String sql = "SELECT TABLE_SCHEMA tableSchema, TABLE_NAME tableName FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, params.get("databaseName").toString());
|
||||
preparedStatement.execute();
|
||||
return HashMapUtil.mapListToBeanList(JdbcUtil.listResult(preparedStatement.getResultSet()), TableDTO.class);
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new SearchException("获取表列表失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableDTO getTableByTableName(Connection connection, String databaseName, String tableName) {
|
||||
try {
|
||||
String sql = "SELECT TABLE_SCHEMA tableSchema, TABLE_NAME tableName FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, databaseName);
|
||||
preparedStatement.setString(2, tableName);
|
||||
preparedStatement.execute();
|
||||
Map<String, Object> tableMap = JdbcUtil.getResult(preparedStatement.getResultSet());
|
||||
if (tableMap == null) {
|
||||
return null;
|
||||
}
|
||||
return HashMapUtil.mapToBean(tableMap, TableDTO.class);
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new SearchException("获取表失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
package com.cm.common.plugin.pojo.dtos.database.table;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: TableColumnDto
|
||||
* @Description: //TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 13:47
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@ApiModel
|
||||
public class TableColumnDTO {
|
||||
|
||||
@ApiModelProperty(name = "columnName", value = "列名")
|
||||
private String columnName;
|
||||
@ApiModelProperty(name = "columnDefault", value = "默认")
|
||||
private String columnDefault;
|
||||
@ApiModelProperty(name = "isNullable", value = "是否为空")
|
||||
private String isNullable;
|
||||
@ApiModelProperty(name = "dataType", value = "数据类型")
|
||||
private String dataType;
|
||||
@ApiModelProperty(name = "characterMaximumLength", value = "字符最大长度")
|
||||
private Long characterMaximumLength;
|
||||
@ApiModelProperty(name = "numericPrecision", value = "数字精度")
|
||||
private Long numericPrecision;
|
||||
@ApiModelProperty(name = "numericScale", value = "数字范围")
|
||||
private Long numericScale;
|
||||
@ApiModelProperty(name = "datetimePrecision", value = "时间精度")
|
||||
private Long datetimePrecision;
|
||||
@ApiModelProperty(name = "columnType", value = "列类型")
|
||||
private String columnType;
|
||||
@ApiModelProperty(name = "columnKey", value = "主键")
|
||||
private String columnKey;
|
||||
@ApiModelProperty(name = "columnComment", value = "说明")
|
||||
private String columnComment;
|
||||
@ApiModelProperty(name = "extra", value = "附加")
|
||||
private String extra;
|
||||
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public void setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
}
|
||||
|
||||
public String getColumnDefault() {
|
||||
return columnDefault;
|
||||
}
|
||||
|
||||
public void setColumnDefault(String columnDefault) {
|
||||
this.columnDefault = columnDefault;
|
||||
}
|
||||
|
||||
public String getIsNullable() {
|
||||
return isNullable;
|
||||
}
|
||||
|
||||
public void setIsNullable(String isNullable) {
|
||||
this.isNullable = isNullable;
|
||||
}
|
||||
|
||||
public String getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public void setDataType(String dataType) {
|
||||
this.dataType = dataType;
|
||||
}
|
||||
|
||||
public Long getCharacterMaximumLength() {
|
||||
return characterMaximumLength;
|
||||
}
|
||||
|
||||
public void setCharacterMaximumLength(Long characterMaximumLength) {
|
||||
this.characterMaximumLength = characterMaximumLength;
|
||||
}
|
||||
|
||||
public Long getNumericPrecision() {
|
||||
return numericPrecision;
|
||||
}
|
||||
|
||||
public void setNumericPrecision(Long numericPrecision) {
|
||||
this.numericPrecision = numericPrecision;
|
||||
}
|
||||
|
||||
public Long getNumericScale() {
|
||||
return numericScale;
|
||||
}
|
||||
|
||||
public void setNumericScale(Long numericScale) {
|
||||
this.numericScale = numericScale;
|
||||
}
|
||||
|
||||
public Long getDatetimePrecision() {
|
||||
return datetimePrecision;
|
||||
}
|
||||
|
||||
public void setDatetimePrecision(Long datetimePrecision) {
|
||||
this.datetimePrecision = datetimePrecision;
|
||||
}
|
||||
|
||||
public String getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
|
||||
public void setColumnType(String columnType) {
|
||||
this.columnType = columnType;
|
||||
}
|
||||
|
||||
public String getColumnKey() {
|
||||
return columnKey;
|
||||
}
|
||||
|
||||
public void setColumnKey(String columnKey) {
|
||||
this.columnKey = columnKey;
|
||||
}
|
||||
|
||||
public String getColumnComment() {
|
||||
return columnComment;
|
||||
}
|
||||
|
||||
public void setColumnComment(String columnComment) {
|
||||
this.columnComment = columnComment;
|
||||
}
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"columnName\":\"")
|
||||
.append(columnName).append('\"');
|
||||
sb.append(",\"columnDefault\":\"")
|
||||
.append(columnDefault).append('\"');
|
||||
sb.append(",\"isNullable\":\"")
|
||||
.append(isNullable).append('\"');
|
||||
sb.append(",\"dataType\":\"")
|
||||
.append(dataType).append('\"');
|
||||
sb.append(",\"characterMaximumLength\":")
|
||||
.append(characterMaximumLength);
|
||||
sb.append(",\"numericPrecision\":")
|
||||
.append(numericPrecision);
|
||||
sb.append(",\"numericScale\":")
|
||||
.append(numericScale);
|
||||
sb.append(",\"datetimePrecision\":")
|
||||
.append(datetimePrecision);
|
||||
sb.append(",\"columnType\":\"")
|
||||
.append(columnType).append('\"');
|
||||
sb.append(",\"columnKey\":\"")
|
||||
.append(columnKey).append('\"');
|
||||
sb.append(",\"columnComment\":\"")
|
||||
.append(columnComment).append('\"');
|
||||
sb.append(",\"extra\":\"")
|
||||
.append(extra).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.cm.common.plugin.pojo.dtos.database.table;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: TableDTO
|
||||
* @Description: //TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 10:25
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@ApiModel
|
||||
public class TableDTO {
|
||||
|
||||
@ApiModelProperty(name = "tableSchema", value = "数据库")
|
||||
private String tableSchema;
|
||||
@ApiModelProperty(name = "tableName", value = "表名")
|
||||
private String tableName;
|
||||
|
||||
public String getTableSchema() {
|
||||
return tableSchema;
|
||||
}
|
||||
|
||||
public void setTableSchema(String tableSchema) {
|
||||
this.tableSchema = tableSchema;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"tableSchema\":\"")
|
||||
.append(tableSchema).append('\"');
|
||||
sb.append(",\"tableName\":\"")
|
||||
.append(tableName).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package com.cm.common.plugin.pojo.vos.database.table;
|
||||
|
||||
import com.cm.common.annotation.CheckBooleanAnnotation;
|
||||
import com.cm.common.annotation.CheckNullAnnotation;
|
||||
import com.cm.common.annotation.CheckNumberAnnotation;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: TableColumnVO
|
||||
* @Description: //TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 14:47
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@ApiModel
|
||||
public class TableColumnVO {
|
||||
|
||||
@CheckNullAnnotation(name = "字段名称")
|
||||
private String fieldName;
|
||||
@CheckNullAnnotation(name = "字段说明")
|
||||
private String fieldComment;
|
||||
@CheckNullAnnotation(name = "字段类型")
|
||||
private String fieldType;
|
||||
@CheckNumberAnnotation(name = "字段长度")
|
||||
private Integer fieldLength;
|
||||
@CheckNumberAnnotation(name = "字段小数点长度")
|
||||
private Integer fieldFloatLength;
|
||||
@CheckBooleanAnnotation(name = "是否为空")
|
||||
private Boolean isNull;
|
||||
@CheckNullAnnotation(name = "默认值")
|
||||
private String defaultValue;
|
||||
|
||||
public String getFieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public void setFieldName(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
public String getFieldComment() {
|
||||
return fieldComment;
|
||||
}
|
||||
|
||||
public void setFieldComment(String fieldComment) {
|
||||
this.fieldComment = fieldComment;
|
||||
}
|
||||
|
||||
public String getFieldType() {
|
||||
return fieldType;
|
||||
}
|
||||
|
||||
public void setFieldType(String fieldType) {
|
||||
this.fieldType = fieldType;
|
||||
}
|
||||
|
||||
public Integer getFieldLength() {
|
||||
return fieldLength;
|
||||
}
|
||||
|
||||
public void setFieldLength(Integer fieldLength) {
|
||||
this.fieldLength = fieldLength;
|
||||
}
|
||||
|
||||
public Integer getFieldFloatLength() {
|
||||
return fieldFloatLength;
|
||||
}
|
||||
|
||||
public void setFieldFloatLength(Integer fieldFloatLength) {
|
||||
this.fieldFloatLength = fieldFloatLength;
|
||||
}
|
||||
|
||||
public Boolean getNull() {
|
||||
return isNull;
|
||||
}
|
||||
|
||||
public void setNull(Boolean aNull) {
|
||||
isNull = aNull;
|
||||
}
|
||||
|
||||
public String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void setDefaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"fieldName\":\"")
|
||||
.append(fieldName).append('\"');
|
||||
sb.append(",\"fieldComment\":\"")
|
||||
.append(fieldComment).append('\"');
|
||||
sb.append(",\"fieldType\":\"")
|
||||
.append(fieldType).append('\"');
|
||||
sb.append(",\"fieldLength\":")
|
||||
.append(fieldLength);
|
||||
sb.append(",\"fieldFloatLength\":")
|
||||
.append(fieldFloatLength);
|
||||
sb.append(",\"isNull\":")
|
||||
.append(isNull);
|
||||
sb.append(",\"defaultValue\":\"")
|
||||
.append(defaultValue).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package com.cm.common.plugin.pojo.vos.database.table;
|
||||
|
||||
import com.cm.common.annotation.CheckBooleanAnnotation;
|
||||
import com.cm.common.annotation.CheckListAnnotation;
|
||||
import com.cm.common.annotation.CheckNullAnnotation;
|
||||
import com.cm.common.annotation.CheckNumberAnnotation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
@ -16,22 +19,10 @@ import com.cm.common.annotation.CheckNumberAnnotation;
|
||||
**/
|
||||
public class TableVO {
|
||||
|
||||
@CheckNullAnnotation(name = "表名", types = {"二狗", "三狗"})
|
||||
@CheckNullAnnotation(name = "表名")
|
||||
private String tableName;
|
||||
@CheckNullAnnotation(name = "字段名称")
|
||||
private String fieldName;
|
||||
@CheckNullAnnotation(name = "字段说明")
|
||||
private String fieldComment;
|
||||
@CheckNullAnnotation(name = "字段类型")
|
||||
private String fieldType;
|
||||
@CheckNumberAnnotation(name = "字段长度", types = {"1", "1"})
|
||||
private Integer fieldLength;
|
||||
@CheckNumberAnnotation(name = "字段小数点长度")
|
||||
private Integer fieldFloatLength;
|
||||
@CheckBooleanAnnotation(name = "是否为空")
|
||||
private Boolean isNull;
|
||||
@CheckNullAnnotation(name = "默认值")
|
||||
private String defaultValue;
|
||||
@CheckListAnnotation(name = "列名")
|
||||
private List<TableColumnVO> tableColumns;
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
@ -41,59 +32,22 @@ public class TableVO {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getFieldName() {
|
||||
return fieldName;
|
||||
public List<TableColumnVO> getTableColumns() {
|
||||
return tableColumns;
|
||||
}
|
||||
|
||||
public void setFieldName(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
public void setTableColumns(List<TableColumnVO> tableColumns) {
|
||||
this.tableColumns = tableColumns;
|
||||
}
|
||||
|
||||
public String getFieldComment() {
|
||||
return fieldComment;
|
||||
}
|
||||
|
||||
public void setFieldComment(String fieldComment) {
|
||||
this.fieldComment = fieldComment;
|
||||
}
|
||||
|
||||
public String getFieldType() {
|
||||
return fieldType;
|
||||
}
|
||||
|
||||
public void setFieldType(String fieldType) {
|
||||
this.fieldType = fieldType;
|
||||
}
|
||||
|
||||
public Integer getFieldLength() {
|
||||
return fieldLength;
|
||||
}
|
||||
|
||||
public void setFieldLength(Integer fieldLength) {
|
||||
this.fieldLength = fieldLength;
|
||||
}
|
||||
|
||||
public Integer getFieldFloatLength() {
|
||||
return fieldFloatLength;
|
||||
}
|
||||
|
||||
public void setFieldFloatLength(Integer fieldFloatLength) {
|
||||
this.fieldFloatLength = fieldFloatLength;
|
||||
}
|
||||
|
||||
public Boolean getNull() {
|
||||
return isNull;
|
||||
}
|
||||
|
||||
public void setNull(Boolean aNull) {
|
||||
isNull = aNull;
|
||||
}
|
||||
|
||||
public String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void setDefaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"tableName\":\"")
|
||||
.append(tableName).append('\"');
|
||||
sb.append(",\"tableColumns\":")
|
||||
.append(tableColumns);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,13 @@
|
||||
package com.cm.common.plugin.service.database.table;
|
||||
|
||||
import com.cm.common.exception.SaveException;
|
||||
import com.cm.common.exception.SearchException;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
|
||||
import com.cm.common.result.SuccessResult;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -17,6 +22,25 @@ import java.util.Map;
|
||||
**/
|
||||
public interface ITableService {
|
||||
|
||||
/**
|
||||
* 新增表
|
||||
*
|
||||
* @param tableVO
|
||||
* @return
|
||||
* @throws SearchException
|
||||
* @throws SaveException
|
||||
*/
|
||||
SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException;
|
||||
|
||||
/**
|
||||
* 通过表名获取表
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
* @throws SearchException
|
||||
*/
|
||||
TableDTO getTableByTableName(String tableName) throws SearchException;
|
||||
|
||||
/**
|
||||
* 表列表
|
||||
*
|
||||
@ -24,7 +48,7 @@ public interface ITableService {
|
||||
* @return
|
||||
* @throws SearchException
|
||||
*/
|
||||
SuccessResult listTables(Map<String, Object> params) throws SearchException;
|
||||
List<TableDTO> listTables(Map<String, Object> params) throws SearchException;
|
||||
|
||||
/**
|
||||
* 表列,列表
|
||||
@ -33,6 +57,7 @@ public interface ITableService {
|
||||
* @return
|
||||
* @throws SearchException
|
||||
*/
|
||||
SuccessResult listTableColumns(Map<String, Object> params) throws SearchException;
|
||||
List<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,18 @@
|
||||
package com.cm.common.plugin.service.database.table.impl;
|
||||
|
||||
import com.cm.common.base.AbstractService;
|
||||
import com.cm.common.exception.SaveException;
|
||||
import com.cm.common.exception.SearchException;
|
||||
import com.cm.common.plugin.dao.database.table.ITableDao;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
|
||||
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
|
||||
import com.cm.common.plugin.service.database.IDatabaseService;
|
||||
import com.cm.common.plugin.service.database.table.ITableService;
|
||||
import com.cm.common.result.SuccessResult;
|
||||
import com.cm.common.result.SuccessResultData;
|
||||
import com.cm.common.utils.HashMapUtil;
|
||||
import com.cm.common.utils.jdbc.JdbcUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Connection;
|
||||
@ -28,45 +34,64 @@ import java.util.Map;
|
||||
@Service
|
||||
public class TableServiceImpl extends AbstractService implements ITableService {
|
||||
|
||||
@Autowired
|
||||
private ITableDao tableDao;
|
||||
|
||||
@Override
|
||||
public SuccessResult listTables(Map<String, Object> params) throws SearchException {
|
||||
public synchronized SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException {
|
||||
Connection connection = getConnectionFromSession();
|
||||
if (connection == null) {
|
||||
throw new SearchException("数据库连接无效");
|
||||
}
|
||||
List<Map<String, Object>> tables;
|
||||
String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
|
||||
String tableName = tableVO.getTableName();
|
||||
TableDTO tableDTO = getTableByTableName(tableName);
|
||||
if (tableDTO != null) {
|
||||
throw new SaveException("表已存在");
|
||||
}
|
||||
try {
|
||||
String sql = "SELECT table_name tableName FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
|
||||
preparedStatement.execute();
|
||||
tables = JdbcUtil.listResult(preparedStatement.getResultSet());
|
||||
connection.setAutoCommit(false);
|
||||
tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns());
|
||||
connection.commit();
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new SearchException("获取表列表失败");
|
||||
try {
|
||||
connection.rollback();
|
||||
} catch (SQLException ex) {
|
||||
LOG.error(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
return new SuccessResultData<>(tables);
|
||||
return new SuccessResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuccessResult listTableColumns(Map<String, Object> params) throws SearchException {
|
||||
public TableDTO getTableByTableName(String tableName) {
|
||||
Connection connection = getConnectionFromSession();
|
||||
if (connection == null) {
|
||||
throw new SearchException("数据库连接无效");
|
||||
}
|
||||
List<Map<String, Object>> columns;
|
||||
try {
|
||||
String sql = "SELECT COLUMN_NAME columnName,COLUMN_DEFAULT columnDefault,IS_NULLABLE isNullable,DATA_TYPE dataType,NUMERIC_PRECISION numericPrecision,NUMERIC_SCALE numericScale,DATETIME_PRECISION datetimePrecision,COLUMN_TYPE columnType,COLUMN_KEY columnKey,COLUMN_COMMENT columnComment,EXTRA extra FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA`= ? AND `TABLE_NAME`= ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
|
||||
preparedStatement.setString(2, params.get("tableName").toString());
|
||||
preparedStatement.execute();
|
||||
columns = JdbcUtil.listResult(preparedStatement.getResultSet());
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new SearchException("获取表列失败");
|
||||
String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
|
||||
return tableDao.getTableByTableName(connection, databaseName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableDTO> listTables(Map<String, Object> params) throws SearchException {
|
||||
Connection connection = getConnectionFromSession();
|
||||
if (connection == null) {
|
||||
throw new SearchException("数据库连接无效");
|
||||
}
|
||||
return new SuccessResultData<>(columns);
|
||||
params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
|
||||
return tableDao.listTables(connection, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableColumnDTO> listTableColumns(Map<String, Object> params) {
|
||||
Connection connection = getConnectionFromSession();
|
||||
if (connection == null) {
|
||||
throw new SearchException("数据库连接无效");
|
||||
}
|
||||
params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
|
||||
return tableDao.listTableColumns(connection, params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,35 +0,0 @@
|
||||
import com.cm.common.exception.ParamsException;
|
||||
import com.cm.common.exception.base.SystemException;
|
||||
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
|
||||
import com.cm.common.utils.annotation.AnnotationUtil;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: AnnotationTest
|
||||
* @Description: TODO
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/14 13:30
|
||||
* @Version: 1.0
|
||||
**/
|
||||
public class AnnotationTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TableVO tableVO = new TableVO();
|
||||
tableVO.setTableName("二狗");
|
||||
tableVO.setFieldName("1");
|
||||
tableVO.setFieldComment("备注");
|
||||
tableVO.setFieldType("t1234");
|
||||
tableVO.setFieldLength(1);
|
||||
tableVO.setFieldFloatLength(0);
|
||||
tableVO.setNull(true);
|
||||
tableVO.setDefaultValue("123");
|
||||
try {
|
||||
AnnotationUtil.checkField(tableVO);
|
||||
} catch (SystemException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user