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

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章