现阶段因为项目验收需要提供数据库设计文档,在网络搜所后发现总有各种各样的问题导致导出效果不理想,所以基于lowagie包下的功能实现导出需求。

  1. Maven添加依赖

            <dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version></dependency><dependency><groupId>com.lowagie.text</groupId><artifactId>com.springsource.com.lowagie.text</artifactId><version>2.0.8</version></dependency>

    2. 代码实现

    public class GenTableDocUtils {/***键类型字典*/private static Map<String, String> keyType = new HashMap<String, String>();//初始化jdbcstatic {try {keyType.put("P", "主键");Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}}//urlprivate static String url = "数据库连接地址";//用户名private static String username = "用户名称";//密码private static String password = "密码";//查询所有表的sql语句private static String sql_get_all_tables = "SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'smartstation' ORDER BY TABLE_NAME;";//查询所有字段的sql语句private static String sql_get_all_columns = "SELECT b.COLUMN_NAME,MAX(b.column_type) ,MAX(b.IS_NULLABLE),MAX(b.column_comment),MAX(b.ORDINAL_POSITION) ORDINAL_POSITION FROM information_schema.COLUMNS b WHERE b.TABLE_NAME = '{table_name}'    GROUP BY COLUMN_NAME ORDER BY ORDINAL_POSITION asc";public static void main(String[] args) throws Exception {//初始化word文档Document document = new Document(PageSize.A4);RtfWriter2.getInstance(document, new FileOutputStream("E:/word.doc"));document.open();//查询开始Connection conn = getConnection();//获取所有表List tables = getDataBySQL(sql_get_all_tables, conn);int i = 1;for (Iterator iterator = tables.iterator(); iterator.hasNext(); ) {String[] arr = (String[]) iterator.next();//循环获取字段信息String tableName = arr[0];System.out.print(i + ".正在处理数据表-----------" + arr[0]);addTableMetaData(document, arr, i);List columns = getDataBySQL(sql_get_all_columns.replace("{table_name}", arr[0]), conn);addTableDetail(document, columns);addBlank(document);System.out.println("...done");i++;}document.close();conn.close();}/*** 添加一个空行** @param document* @throws Exception*/public static void addBlank(Document document) throws Exception {Paragraph ph = new Paragraph("");ph.setAlignment(Paragraph.ALIGN_LEFT);document.add(ph);}/*** 添加包含字段详细信息的表格** @param document* @param columns* @throws Exception*/public static void addTableDetail(Document document, List columns) throws Exception {Table table = new Table(5);table.setWidth(100f);table.setBorderWidth(1);table.setBorderColor(Color.BLACK);table.setPadding(0);table.setSpacing(0);Cell cell1 = new Cell("序号");cell1.setHeader(true);Cell cell2 = new Cell("列名");cell2.setHeader(true);Cell cell3 = new Cell("类型");cell3.setHeader(true);Cell cell6 = new Cell("非空");Cell cell7 = new Cell("说明");cell6.setHeader(true);cell7.setHeader(true);//设置表头格式table.setWidths(new float[]{8f, 30f, 15f, 9f, 20f});cell1.setHorizontalAlignment(Cell.ALIGN_CENTER);cell1.setBackgroundColor(Color.gray);cell2.setHorizontalAlignment(Cell.ALIGN_CENTER);cell2.setBackgroundColor(Color.gray);cell3.setHorizontalAlignment(Cell.ALIGN_CENTER);cell3.setBackgroundColor(Color.gray);cell6.setHorizontalAlignment(Cell.ALIGN_CENTER);cell6.setBackgroundColor(Color.gray);cell7.setHorizontalAlignment(Cell.ALIGN_CENTER);cell7.setBackgroundColor(Color.gray);table.addCell(cell1);table.addCell(cell2);table.addCell(cell3);table.addCell(cell6);table.addCell(cell7);table.endHeaders();// 表头结束int x = 1;for (Iterator iterator = columns.iterator(); iterator.hasNext(); ) {String[] arr2 = (String[]) iterator.next();Cell c1 = new Cell(x + "");Cell c2 = new Cell(arr2[0]);Cell c3 = new Cell(arr2[1]);Cell c6 = new Cell("YES".equals(arr2[2]) ? "否" : "是");Cell c7 = new Cell(arr2[3]);c1.setHorizontalAlignment(Cell.ALIGN_CENTER);c2.setHorizontalAlignment(Cell.ALIGN_CENTER);c3.setHorizontalAlignment(Cell.ALIGN_CENTER);c6.setHorizontalAlignment(Cell.ALIGN_CENTER);c7.setHorizontalAlignment(Cell.ALIGN_CENTER);table.addCell(c1);table.addCell(c2);table.addCell(c3);table.addCell(c6);table.addCell(c7);x++;}document.add(table);}/*** 增加表概要信息** @param dcument* @param arr* @param i* @throws Exception*/public static void addTableMetaData(Document dcument, String[] arr, int i) throws Exception {Paragraph ph = new Paragraph(i + ". 表名: " + arr[0] + "        说明: " + (arr[1] == null ? "" : arr[1]));ph.setAlignment(Paragraph.ALIGN_LEFT);dcument.add(ph);}/*** 把SQL语句查询出列表** @param sql* @param conn* @return*/public static List getDataBySQL(String sql, Connection conn) {Statement stmt = null;ResultSet rs = null;List list = new ArrayList();try {stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next()) {String[] arr = new String[rs.getMetaData().getColumnCount()];for (int i = 0; i < arr.length; i++) {arr[i] = rs.getString(i + 1);}list.add(arr);}} catch (SQLException e) {e.printStackTrace();} finally {try {if (rs != null) {rs.close();}if (stmt != null) {stmt.close();}} catch (SQLException e) {e.printStackTrace();}}return list;}/*** 获取数据库连接** @return*/public static Connection getConnection() {try {return DriverManager.getConnection(url, username, password);} catch (SQLException e) {e.printStackTrace();}return null;}
    }

