mysql数据库自动生成数据库开发设计文档


1、输出表结果,表结构可自己通过代码调整,主要思路:

a 在java代码中,通过数据库查询语句获取所有表名和表名备注信息。

b 通过表名获取某张表的所有字段说明。

c 整理查询出来的结果,写入到word文档中。

输出表结果

2、主要数据库查询语句说明

a、查询数据库所有表名和表名说明,查询语句如下

" select table_name,table_comment from information_schema.tables where table_schema = 'xmsa_trace'  "

所有表名

b、查询数据库某张表的所有字段说明,查询语句如下

" SHOW FULL FIELDS FROM xmsa_trace.area_classify "

表字段说明

3、java代码中,通过sql语句查询,查询上述两个结果,不同的框架查询方法不同,以下的是springmvc+mybatis框架的代码,详细代码见文章结尾。

查询数据表名和备注

4、将查询出来的结果整理,写入word,并生成表格。

写入word
写入word
写入word
写入word

5、写入word表格的详细代码,需导入itext-2.1.7.jar  itext-asian-5.2.0.jar  itext-rtf-2.1.7.jar 三个架包

package com.xmbestone.tlb.manage.util;

import java.awt.Color;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;import java.io.IOException;import java.util.List;

import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.lowagie.text.Cell;import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Font;import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Table;

import com.lowagie.text.rtf.RtfWriter2;

import com.xmbestone.tlb.manage.service.business.IBusinessSupplierService;

/** * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据 5,关闭文档 */

@Service("dateToWordUtil")public class DateToWordUtil {@Autowiredprivate IBusinessSupplierService businessSupplierService;

/** * @param args * @throws Exception */public void toWord(List> listAll) throws Exception {// 创建word文档,并设置纸张的大小Document document = new Document(PageSize.A4);

try {// 创建word文档RtfWriter2.getInstance(document, new FileOutputStream("E:/word5.doc"));

document.open();// 设置文档标题

Paragraph ph = new Paragraph();

Font f = new Font();

Paragraph p = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24,Font.BOLDITALIC, new Color(0, 0, 0)));

p.setAlignment(1);

document.add(p);

ph.setFont(f);/* * 创建表格 通过查询出来的表遍历 */

for (int i = 0; i < listAll.size(); i++) {

// 表名

String table_name = (String) listAll.get(i).get("table_name");

// 表说明

String table_comment = (String) listAll.get(i).get("table_comment");

String sql = "SHOW FULL FIELDS FROM xmsa_trace." + table_name+ " ";

//获取某张表的所有字段说明

List> list = businessSupplierService.listMap(sql);

//构建表说明

String all = "" + (i + 1) + " 表名:" + table_name + " "+ table_comment + "";

//创建有6列的表格

Table table = new Table(6);

document.add(new Paragraph(""));

table.setBorderWidth(1);

// table.setBorderColor(Color.BLACK);

table.setPadding(0);

table.setSpacing(0);

/*

* 添加表头的元素,并设置表头背景的颜色

*/

Color chade = new Color(176, 196, 222);

Cell cell = new Cell("序号");// 单元格

cell.setBackgroundColor(chade);

cell.setHeader(true);

// cell.setColspan(3);//设置表格为三列

// cell.setRowspan(3);//设置表格为三行

table.addCell(cell);

cell = new Cell("字段名");// 单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("类型");// 单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("是否为空");// 单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("主键");// 单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("字段说明");// 单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

table.endHeaders();// 表头结束

// 表格的主体,

for (int k = 0; k < list.size(); k++) {

//获取某表每个字段的详细说明

String Field = (String) list.get(k).get("Field");

String Type = (String) list.get(k).get("Type");

String Null = (String) list.get(k).get("Null");

String Key = (String) list.get(k).get("Key");

String Comment = (String) list.get(k).get("Comment");

table.addCell((k + 1) + "");

table.addCell(Field);

table.addCell(Type);

table.addCell(Null);

table.addCell(Key);

table.addCell(Comment);

}

Paragraph pheae = new Paragraph(all);

//写入表说明

document.add(pheae);

//生成表格

document.add(table);

}

document.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

6、java代码,将数据写入到word文档中并生成表格的样例代码。

package com.xmbestone.tlb.manage.util;

import java.awt.Color;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Font;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.rtf.RtfWriter2;

/**

* 创建word文档 步骤:

* 1,建立文档

* 2,创建一个书写器

* 3,打开文档

* 4,向文档中写入数据

* 5,关闭文档

*/

public class WordDemo {

public WordDemo() {

}

/**

* @param args

*/

public static void main(String[] args) {

// 创建word文档,并设置纸张的大小

Document document = new Document(PageSize.A4);

try {

RtfWriter2.getInstance(document,

new FileOutputStream("E:/word5.doc"));

document.open();

//设置合同头

Paragraph ph = new Paragraph();

Font f  = new Font();

Paragraph p = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)) );

p.setAlignment(1);

document.add(p);

ph.setFont(f);

// 设置中文字体

// BaseFont bfFont =    BaseFont.createFont("STSongStd-Light",  "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

// Font chinaFont = new Font();

/*

* 创建有三列的表格

*/

for(int i=0;i<5;i++){

Table table = new Table(6);

document.add(new Paragraph(""));

table.setBorderWidth(1);

// table.setBorderColor(Color.BLACK);

table.setPadding(0);

table.setSpacing(0);

/*

* 添加表头的元素

*/

Color chade = new Color(176, 196, 222);

Cell cell = new Cell("序号");//单元格

cell.setBackgroundColor(chade);

cell.setHeader(true);

//        cell.setColspan(1);//设置表格为三列

//        cell.setRowspan(1);//设置表格为三行

table.addCell(cell);

cell = new Cell("字段名");//单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("类型");//单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("是否为空");//单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("主键");//单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

cell = new Cell("字段说明");//单元格

cell.setBackgroundColor(chade);

table.addCell(cell);

table.endHeaders();// 表头结束

// 表格的主体

table.addCell("1,1");

table.addCell("1,2");

table.addCell("1,3");

table.addCell("1,4");

table.addCell("1,5");

table.addCell("1,6");

table.addCell("你好啊");

table.addCell("你好啊");

table.addCell("你好啊");

table.addCell("你好啊");

table.addCell("你好啊");

table.addCell("你好啊");

document.add(new Paragraph("表一"));

document.add(table);

}

document.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容