freemaker模板引擎不仅可以作为网页展现数据的工具还可以作为输出指定文件的工具使用。
现在我们使用freemaker模板引擎提供的template做一个自己的代码生成器;
主要功能:根据数据库表结构生成前端新增、编辑、详情、列表页面。
- pom.xml中引入freemaker模板引擎
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.通过jdbc查询数据库表信息
public class FieldDetail {
//字段名称 对应input中的name
private String name;
//字段类型 对应input中的type
private String type;
//字段描述 对应字段描述
private String describe;
此处省略get、set
}
/**
* 获取数据库表字段详细详细 字段名称,字段类型,注解
*/
public class TableField {
//获取数据库表对应的字段详细详细
public List<FieldDetail> getField(String tableName){
String drive="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=UTF-8";
String user="root";
String password="123456";
Connection connection=null;
ResultSet resultSet=null;
List<FieldDetail> fields=new ArrayList<>();
try {
Class.forName(drive);
try {
connection= DriverManager.getConnection(url,user,password);
DatabaseMetaData dmd=connection.getMetaData();
resultSet=dmd.getTables(null,"%",tableName,new String[]{"TABLE"});
while (resultSet.next()){
String sTableName=resultSet.getString("TABLE_NAME");
System.out.println("所属表:"+sTableName);
if(sTableName.equalsIgnoreCase(tableName)){
ResultSet rs=connection.getMetaData().getColumns(null,"%",sTableName,"%");
while (rs.next()){
FieldDetail field=new FieldDetail();
field.setName(rs.getString("COLUMN_NAME"));//字段名称
field.setType(type2Type(rs.getString("TYPE_NAME")));//字段属性
field.setDescribe(rs.getString("REMARKS"));//字段注释
fields.add(field);
}
rs.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(resultSet!=null){
resultSet.close();
}
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return fields;
}
private String type2Type(String type){
String rtype="";
switch (type){
case "INT":
rtype="int";
break;
case "DATETIME":
rtype="date";
break;
case "DOUBLE":
rtype="double";
break;
default:
rtype="string";
}
return rtype;
}
3.使用freemaker输出指定文件
生成前端页面
/**
* 自动生成 新增(add.ftl),编辑(edit.ftl),查看(detail.ftl),主页(index.ftl)
*/
public class TemplateGeneration {
private static String tepath="D:/MyWork/videoapp_web/src/main/resources/templates/view/ftl/html";
private static Configuration cfg=null;
public static Configuration getConfiguration() throws IOException {
if(cfg==null){
cfg=new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new File(tepath+"/template"));
cfg.setEncoding(Locale.getDefault(), "UTF-8");
// 设置对象的包装器
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
return cfg;
}
return cfg;
}
public static void main(String[] args) throws IOException, TemplateException {
//输出地址
String modal="admin";//模块
String object="role";//对象
String objectName="角色";//对象名称
String temps[]={"detail","add","edit","index"};
//获取到对象的字段详细
TableField table=new TableField();
List<FieldDetail> fields=table.getField(object);
//返回给文件上的参数
Map param=new HashMap<>();
param.put("title",objectName);
param.put("object",object);
param.put("modal",modal);
param.put("fields",fields);
Template template=null;
File file=null;
FileWriter writer=null;
for (String tel:temps
) {
// 创建Template对象
Configuration cfg = getConfiguration();
//模板所在位置
template=cfg.getTemplate(tel+".ftl");
template.setEncoding("UTF-8");
//模板输出位置
file=new File(tepath+"/newtemplate/"+object+"/"+tel+".ftl");
if(!file.exists()){
boolean k=file.getParentFile().mkdir();
if(k){
file.createNewFile();
}
}
writer=new FileWriter(file);
template.process(param,writer);
}
System.out.println("模板输出地址:"+tepath+"/newtemplate/"+object);
System.out.println("生成模板成功");
writer.close();
}
}