Mysql导出数据库设计文档相关推荐

  1. PowerDesigner导出数据库设计文档

    pd这款软件我之前用来设计数据库的,最近接到一个编写数据库设计文档的任务,222张表,一个个地写显然是不行的.去年刚开始用的时候有个要求就是导出数据库成word,当时还不会,按照网上的教程去操作也没弄 ...

  2. 使用Navicat将表设计导出数据库设计文档

    我们在写数据库设计文档的时候,会需要对数据库表进行设计的编写,手动写的话会很费时间费精力,尤其是如果有大量的表需要写的时候,就更加浪费时间了.下面就让我给大家讲一个简单方法. 我的是在Navicat中 ...

  3. 【MySQL】数据库设计文档生成

    MySQL数据库设计文档生成 文章目录 MySQL数据库设计文档生成 前言 0.全局浏览 1.引入依赖 2.启动类 3.配置文件 4.实现类 5.成品展示 前言 记录MySQL数据库设计文档生成,旨在 ...

  4. 将表结构设计导出数据库设计文档

    我们在写数据库设计文档的时候,会需要对数据库表进行设计的编写,手动写的话会很费时间费精力,尤其是如果有大量的表需要写的时候,就更加浪费时间了. 下面就让我给大家讲一个简单方法.我的是在Navicat中 ...

  5. mysql导出设计文档_Mysql导出数据库设计文档

    在word中,利用ODBC驱动源,OFFICE宏来控制报表输出 作者博客地址: 下载工具 工具下载: 链接: 提取码:mhtd 根据Mysql数据库的版本下载想用的驱动源 也可去官网下载: 双击安装O ...

  6. 根据mysql生成数据库设计文档_通过navicat工具导出数据库的word格式的设计文档...

    1.打开数据库 1.1.打开数据库,执行如下查询语句,查询单个表的语句 SELECT COLUMN_NAME 代码, COLUMN_COMMENT 名称, COLUMN_TYPE 数据类型, 'fal ...

  7. Navicat导出数据库设计文档

    1.在Navicat中点击查询,然后编写代码如下(仅能单表导出) SELECT TABLE_NAME 表名, COLUMN_NAME 列名, COLUMN_TYPE 数据类型, COLUMN_KEY ...

  8. 根据mysql生成数据库设计文档,第100篇博文纪念 | C# 根据数据库表结构生成DOC数据库文档(1)...

    //==============================================================================作 者:农民伯伯//邮 箱:over14 ...

  9. mysql自动生成数据库设计文档

    mysql生成数据库设计文档 引入jar包 <!-- screw核心 --> <dependency><groupId>cn.smallbun.screw</ ...

最新文章

  1. NLPIR-KGB知识图谱引擎突破传统数据挖掘束缚
  2. ADS1.2安装教程
  3. 硬件语言编写规范与技巧
  4. python发挥程度_你为什么用 Python?
  5. STL容器汇总(二)
  6. android 购物车抛物线,添加到购物车抛物线动画
  7. C++ 对引用的理解4
  8. 使用 Pandas 分析 Apache 日志
  9. chrome浏览器session问题_Chrome浏览器的音频自动播放问题
  10. matlab编写文件格式,MATLAB程序设计教程(4)——MATLAB文件操作
  11. css多行文本溢出显示省略号(…)
  12. c语言快递信息系统有哪些信息,国内知名物流信息管理系统软件有哪些?分别是什么?...
  13. C++中的DLL调用0x00000000错误
  14. 习惯养成app_如何培养优秀的开发人员沟通技巧,养成不良习惯
  15. 【数据科学家】如何成为一名数据科学家?
  16. canvas绘制表盘时钟
  17. 二烷基二硫代磷酸锌添加剂的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  18. 【Python百日进阶-Web开发-Feffery】Day432 - fac实例:使用国内cdn加载静态资源
  19. 微信小程序点击获取昵称头像
  20. 如何成为一个优秀的数据分析师?

热门文章

  1. AS3 加解密的隐藏常用方式分析
  2. 【面试高频题】CMS垃圾收集器是如何工作的?
  3. 【CheatEngine】关于BCR的内存分析
  4. ROS2机器人笔记20-12-04
  5. 大数据培训有前途吗,大数据工资一般多少?
  6. Android 各种自定义进度条Progressbar
  7. 带你读AI论文丨SP21 Survivalism: Living-Off-The-Land 经典离地攻击
  8. artifactory-pro7 部署以及ladp、nginx配置
  9. 20201214c列出最简真分数序列
  10. SSH+ExtJs4开发项目的Demo实例视频讲解