数据导出功能完善
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.zyplayer.doc.db.controller;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
@@ -92,6 +94,7 @@ public class DbDataViewController {
|
||||
*/
|
||||
@PostMapping(value = "/download")
|
||||
public ResponseJson download(HttpServletResponse response, DataViewParam param) {
|
||||
param.setExecuteId(RandomUtil.simpleUUID());
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
// 获取列信息等
|
||||
TableColumnVo tableColumnVo = dbBaseService.getTableColumnList(param.getSourceId(), param.getDbName(), param.getTableName());
|
||||
@@ -125,17 +128,26 @@ public class DbDataViewController {
|
||||
@PostMapping(value = "/downloadMultiple")
|
||||
public ResponseJson downloadMultiple(HttpServletResponse response, DataViewParam param) {
|
||||
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(param.getSourceId());
|
||||
if (StringUtils.isBlank(param.getTableName())) {
|
||||
if (StringUtils.isBlank(param.getTableNames())) {
|
||||
return DocDbResponseJson.warn("请选择导出的表");
|
||||
}
|
||||
StringBuilder resultSb = new StringBuilder();
|
||||
String[] tableNameArr = param.getTableName().split(",");
|
||||
param.setExecuteId(RandomUtil.simpleUUID());
|
||||
StringBuilder resultSb = new StringBuilder("/*\n" +
|
||||
" zyplayer-doc 表数据导出\n" +
|
||||
"\n" +
|
||||
" 数据库 : " + param.getDbName() + "\n" +
|
||||
" 数据库类型 : " + dbBaseService.getDatabaseProduct().name() + "\n" +
|
||||
" 导出时间 : " + DateTime.now().toString() + "\n" +
|
||||
"*/\n\n");
|
||||
String[] tableNameArr = param.getTableNames().split(",");
|
||||
try {
|
||||
for (String tabName : tableNameArr) {
|
||||
param.setTableName(tabName);
|
||||
// 获取列信息等
|
||||
TableColumnVo tableColumnVo = dbBaseService.getTableColumnList(param.getSourceId(), param.getDbName(), tabName);
|
||||
Set<String> conditionSet = StringUtils.isBlank(param.getConditionColumn()) ? Collections.emptySet() : Stream.of(param.getConditionColumn().split(",")).collect(Collectors.toSet());
|
||||
List<TableColumnDescDto> columnList = tableColumnVo.getColumnList();
|
||||
// 找主键作为更新条件
|
||||
Set<String> conditionSet = columnList.stream().filter(item -> Objects.equals(item.getIspramary(), "1")).map(TableColumnDescDto::getName).collect(Collectors.toSet());
|
||||
// 数据查询
|
||||
String queryAllSql = dbBaseService.getQueryAllSql(param);
|
||||
ExecuteParam executeParam = new ExecuteParam();
|
||||
@@ -150,8 +162,9 @@ public class DbDataViewController {
|
||||
resultSb.append(downloadTableData).append("\n");
|
||||
}
|
||||
String prefix = Objects.equals(param.getDownloadType(), "json") ? ".json" : ".sql";
|
||||
baseDownloadService.sendResponse(response, param.getTableName(), prefix, resultSb.toString());
|
||||
baseDownloadService.sendResponse(response, param.getDbName(), prefix, resultSb.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return DocDbResponseJson.error("导出失败:" + e.getMessage());
|
||||
}
|
||||
return DocDbResponseJson.ok();
|
||||
|
||||
@@ -7,6 +7,8 @@ public class DataViewParam {
|
||||
private Integer pageNum;
|
||||
private String dbName;
|
||||
private String tableName;
|
||||
// 下载多张数据表
|
||||
private String tableNames;
|
||||
private String orderColumn;
|
||||
private String orderType;
|
||||
private String condition;
|
||||
@@ -131,4 +133,12 @@ public class DataViewParam {
|
||||
public void setRetainColumn(String retainColumn) {
|
||||
this.retainColumn = retainColumn;
|
||||
}
|
||||
|
||||
public String getTableNames() {
|
||||
return tableNames;
|
||||
}
|
||||
|
||||
public void setTableNames(String tableNames) {
|
||||
this.tableNames = tableNames;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,10 @@ public class TableColumnDescDto {
|
||||
@ExcelProperty("长度")
|
||||
private String length;
|
||||
|
||||
@ColumnWidth(10)
|
||||
@ExcelProperty("小数点")
|
||||
private String numericScale;
|
||||
|
||||
@ColumnWidth(10)
|
||||
@ExcelProperty("主键")
|
||||
private String ispramary;
|
||||
@@ -99,4 +103,12 @@ public class TableColumnDescDto {
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getNumericScale() {
|
||||
return numericScale;
|
||||
}
|
||||
|
||||
public void setNumericScale(String numericScale) {
|
||||
this.numericScale = numericScale;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<result column="TYPE" property="type" jdbcType="VARCHAR" />
|
||||
<result column="NULLABLE" property="nullable" jdbcType="VARCHAR" />
|
||||
<result column="LENGTH" property="length" jdbcType="VARCHAR" />
|
||||
<result column="NUMERIC_SCALE" property="numericScale" jdbcType="VARCHAR" />
|
||||
<result column="ISPRAMARY" property="ispramary" jdbcType="VARCHAR" />
|
||||
<result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
|
||||
</resultMap>
|
||||
@@ -59,7 +60,10 @@
|
||||
</select>
|
||||
|
||||
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
|
||||
SELECT table_name as TABLE_NAME,COLUMN_NAME NAME,column_comment DESCRIPTION,column_type TYPE,if(is_nullable='YES','允许','不允许') NULLABLE
|
||||
SELECT table_name as TABLE_NAME,COLUMN_NAME as NAME,column_comment DESCRIPTION,data_type TYPE,
|
||||
COALESCE(CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION) as `LENGTH`,NUMERIC_SCALE as NUMERIC_SCALE,
|
||||
if(COLUMN_KEY='PRI','1','0') as ISPRAMARY,
|
||||
if(is_nullable='YES','1','0') NULLABLE
|
||||
FROM `INFORMATION_SCHEMA`.Columns
|
||||
WHERE table_schema=#{dbName}
|
||||
<if test="tableName != null">and table_name=#{tableName}</if>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
<!-- 获取表字段集合 -->
|
||||
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
|
||||
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '允许' when 'N' then '不允许' end NULLABLE, c.COMMENTS
|
||||
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '1' when 'N' then '0' end NULLABLE, c.COMMENTS
|
||||
from user_tab_columns t left join user_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME
|
||||
<where>
|
||||
t.table_name in (select table_name from all_tables where owner = #{dbName} )
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
</select>
|
||||
|
||||
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
|
||||
SELECT table_name as TABLE_NAME,a.COLUMN_NAME as NAME,b.DESCRIPTION as DESCRIPTION,udt_name as TYPE,case when is_nullable ='YES' then '允许' else '不允许' end as NULLABLE , case when character_maximum_length>0 then character_maximum_length else numeric_precision end LENGTH
|
||||
SELECT table_name as TABLE_NAME,a.COLUMN_NAME as NAME,b.DESCRIPTION as DESCRIPTION,udt_name as TYPE,case when is_nullable ='YES' then '1' else '0' end as NULLABLE , case when character_maximum_length>0 then character_maximum_length else numeric_precision end LENGTH
|
||||
FROM information_schema.columns a
|
||||
left join (SELECT a.attname as COLUMN_NAME,col_description(a.attrelid,a.attnum) as DESCRIPTION
|
||||
FROM pg_class as c,pg_attribute as a, pg_namespace as n
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
SELECT (
|
||||
SELECT IS_IDENTITY FROM SYS.ALL_COLUMNS
|
||||
WHERE SYS.ALL_COLUMNS.NAME=SYSCOLUMNS.NAME AND OBJECT_ID = OBJECT_ID(#{tableName})
|
||||
) ISIDENTITY,SYSCOLUMNS.NAME NAME,SYSTYPES.NAME TYPE,Iif(SYSCOLUMNS.ISNULLABLE=1,'允许','不允许') NULLABLE,SYSCOLUMNS.LENGTH LENGTH,PRIMARYINFO.ISPRAMARY
|
||||
) ISIDENTITY,SYSCOLUMNS.NAME NAME,SYSTYPES.NAME TYPE,Iif(SYSCOLUMNS.ISNULLABLE=1,'1','0') NULLABLE,SYSCOLUMNS.LENGTH LENGTH,PRIMARYINFO.ISPRAMARY
|
||||
FROM SYSCOLUMNS
|
||||
LEFT JOIN PRIMARYINFO ON PRIMARYINFO.COLUMNNAME=NAME
|
||||
LEFT JOIN SYSTYPES ON SYSCOLUMNS.XUSERTYPE = SYSTYPES.XUSERTYPE
|
||||
|
||||
@@ -18,10 +18,7 @@ import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* poi导出相关工具
|
||||
@@ -144,11 +141,12 @@ public class PoiUtil {
|
||||
run.setFontSize(18);
|
||||
List<List<String>> dataList = new LinkedList<>();
|
||||
List<TableColumnDescDto> tableColumnDescDtos = columnMap.get(tableInfoVo.getTableName());
|
||||
dataList.add(Arrays.asList("字段名", "是否自增", "类型", "空值", "长度", "主键", "注释"));
|
||||
dataList.add(Arrays.asList("字段名", "是否自增", "类型", "空值", "长度", "小数点", "主键", "注释"));
|
||||
// 写入表格
|
||||
for (TableColumnDescDto dto : tableColumnDescDtos) {
|
||||
dataList.add(Arrays.asList(dto.getName(), dto.getIsidenity(), dto.getType(),
|
||||
dto.getNullable(), dto.getLength(), dto.getIspramary(), dto.getDescription()));
|
||||
String nullable = Objects.equals(dto.getNullable(), "1") ? "允许" : "不允许";
|
||||
dataList.add(Arrays.asList(dto.getName(), dto.getIsidenity(), dto.getType(), nullable, dto.getLength(),
|
||||
dto.getNumericScale(), dto.getIspramary(), dto.getDescription()));
|
||||
}
|
||||
PoiUtil.createTable(document, dataList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user