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();

}

}

}

猜你喜欢

转载自blog.csdn.net/lq18050010830/article/details/78851180