这个星期做数据字典功能,有一项任务就是将数据库中的每个表的字段导出,生成word文档,在综合比较网上各种技术之后,参照csdn上骆豪的博客完成了任务。
骆昊的链接:http://blog.csdn.net/jackfrued/article/details/39449021
首先打开word文档,建立自己所需要的模板,然后将word保存为XML的格式,这里可能出现的一个问题就是需要填入的内容放上${}占位符的时候可能会出现字符分离的情况,所以建议先将需要用${}占位符的地方用中文写在word里然后保存为XML的格式,再打开XML对需要用${}占位符的地方进行替换,这样就避免了字符分离的情况。推荐使用一个软件,foxe,这个软件编辑XML很方便,如下图所示,按F8可以对XML进行格式化,然后再对XML进行编辑。
将需要用${}占位符输出的字段替换好之后,将文件另存为ftl格式即可。
将freemarker的jar包导入lib之后,开始编写代码。
因为项目需要,我是需要将数据库中的表的和表的字段都读取出来,所以可以使用两层list进行封装。
首先,使用一个Java bean封装,代码如图所示
import java.util.List;
public class allTables {
String title;
String commont;
int count;
List<CommonDictionary> commonDictionaries;
public String getCommont() {
return commont;
}
public void setCommont(String commont) {
this.commont = commont;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<CommonDictionary> getCommonDictionaries() {
return commonDictionaries;
}
public void setCommonDictionaries(List<CommonDictionary> commonDictionaries) {
this.commonDictionaries = commonDictionaries;
}
}
这个Javabean是对表进行进行封装,然后再使用一个Javabean对表的属性进行封装,如图所示:
package cn.changhong.dictionary.domain;
import java.util.Date;
public class CommonDictionary {
String cId;
String cName;
String cCommont;
String cDataType;
String primaryKey;
String createUser;
Date createTime;
String updateUser;
Date updateTime;
String cLength;
int num;
String isNull;
public String getIsNull() {
return isNull;
}
public void setIsNull(String isNull) {
this.isNull = isNull;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getcId() {
return cId;
}
public void setcId(String cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getcCommont() {
return cCommont;
}
public void setcCommont(String cCommont) {
this.cCommont = cCommont;
}
public String getcDataType() {
return cDataType;
}
public void setcDataType(String cDataType) {
this.cDataType = cDataType;
}
public String getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
public String getcLength() {
return cLength;
}
public void setcLength(String cLength) {
this.cLength = cLength;
}
}
项目的结构如下图: