当前位置:   article > 正文

导出mysql数据库表结构文档_导出数据库表结构说明

导出数据库表结构说明

导出文档示例:

 

 说明,文档是基poi工具来生成的(主要是依赖与数据库名正确的话可以直接运行)

一、需要的依赖

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>4.1.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>4.1.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>mysql</groupId>
  13. <artifactId>mysql-connector-java</artifactId>
  14. </dependency>

二具体代码

  1. package com.example.mypoidemo.poi;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.math.BigInteger;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  13. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  14. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  15. import org.apache.poi.xwpf.usermodel.XWPFRun;
  16. import org.apache.poi.xwpf.usermodel.XWPFTable;
  17. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  18. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
  20. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  21. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
  22. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
  23. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol;
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
  27. import com.alibaba.fastjson.JSONObject;
  28. public class WordExportTable {
  29. public static final String driverUrl = "jdbc:mysql://127.0.0.1:3306/snowy-pub?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false";
  30. public static final String username = "root";
  31. public static final String password = "123456";
  32. /**
  33. * 导出数据库需要与driverUrl中连接的数据库一致
  34. */
  35. public static final String dataBase = "snowy-pub";
  36. /**
  37. * 不需要导出的表,可为null
  38. */
  39. public static final String notTbales = "'survey','survey_answer_down'";
  40. /**
  41. * 匹配前缀不导出,可为null
  42. */
  43. public static final String notLike = "'sys_%'";
  44. /**
  45. * 文档标题
  46. */
  47. public static final String title = "数据库设计详细说明书";
  48. /**
  49. * 输出文档地址
  50. */
  51. public static final String path = "d:\\";
  52. /**
  53. * 输出文档名称
  54. */
  55. public static final String fileName = "数据库设计详细说明书V1.0.0.docx";
  56. public static void main(String[] args)throws Exception {
  57. //Blank Document
  58. XWPFDocument document= new XWPFDocument();
  59. //添加标题
  60. XWPFParagraph titleParagraph = document.createParagraph();
  61. //设置段落居中
  62. titleParagraph.setAlignment(ParagraphAlignment.CENTER);
  63. XWPFRun titleParagraphRun = titleParagraph.createRun();
  64. titleParagraphRun.setText(title);
  65. titleParagraphRun.setColor("000000");
  66. titleParagraphRun.setFontSize(20);
  67. WordExportTable we = new WordExportTable();
  68. List<JSONObject> list= we.getTables(dataBase);
  69. for (JSONObject json : list) {
  70. List<String[]> columns = we.getTablesDetail(dataBase, json.getString("name"));
  71. addTable(document, json.getString("name"), json.getString("remark"), columns);
  72. }
  73. //Write the Document in file system
  74. FileOutputStream out = new FileOutputStream(new File(path+fileName));
  75. document.write(out);
  76. out.close();
  77. System.out.println("create_table document written success.");
  78. }
  79. private List<String[]> getTablesDetail(String schema, String tableName){
  80. List<String[]> list = new ArrayList<>();
  81. Connection connection = null;
  82. PreparedStatement preparedStatement = null;
  83. ResultSet resultSet = null;
  84. try {
  85. //加载数据库驱动
  86. Class.forName("com.mysql.jdbc.Driver");
  87. //通过驱动管理类获取数据库链接
  88. connection = DriverManager.getConnection(driverUrl, username, password);
  89. //定义sql语句 ?表示占位符
  90. String sql = "SELECT COLUMN_NAME , COLUMN_TYPE , COLUMN_DEFAULT , IS_NULLABLE , COLUMN_COMMENT "
  91. +" FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? and table_name = ? ";
  92. //获取预处理statement
  93. preparedStatement = connection.prepareStatement(sql);
  94. //设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
  95. preparedStatement.setString(1, schema);
  96. preparedStatement.setString(2, tableName);
  97. //向数据库发出sql执行查询,查询出结果集
  98. resultSet = preparedStatement.executeQuery();
  99. int i = 1;
  100. //遍历查询结果集
  101. while(resultSet.next()){
  102. String[] str = new String[7];
  103. str[0] = i+"";
  104. str[1] = resultSet.getString("COLUMN_NAME");
  105. str[2] = resultSet.getString("COLUMN_TYPE");
  106. str[3] = resultSet.getString("COLUMN_DEFAULT");
  107. str[4] = resultSet.getString("IS_NULLABLE");
  108. str[5] = "";
  109. str[6] = resultSet.getString("COLUMN_COMMENT");
  110. list.add(str);
  111. i++;
  112. }
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }finally{
  116. //释放资源
  117. if(resultSet!=null){
  118. try {
  119. resultSet.close();
  120. } catch (SQLException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. if(preparedStatement!=null){
  125. try {
  126. preparedStatement.close();
  127. } catch (SQLException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. if(connection!=null){
  132. try {
  133. connection.close();
  134. } catch (SQLException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139. return list;
  140. }
  141. private List<JSONObject> getTables(String schema){
  142. List<JSONObject> list = new ArrayList<>();
  143. Connection connection = null;
  144. PreparedStatement preparedStatement = null;
  145. ResultSet resultSet = null;
  146. try {
  147. //加载数据库驱动
  148. Class.forName("com.mysql.jdbc.Driver");
  149. //通过驱动管理类获取数据库链接
  150. connection = DriverManager.getConnection(driverUrl, username, password);
  151. //定义sql语句 ?表示占位符
  152. StringBuffer sql = new StringBuffer();
  153. sql.append("select TABLE_NAME,TABLE_COMMENT from information_schema.tables where table_schema= ? ");
  154. if(null != notLike){
  155. sql.append(" AND table_name NOT LIKE "+notLike);
  156. }
  157. if(null != notTbales){
  158. sql.append(" AND table_name NOT IN ("+notTbales+")");
  159. }
  160. //获取预处理statement
  161. preparedStatement = connection.prepareStatement(sql.toString());
  162. //设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
  163. preparedStatement.setString(1, schema);
  164. //向数据库发出sql执行查询,查询出结果集
  165. resultSet = preparedStatement.executeQuery();
  166. //遍历查询结果集
  167. while(resultSet.next()){
  168. JSONObject j = new JSONObject();
  169. j.put("name", resultSet.getString("TABLE_NAME"));
  170. j.put("remark", resultSet.getString("TABLE_COMMENT"));
  171. list.add(j);
  172. }
  173. } catch (Exception e) {
  174. e.printStackTrace();
  175. }finally{
  176. //释放资源
  177. if(resultSet!=null){
  178. try {
  179. resultSet.close();
  180. } catch (SQLException e) {
  181. e.printStackTrace();
  182. }
  183. }
  184. if(preparedStatement!=null){
  185. try {
  186. preparedStatement.close();
  187. } catch (SQLException e) {
  188. e.printStackTrace();
  189. }
  190. }
  191. if(connection!=null){
  192. try {
  193. connection.close();
  194. } catch (SQLException e) {
  195. e.printStackTrace();
  196. }
  197. }
  198. }
  199. return list;
  200. }
  201. private static void addTable(XWPFDocument document,String tableName,String remark, List<String[]> columns){
  202. //两个表格之间加个换行
  203. document.createParagraph().createRun().setText("\r");
  204. // 标题11级大纲
  205. document.createParagraph().createRun().setText(remark+" "+tableName);
  206. //工作经历表格
  207. XWPFTable ComTable = document.createTable();
  208. // //列宽自动分割
  209. // CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
  210. // comTableWidth.setType(STTblWidth.DXA);
  211. // comTableWidth.setW(BigInteger.valueOf(9072));
  212. CTTbl ttbl = ComTable.getCTTbl();
  213. int[] COLUMN_WIDTHS = new int[] {572,2072,1372,872,672,672,2572};
  214. CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
  215. : ttbl.addNewTblGrid();
  216. for (int j = 0, len = COLUMN_WIDTHS.length; j < len; j++) {
  217. CTTblGridCol gridCol = tblGrid.addNewGridCol();
  218. gridCol.setW(new BigInteger(String.valueOf(COLUMN_WIDTHS[j])));
  219. }
  220. //表格第一行
  221. XWPFTableRow comTableRowOne = ComTable.getRow(0);
  222. setCellvalue(comTableRowOne.getCell(0), "序号");
  223. setCellvalue(comTableRowOne.addNewTableCell(),"字段名");
  224. setCellvalue(comTableRowOne.addNewTableCell(),"类型");
  225. setCellvalue(comTableRowOne.addNewTableCell(),"默认值");
  226. setCellvalue(comTableRowOne.addNewTableCell(),"是否可为空");
  227. setCellvalue(comTableRowOne.addNewTableCell(),"是否主键");
  228. setCellvalue(comTableRowOne.addNewTableCell(),"注释");
  229. for (String[] str : columns) {
  230. //表格第二行
  231. XWPFTableRow comTableRowTwo = ComTable.createRow();
  232. for (int j = 0; j < str.length; j++) {
  233. if(j==0 || j==3 || j==4 || j==5){
  234. setCellvalue(comTableRowTwo.getCell(j),str[j]);
  235. }else{
  236. comTableRowTwo.getCell(j).setText(str[j]);
  237. }
  238. }
  239. }
  240. }
  241. private static void setCellvalue(XWPFTableCell cell, String text){
  242. cell.setText(text);
  243. //垂直居中
  244. cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
  245. CTTc cttc = cell.getCTTc();
  246. CTP ctp = cttc.getPList().get(0);
  247. CTPPr ctppr = ctp.getPPr();
  248. if (ctppr == null) {
  249. ctppr = ctp.addNewPPr();
  250. }
  251. CTJc ctjc = ctppr.getJc();
  252. if (ctjc == null) {
  253. ctjc = ctppr.addNewJc();
  254. }
  255. //水平居中
  256. ctjc.setVal(STJc.CENTER);
  257. }
  258. }

参考:poi生成Word数据库设计详细说明书_路途IT的博客-CSDN博客 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/88103
推荐阅读
相关标签
  

闽ICP备14008679号