自動(dòng)生成數(shù)據(jù)庫(kù)表設(shè)計(jì)(二)之Freemarker的基本使用

最近項(xiàng)目快了驗(yàn)收,那么接下來(lái)就是寫一些比較煩人的文檔,在寫數(shù)據(jù)庫(kù)設(shè)計(jì)文檔時(shí),到了詳細(xì)設(shè)計(jì)這一塊有點(diǎn)尷尬了,每張表,沒個(gè)字段都要寫上去,由于前期沒有整理,所以這個(gè)工作量還是很大,我查看了我們的數(shù)據(jù)庫(kù)發(fā)現(xiàn)有353張表,這樣寫,得花多久的時(shí)間啊。。。于是想通過程序來(lái)自動(dòng)完成,這就是這篇文章的核心。

系列文章:
自動(dòng)生成數(shù)據(jù)庫(kù)表設(shè)計(jì)(一)之獲取JDBC獲取元數(shù)據(jù)
自動(dòng)生成數(shù)據(jù)庫(kù)表設(shè)計(jì)(二)之Freemarker的基本使用
自動(dòng)生成數(shù)據(jù)庫(kù)表設(shè)計(jì)(三)之制作word模版

本篇主要內(nèi)容:
1、Freemarker的使用
2、Freemarker的List的使用
3、Freemarker的List嵌套List的使用

Freemarker簡(jiǎn)單使用

1、引入freemarker依賴

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

2、引入Freemarker的工具類

public class FtUtil {
    /**
     * 獲取模板
     *
     * @param templatesDir 例如"/templates"
     * @return
     */
    public Template getTemplate(String templatesDir, String name) {
        try {
            //通過Freemaker的Configuration讀取相應(yīng)的ftl
            Configuration cfg = new Configuration();
            //設(shè)定去哪里讀取相應(yīng)的ftl模板文件
            cfg.setClassForTemplateLoading(this.getClass(), templatesDir);
            //在模板文件目錄中找到名稱為name的文件
            Template temp = cfg.getTemplate(name);
            return temp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Description: 根據(jù)模版生成文件 <br/>
     */
    public void generateFile(String templatesDir, String templateName, Map root, String outDir, String outFileName) {
        FileWriter out = null;
        try {
            //通過一個(gè)文件輸出流,就可以寫到相應(yīng)的文件中
            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();
            }
        }
    }
}

3、在resources下新建test.ftl文件

<#-- 這是Freemarker注釋 -->
<#-- 獲取簡(jiǎn)單值 -->
${table}

這里就是簡(jiǎn)單的取變量為table的值,下面我們就是用這個(gè)模版文件生成文件

4、測(cè)試 生成文件

public class TestFreemaker {
    public static void main(String[] args) throws Exception {
        Map map = new HashMap<>();
        map.put("table", "123");

        FtUtil ftUtil = new FtUtil();
        ftUtil.generateFile("/", "test.ftl", map, "D:/", "test.txt");
    }
}

這里給變量table設(shè)置了值123,生成文件將會(huì)顯示123,就說(shuō)明我們已經(jīng)會(huì)使用Freemarker了,運(yùn)行結(jié)果如下:

test.txt

現(xiàn)在我們已經(jīng)可以取單個(gè)變量了,下面我們看在模版里取集合的應(yīng)用

Freemarker中List的使用

1、遍歷List的用法如下:

<#-- List使用 -->
<#list table as t>
序號(hào):${t_index}   表名:${t.NAME}
</#list>

其中table表示List,而t是List里的元素, t_index是表示t這個(gè)元素在list中的下標(biāo),t.NAME是表示取名為NAME的屬性值

2、修改Main方法

    public static void main(String[] args) throws Exception {

        //模擬一張表
        Map<String, Object> table = new HashMap<>();
        table.put("NAME", "T_USER");

        //模擬一個(gè)表集合
        List<Map<String, Object>> tableList = new ArrayList<>();
        tableList.add(table);

        Map map = new HashMap<>();
        //map.put("table", "123");
        map.put("table", tableList);


        FtUtil ftUtil = new FtUtil();
        ftUtil.generateFile("/", "test.ftl", map, "D:/", "test.txt");
    }

結(jié)果如下:

test.txt

Freemarker中List嵌套List的使用

1、修改test.ftl如下:

<#-- List嵌套List的使用 -->
<#list table as t>
序號(hào):${t_index}   表名:${t.NAME}
    <#list t.COLUMNS as c>
    序號(hào):${c_index}   列名:${c.NAME}
    </#list>
</#list>

其中t對(duì)象里多了一個(gè)屬性COLUMNS,這個(gè)COLUMNS也是一個(gè)集合

2、修改Main方法

public static void main(String[] args) throws Exception {

        //模擬2個(gè)列ID、AGE
        Map<String, Object> column1 = new HashMap<>();
        column1.put("NAME", "ID");
        Map<String, Object> column2 = new HashMap<>();
        column2.put("NAME", "AGE");

        //模擬一個(gè)列集合
        List<Map<String, Object>> columnList = new ArrayList<>();
        columnList.add(column1);
        columnList.add(column2);

        //模擬一張表
        Map<String, Object> table = new HashMap<>();
        table.put("NAME", "T_USER");
        table.put("COLUMNS", columnList);

        //模擬一個(gè)表集合
        List<Map<String, Object>> tableList = new ArrayList<>();
        tableList.add(table);

        Map map = new HashMap<>();
        //map.put("table", "123");
        map.put("table", tableList);


        FtUtil ftUtil = new FtUtil();
        ftUtil.generateFile("/", "test.ftl", map, "D:/", "test.txt");
    }

運(yùn)行結(jié)果如下:

test.ftl

有了如上知識(shí),加上前面我們準(zhǔn)備的表和列的數(shù)據(jù),就可以根據(jù)任意模版生成對(duì)應(yīng)的表數(shù)據(jù)和列數(shù)據(jù)了。下一篇我將介紹如何創(chuàng)建word模版。

文檔下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容