1.概述
可在线自动生成代码,省去复制、修改通用模板代码的繁琐过程,减少团队70%以上的开发工作量
基于java的template模板引擎velocity,在定义好模板文件后,动态产生适应业务的java、xml、html、sql等代码文件
2.自动生成过程
首先引入velocity的pom依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<artifactId>velocity</artifactId>
<groupId>org.apache.velocity</groupId>
<version>1.7</version>
</dependency>
其中commons-io为io流的相关框架。
在Resources目录下先定义好模板文件template/User.java.vm
package com.yrr.mvntest;
import java.util.Date;
/**
* ${comments}
*/
@Data
public class ${className} {
#foreach ($column in $columns)
/**
* $column.comments
*/
#if($column.columnName == $pk.columnName)
@TableId
#end
private $column.attrType $column.attrName;
#end
}
通过数据库查出表和列的表备注、列备注、表名、字段名、字段类型等,将其填充入模板,即可生成所需要的代码文件(此处省略查询数据库的过程):
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
@WebServlet("/MvnServlet")
public class MvnServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
//封装模板数据
Map<String, Object> map = new HashMap<>();
map.put("comments", "表注释");
map.put("className", "User");
map.put("priName", "id");
MvnColumn c1 = new MvnColumn("id","bit","列备注1","id","Integer");
MvnColumn c2 = new MvnColumn("name","varchar","列备注2","name","String");
MvnColumn c3 = new MvnColumn("mobile","varchar","列备注3","mobile","String");
List<MvnColumn> mvnColumns = new ArrayList<>();
mvnColumns.add(c1);
mvnColumns.add(c2);
mvnColumns.add(c3);
map.put("columns", mvnColumns);
VelocityContext context = new VelocityContext(map);
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate("template/User.java.vm", "UTF-8");
tpl.merge(context, sw);//将数据context合并入模板tpl中,获得的数据写入sw
OutputStream ouputStream = response.getOutputStream();
IOUtils.write(sw.toString(), ouputStream, "UTF-8");
IOUtils.closeQuietly(sw);
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-disposition", "attachment;filename=User.java");
}
}
public class MvnColumn {
private String columnName;
private String dataType;
private String comments;
private String attrName;
private String attrType;
public MvnColumn(String columnName, String dataType, String comments, String attrName, String attrType){
this.columnName = columnName;
this.dataType = dataType;
this.comments = comments;
this.attrName = attrName;
this.attrType = attrType;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getAttrName() {
return attrName;
}
public void setAttrName(String attrName) {
this.attrName = attrName;
}
public String getAttrType() {
return attrType;
}
public void setAttrType(String attrType) {
this.attrType = attrType;
}
}
在web客户端即可下载获得User.java代码文件,文件内容如下:
一般情况下,会生成多个代码文件,打包成压缩文件的形式下载:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
@WebServlet("/MvnServlet")
public class MvnServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] data = this.generatorCode();
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"mvntest.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
public byte[] generatorCode(){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
//获取模板列表
String[] templates = {"template/User.java.vm"};
for (String template : templates) {
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
//封装模板数据
Map<String, Object> map = new HashMap<>();
map.put("comments", "表注释");
map.put("className", "User");
map.put("priName", "id");
MvnColumn c1 = new MvnColumn("id","bit","列备注1","id","Integer");
MvnColumn c2 = new MvnColumn("name","varchar","列备注2","name","String");
MvnColumn c3 = new MvnColumn("mobile","varchar","列备注3","mobile","String");
List<MvnColumn> mvnColumns = new ArrayList<>();
mvnColumns.add(c1);
mvnColumns.add(c2);
mvnColumns.add(c3);
map.put("columns", mvnColumns);
VelocityContext context = new VelocityContext(map);
//渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);
try {
//添加到zip
zip.putNextEntry(new ZipEntry("main/java/com/yrr/mvntest/User.java"));
IOUtils.write(sw.toString(), zip, "UTF-8");
IOUtils.closeQuietly(sw);
zip.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
IOUtils.closeQuietly(zip);
return outputStream.toByteArray();
}
}
在web客户端访问即可获得如下压缩文件:
开源项目参考:renren-generator: 人人开源项目的代码生成器,可在线生成entity、xml、dao、service、vue、sql代码,减少70%以上的开发任务