您的位置:首页 > 数据库

自动生成数据库表设计(二)之Freemarker的基本使用

2017-02-21 15:06 567 查看
最近项目快了验收,那么接下来就是写一些比较烦人的文档,在写数据库设计文档时,到了详细设计这一块有点尴尬了,每张表,没个字段都要写上去,由于前期没有整理,所以这个工作量还是很大,我查看了我们的数据库发现有353张表,这样写,得花多久的时间啊。。。于是想通过程序来自动完成,这就是这篇文章的核心。

系列文章:

自动生成数据库表设计(一)之获取JDBC获取元数据

自动生成数据库表设计(二)之Freemarker的基本使用

自动生成数据库表设计(三)之制作word模版

本篇主要内容:

1、Freemarker的使用

2、Freemarker的List的使用

3、Freemarker的List嵌套List的使用

Freemarker简单使用

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读取相应的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: 根据模版生成文件 <br/>
*/
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();
}
}
}
}


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

<#-- 这是Freemarker注释 -->
<#-- 获取简单值 -->
${table}


这里就是简单的取变量为table的值,下面我们就是用这个模版文件生成文件

4、测试 生成文件

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设置了值123,生成文件将会显示123,就说明我们已经会使用Freemarker了,运行结果如下:



现在我们已经可以取单个变量了,下面我们看在模版里取集合的应用

Freemarker中List的使用

1、遍历List的用法如下:

<#-- List使用 -->
<#list table as t>
序号:${t_index}   表名:${t.NAME}
</#list>


其中table表示List,而t是List里的元素, t_index是表示t这个元素在list中的下标,t.NAME是表示取名为NAME的属性值

2、修改Main方法

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

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

//模拟一个表集合
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");
}


结果如下:



Freemarker中List嵌套List的使用

1、修改test.ftl如下:

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


其中t对象里多了一个属性COLUMNS,这个COLUMNS也是一个集合

2、修改Main方法

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

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

//模拟一个列集合
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);

//模拟一个表集合
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");
}


运行结果如下:



有了如上知识,加上前面我们准备的表和列的数据,就可以根据任意模版生成对应的表数据和列数据了。下一篇我将介绍如何创建word模版。

文档下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 freemarker
相关文章推荐