生成 数据库设计文档

最近要写一个数据库设计文档

        数据设计文档中数据库结构设计这个模块是需要把数据库的所有字段,标识,注释等的设计写出来,如果手写会疯的,现在自己来写个生成工具生成,只要连接数据库就ok。

一、在用idea创建maven项目,在pom里面添加下面的jar(dbcp可以不要,当前项目用不到):


    
      org.springframework
      spring-jdbc
      4.1.7.RELEASE
    

    
    
      mysql
      mysql-connector-java
      8.0.9-rc
    

    
    
      commons-dbcp
      commons-dbcp
      1.4
    

    
      org.freemarker
      freemarker
      2.3.23
    

二、创建包

生成 数据库设计文档_第1张图片

三、数据库连接工具

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConn {  
    //链接数据库  
    public Connection getCon(){  
        Connection conn = null;  
        try {  
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            String user = "root";
            String pwd ="root";
            conn=DriverManager.getConnection(url, user, pwd);  
              
        } catch (ClassNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return conn;  
    }  
    //关闭数据库  
    public void closeConn(){  
        Connection con = getCon();  
        try {  
            if(!con.isClosed()){  
                con.close();  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
    public static void main(String[] args){  
        DbConn db = new DbConn();
        db.getCon();  
        db.closeConn();  
    }  
  
}  

四、创建实体类

public class TableInfo {

    private String sort;//序号
    private String tableName;//表名
    private String tableComment;//表注释
    private String columnName;//字段名
    private String columnComment;//字段注释
    private String isNullable;//是否可以为空
    private String columnType;//字段类型
    private String columnKey;//主键

    get;set;
}

五、用到的sql语句

可以自己先试试。

    select table_name, TABLE_COMMENT from information_schema.tables where table_schema='test' and table_type='base table';


    select * from information_schema.columns
    where table_schema = 'test'
    and table_name = 'jc_model' ;


    select ORDINAL_POSITION sort, TABLE_NAME, COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, COLUMN_KEY , COLUMN_COMMENT from information_schema.columns
    where table_schema = 'test'
    and table_name = 'jc_model' ;

六、Freemaker工具类

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

public class FtUtil {
    /**
     * 获取模板
     *
     * @param templatesDir 例如"/templates"
     * @return
     */
    public Template getTemplate(String templatesDir, String name) {
        try {
            //通过Freemaker的Configuration读取相应的ftl
            Configuration cfg = new Configuration();
            //设定去哪里读取相应的ftl模板文件
            cfg.setClassForTemplateLoading(this.getClass(), templatesDir);
            //在模板文件目录中找到名称为name的文件
            Template temp = cfg.getTemplate(name);
            return temp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Description: 根据模版生成文件 
*/ public void generateFile(String templatesDir, String templateName, Map root, String outDir, String outFileName) { FileWriter out = null; try { //通过一个文件输出流,就可以写到相应的文件中 out = new FileWriter(new File(outDir, outFileName)); Template temp = this.getTemplate(templatesDir, templateName); temp.process(root, out); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } }

七、把word.doc文件转为word.xml放在resources下面(有时间可以研究一下,会发现word其实是xml文件。)




    
        
            
                
                
                
            
        
    
    
        
            
                
                
                
                
                
                
                
            
        
    
    
        
            
                



                    <#list tableList as l>
                    
                        
                            
                            
                        
                        
                            
                                
                            
                            表:
                        
                        
                        
                            
                                
                            
                            ${l.tableName}
                        
                        
                    
                    
                        
                            
                            
                                
                                
                                
                                
                                
                                
                            
                            
                        
                        
                            
                            
                            
                            
                            
                            
                        
                        
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        表名
                                    
                                
                            
                            
                                
                                    
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${l.tableName}
                                    
                                    
                                
                            
                        
                        
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        表注释
                                    
                                
                            
                            
                                
                                    
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${l.tableComment}
                                    
                                    
                                
                            
                        
                        
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        序号
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        字段名
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        注释
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        字段类型
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        强制
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        主键
                                    
                                
                            
                        

                        <#list l.table as t>
                        
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.sort}
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.columnName}
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.columnComment}
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.columnType}
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.isNullable}
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                    
                                        
                                            
                                        
                                        ${t.columnKey}
                                    
                                    
                                
                            
                        
                        

                    
                    

                    
                        
                            
                                
                            
                        
                        
                        
                    
                    
                        
                        
                        
                        
                    
                
            
        
    
    
        
            
                
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
        
    
    
        
            
                
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
        
    
    
        
            
                
                    
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                    
                    
                        
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                        
                        
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                        
                    
                    
                        
                            
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                
                                
                            
                        
                        
                            
                                
                                    
                                
                                
                                
                            
                            
                                
                                    
                                
                                
                                
                            
                            
                                
                                    
                                
                                
                                
                            
                        
                        
                            
                                
                            
                            
                                
                            
                            
                                
                                    
                                        
                                            
                                        
                                    
                                
                            
                        
                        
                            
                                
                            
                            
                                
                                    
                                    
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                        
                                    
                                
                                
                            
                        
                    
                
                
                
                
                    
                        
                    
                
            
        
    
    
        
            
                
                
                
                
                
                
                
                
                
                
                
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                
                
                    
                    
                        
                    
                
                
                
                
                
                
            
        
    
    
        
            
                
                
            
        
    
    
        
            
                
                
                jcroad
                
                
                jcroad
                7
                2018-05-29T09:01:00Z
                2018-05-29T13:33:00Z
            
        
    
    
        
            
                
                    
                        
                            
                            
                        
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                        
                        
                    
                    
                        
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        
                        
                        
                        
                        
                    
                    
                        
                        
                        
                        
                        
                    
                
                
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                        
                        
                            
                            
                            
                            
                        
                    
                
                
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                        
                            
                        
                        
                            
                            
                        
                        
                        
                    
                    
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                        
                            
                            
                        
                        
                        
                    
                    
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                        
                        
                        
                        
                        
                        
                    
                
                
                    
                    
                    
                    
                    
                    
                        
                        
                        
                        
                    
                    
                        
                            
                            
                            
                            
                            
                            
                        
                    
                
            
        
    
    
        
            
                
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
            
        
    
    
        
            
                
                2
                1
                18
                104
                Microsoft Office Word
                0
                1
                1
                false
                
                false
                121
                false
                false
                16.0000
            
        
    

八、执行的类(执行main方法即可)

import jdbc.DbConn;
import model.TableInfo;
import org.springframework.util.StringUtils;
import utils.FtUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateSqlWord {

    private static String dataBaseName = "cms_wdd";

    public static void main(String[] args) throws Exception {
        List tableList = getAllTableList();
        FtUtil ftUtil = new FtUtil();
        Map map = new HashMap<>();
        map.put("tableList", tableList);
        ftUtil.generateFile("/", "word.xml", map, "D:/", "word.doc");
    }

    public static List getAllTableList(){
        DbConn db = new DbConn();
        Connection con = db.getCon();

        List tables = getAllTable(con);
        List listTabel = new ArrayList<>();

        for (TableInfo t : tables ) {
            Map map=new HashMap();
            map.put("tableName",t.getTableName());
            map.put("tableComment",t.getTableComment());
            map.put("table", getTableList(t.getTableName(), con));
            listTabel.add(map);
        }
        db.closeConn();
        return listTabel;
    }

    private static List getAllTable(Connection con) {
        List list = new ArrayList<>();
        String sql="select table_name, TABLE_COMMENT from information_schema.tables where table_schema='"+dataBaseName+"' and table_type='base table';";
        try
        {
            PreparedStatement pp= con.prepareStatement(sql);
            ResultSet rr= pp.executeQuery();
            while(rr.next())
            {
                TableInfo table = new TableInfo();
                table.setTableName(formattUpperCase(rr.getString(1)));
                table.setTableComment(rr.getString(2));
                System.out.println(table.getTableComment()+"("+table.getTableName()+"):"+formattTableComment(table.getTableComment()));
                list.add(table);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }


    private static List getTableList(String tableName , Connection con) {
        List list = new ArrayList<>();
        String sql="" +
                " select ORDINAL_POSITION sort, COLUMN_NAME columnName, COLUMN_COMMENT columnComment, " +
                " IS_NULLABLE isNullable, COLUMN_TYPE columnType, COLUMN_KEY columnKey from information_schema.columns\n " +
                " where table_schema = '"+dataBaseName+"' \n" +
                " and table_name = '"+tableName+"' ; ";
        try
        {
            PreparedStatement pp= con.prepareStatement(sql);
            ResultSet rr= pp.executeQuery();
            while(rr.next())
            {
                Map map=new HashMap();
                map.put("sort", formattData(rr.getString(1)));
                map.put("columnName", formattUpperCase(rr.getString(2)));
                map.put("columnComment", formattData(rr.getString(3)));
                map.put("isNullable", formattData(rr.getString(4)));
                map.put("columnType", formattData(rr.getString(5)));
                map.put("columnKey", formattData(rr.getString(6)));
                list.add(map);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    private static String formattUpperCase(String column) {
        if(!StringUtils.isEmpty(column)){
            return column.toUpperCase();
        }
        return column;
    }

    private static String formattTableComment(String tableComment) {
        if(tableComment.endsWith("表")){
            return tableComment.substring(0,tableComment.length()-1);
        }
        return tableComment;
    }

    public static String formattData(String val){
        if(val.isEmpty()){
            return "";
        }
        if(val.contains("\"")){
            val.replaceAll("\"", """);
        }
        if(val.contains("'")){
            val.replaceAll("'", "'");
        }
        if(val.contains("&")){
            val.replaceAll("&", "&");
        }
        if(val.contains(">")){
            val.replaceAll(">", ">");
        }
        if(val.contains("<")){
            val.replaceAll("<", "<");
        }
        if(val.equals("PRI")){
            val = "主键(PRI)";
        }
        if(val.equals("MUL")){
            val = "";
        }
        return val;
    }
}
欢迎留言


你可能感兴趣的:(freemaker,文档设计)