feat:将刷新全文检索数据接口改为根据FileId判断。
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package com.adc.da;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DataDictionaryGenerator {
|
||||
|
||||
/**
|
||||
* 生成数据字典Word
|
||||
*/
|
||||
@Test
|
||||
public void generate() throws Exception {
|
||||
// Connect to the database
|
||||
String url = "jdbc:mysql://localhost:3306/report_library?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";
|
||||
String username = "root";
|
||||
String password = "rootroot";
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
|
||||
// Get metadata
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
// Specify the database name
|
||||
String databaseName = "report_library"; // Replace "report_library" with the actual database name
|
||||
|
||||
// Get tables in the specified database
|
||||
ResultSet tables = metaData.getTables(databaseName, null, "%", null);
|
||||
|
||||
// Create a new Word document
|
||||
XWPFDocument document = new XWPFDocument();
|
||||
|
||||
int tableCounter = 1;
|
||||
|
||||
// Iterate over the tables
|
||||
while (tables.next()) {
|
||||
String tableName = tables.getString(3);
|
||||
|
||||
// Add the table name to the document
|
||||
XWPFParagraph tableNameParagraph = document.createParagraph();
|
||||
XWPFRun run = tableNameParagraph.createRun();
|
||||
|
||||
// Set table name as "Table 1: 表名"
|
||||
String tableNumber = tableCounter + ": ";
|
||||
String tableNameText = tableNumber + tableName;
|
||||
run.setText(tableNameText);
|
||||
run.setBold(true);
|
||||
|
||||
// Set font size
|
||||
run.setFontSize(12); // 12 points for font size (equivalent to three号字号)
|
||||
|
||||
// Increment table counter
|
||||
tableCounter++;
|
||||
|
||||
// Create table
|
||||
XWPFTable table = document.createTable();
|
||||
CTTblWidth tableWidth = table.getCTTbl().addNewTblPr().addNewTblW();
|
||||
tableWidth.setType(STTblWidth.PCT);
|
||||
tableWidth.setW(BigInteger.valueOf(5000)); // Set the table width (10000 = 100%)
|
||||
|
||||
// create header row
|
||||
XWPFTableRow headerRow = table.getRow(0);
|
||||
headerRow.getCell(0).setText("列名");
|
||||
headerRow.addNewTableCell().setText("中文含义");
|
||||
headerRow.addNewTableCell().setText("数据类型");
|
||||
headerRow.addNewTableCell().setText("主键");
|
||||
headerRow.addNewTableCell().setText("外键");
|
||||
headerRow.addNewTableCell().setText("不为空");
|
||||
headerRow.addNewTableCell().setText("备注");
|
||||
|
||||
// Get columns
|
||||
ResultSet columns = metaData.getColumns(null, null, tableName, "%");
|
||||
|
||||
// get primary keys
|
||||
ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, tableName);
|
||||
Set<String> primaryKeySet = new HashSet<>();
|
||||
while (primaryKeys.next()) {
|
||||
primaryKeySet.add(primaryKeys.getString("COLUMN_NAME"));
|
||||
}
|
||||
|
||||
// Pattern for matching remarks
|
||||
Pattern pattern = Pattern.compile("([^((]+)[((]([^))]+)[))]");
|
||||
|
||||
// Iterate over the columns
|
||||
while (columns.next()) {
|
||||
String columnName = columns.getString(4);
|
||||
int columnSize = columns.getInt("COLUMN_SIZE");
|
||||
String columnType = columns.getString(6) + "(" + columnSize + ")";
|
||||
String isNullable = columns.getString(18);
|
||||
boolean isNotNull = isNullable.equals("NO");
|
||||
|
||||
// Check if column is primary key
|
||||
String isPrimaryKey = primaryKeySet.contains(columnName) ? "是" : "否";
|
||||
|
||||
String isForeignKey = "否";
|
||||
|
||||
// Extracting Chinese meaning and remarks from the remarks column
|
||||
String fullRemarks = columns.getString("REMARKS");
|
||||
Matcher matcher = pattern.matcher(fullRemarks);
|
||||
|
||||
String chineseMeaning = "";
|
||||
String remarks = "";
|
||||
if (matcher.find()) {
|
||||
chineseMeaning = matcher.group(1);
|
||||
remarks = matcher.group(2);
|
||||
} else {
|
||||
chineseMeaning = fullRemarks;
|
||||
}
|
||||
|
||||
// Add the column details to the table
|
||||
XWPFTableRow row = table.createRow();
|
||||
XWPFRun cellRun = row.getCell(0).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(columnName);
|
||||
|
||||
cellRun = row.getCell(1).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(chineseMeaning);
|
||||
|
||||
cellRun = row.getCell(2).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(columnType);
|
||||
|
||||
cellRun = row.getCell(3).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(isPrimaryKey);
|
||||
|
||||
cellRun = row.getCell(4).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(isForeignKey);
|
||||
|
||||
cellRun = row.getCell(5).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(isNotNull ? "是" : "否");
|
||||
|
||||
cellRun = row.getCell(6).getParagraphs().get(0).createRun();
|
||||
cellRun.setFontSize(10);
|
||||
cellRun.setText(remarks);
|
||||
}
|
||||
// Create an empty paragraph
|
||||
XWPFParagraph emptyParagraph = document.createParagraph();
|
||||
emptyParagraph.setSpacingAfter(200); // Set the spacing after the paragraph (adjust the value as needed)
|
||||
}
|
||||
|
||||
// Save the document
|
||||
document.write(Files.newOutputStream(Paths.get("DataDictionary.docx")));
|
||||
|
||||
// Close the document
|
||||
document.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user