写在前面:
因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失。故写此篇,以便复用,也希望对大家有点帮助。
随笔内容不高级,如有不妥,不吝指正。
------------------------------------------------------------分-割-线------------------------------------------------------------
第一步:查询数据库
查询语句:
SELECT pretab.TABLE_NAME AS 表名,pretab.TABLE_COMMENT AS 表释义,precol.COLUMN_NAME AS 字段名,precol.COLUMN_TYPE AS 字段类型,precol.COLUMN_DEFAULT AS 字段默认值,precol.COLUMN_COMMENT AS 表字段释义 FROM information_schema.`TABLES` AS pretab RIGHT JOIN information_schema.`COLUMNS` AS precol ON precol.TABLE_NAME=pretab.TABLE_NAME WHERE pretab.TABLE_SCHEMA ="此处填写库名";
结果图示:
第二步:导出查询结果
导出txt:
导出结果:
第三步:一键整合至excel
运行下方代码:
1 import java.io.BufferedReader; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStreamReader; 5 6 import org.apache.poi.hssf.usermodel.HSSFCell; 7 import org.apache.poi.hssf.usermodel.HSSFRow; 8 import org.apache.poi.hssf.usermodel.HSSFSheet; 9 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 10 import org.apache.poi.ss.util.CellRangeAddress; 11 12 /** 13 * 重新整合数据库导出的文件,形成可视化的数据库字典 14 * 15 * @author ruran 16 * @since 2019年7月4日 下午3:25:13 17 */ 18 public class reArrangeFromSQLtxt { 19 20 /* 21 * 导出的数据来源 SELECT pretab.TABLE_NAME AS 表名,pretab.TABLE_COMMENT AS 表释义, 22 * precol.COLUMN_NAME AS 字段名,precol.COLUMN_TYPE AS 字段类型, 23 * precol.COLUMN_DEFAULT AS 字段默认值,precol.COLUMN_COMMENT AS 表字段释义 FROM 24 * information_schema.`TABLES` AS pretab RIGHT JOIN 25 * information_schema.`COLUMNS` AS precol ON 26 * precol.TABLE_NAME=pretab.TABLE_NAME WHERE pretab.TABLE_SCHEMA ="此处填写库名"; 27 */ 28 public static void main(String[] args) { 29 String url = "F:\\2-ME\\中心+部门\\1-scrs学习整理区\\数据库整理 - 暂划区\\"; 30 reArrangeToExcelFromSQLtxt(url, "preScrssit", "@"); 31 } 32 33 /** 34 * 整理出数据导入到excel中 35 * 36 * @author ruran 37 * @since 2019年7月22日 下午2:06:27 38 * @param url 39 * @param fileName 40 * @param splitStr 41 */ 42 private static void reArrangeToExcelFromSQLtxt(String url, String fileName, String splitStr) { 43 44 String fileType = ".txt"; 45 String forFileName = fileName + "_bak"; 46 String forFileType = ".xls"; 47 48 try (FileInputStream fis = new FileInputStream(url + fileName + fileType); 49 InputStreamReader isr = new InputStreamReader(fis); 50 BufferedReader br = new BufferedReader(isr); 51 52 FileOutputStream fos = new FileOutputStream(url + forFileName + forFileType);) { 53 54 String readLine = ""; 55 String tableName = ""; 56 String tableComment = ""; 57 HSSFWorkbook currentWorkbook = new HSSFWorkbook(); 58 HSSFSheet currentSheet = currentWorkbook.createSheet("数据库字典"); 59 HSSFRow currentRow = null; 60 HSSFCell currentCell = null; 61 int rowIndex = -1; 62 while (isNotBlank((readLine = br.readLine()))) { 63 String[] lineSplit = readLine.split(splitStr); 64 int lineSplitLenght = lineSplit.length; 65 String currentTableName = ""; 66 if (lineSplitLenght > 0) { 67 currentTableName = lineSplit[0]; 68 } 69 if (currentTableName.equals(tableName)) { 70 currentRow = currentSheet.createRow(++rowIndex); 71 // 输出列数据 72 for (int i = 2; i < 6; i++) { 73 currentCell = currentRow.createCell((i - 2)); 74 if (lineSplitLenght > i) { 75 currentCell.setCellValue(lineSplit[i]); 76 } 77 } 78 // 开启下次循环 79 continue; 80 } 81 String currentTableComment = ""; 82 if (lineSplitLenght > 1) { 83 currentTableComment = lineSplit[1]; 84 } 85 // 表名和表注释赋值 86 tableName = currentTableName; 87 tableComment = currentTableComment; 88 // 输入空行 89 currentSheet.createRow(++rowIndex); 90 // 切表-表名(注释) 91 currentRow = currentSheet.createRow(++rowIndex); 92 for (int i = 0; i < 4; i++) { 93 currentCell = currentRow.createCell(i); 94 if (i == 0) { 95 currentCell.setCellValue(tableName + "(" + tableComment + ")"); 96 } 97 } 98 CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, 3); 99 currentSheet.addMergedRegion(region); 100 101 // 切表-标题栏 102 currentRow = currentSheet.createRow(++rowIndex); 103 currentCell = currentRow.createCell(0); 104 currentCell.setCellValue("列名"); 105 currentCell = currentRow.createCell(1); 106 currentCell.setCellValue("类型"); 107 currentCell = currentRow.createCell(2); 108 currentCell.setCellValue("默认值"); 109 currentCell = currentRow.createCell(3); 110 currentCell.setCellValue("释义"); 111 currentRow = currentSheet.createRow(++rowIndex); 112 // 切表第一列 113 for (int i = 2; i < 6; i++) { 114 currentCell = currentRow.createCell((i - 2)); 115 if (lineSplitLenght > i) { 116 currentCell.setCellValue(lineSplit[i]); 117 } 118 } 119 } 120 currentWorkbook.write(fos); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 } 124 } 125 126 /** 127 * 字符串判空 128 * 129 * @author ruran 130 * @since 2019年7月23日 下午2:11:28 131 * @param str 132 * @return 133 */ 134 private static boolean isNotBlank(String str) { 135 if (null == str) { 136 return false; 137 } 138 if (str.trim().length() == 0) { 139 return false; 140 } 141 return true; 142 } 143 144 }
运行结果:
原文地址:https://www.cnblogs.com/ruanian/p/11231395.html
时间: 2024-10-29 04:18:28