自动生成代码-Velocity引擎-vm文件-参考ruoyi 根据数据库表生成实体类
此处 template 为 第三步的模板地址。根据上篇文章,获取数据库表格信息。1、初始化 Velocity引擎。【此处根据每个项目不同决定】5、这里文件输出到zip。2、设置模板变量信息。
·
根据上篇文章,获取数据库表格信息
https://blog.csdn.net/MinisterOL/article/details/137638912
1、初始化 Velocity引擎
import org.apache.velocity.app.Velocity;
import java.util.Properties;
/**
* VelocityEngine工厂
*
* @author ruoyi
*/
public class VelocityInitializer {
/**
* 初始化vm方法
*/
public static void initVelocity() {
Properties p = new Properties();
try {
// 加载classpath目录下的vm文件
p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// 定义字符集
p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
// 初始化Velocity引擎,指定配置Properties
Velocity.init(p);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2、设置模板变量信息
import org.apache.velocity.VelocityContext;
/**
* 设置模板变量信息
*
* @return 模板列表
*/
public static VelocityContext prepareContext(GenTable genTable) {
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("tableName", genTable.getTableName());
velocityContext.put("tableAlias", genTable.getTableAlias());
velocityContext.put("tableComment", genTable.getTableComment());
velocityContext.put("className", genTable.getClassName());
velocityContext.put("classNoteName", genTable.getClassNoteName());
velocityContext.put("packageName", genTable.getPackageName());
velocityContext.put("sysName", genTable.getSysName());
velocityContext.put("moduleName", genTable.getModuleName());
velocityContext.put("functionName", genTable.getFunctionName());
velocityContext.put("functionAuthor", genTable.getFunctionAuthor());
velocityContext.put("importList", getImportList(genTable));
velocityContext.put("table", genTable);
velocityContext.put("controllerMappingPath", genTable.getControllerMappingPath());
velocityContext.put("datetime", new Date());
return velocityContext;
}
3、获取模板列表
【此处根据每个项目不同决定】
import java.util.List;
/**
* 获取模板信息
*
* @return 模板列表
*/
public static List<String> listTemplate() {
List<String> templateList = new ArrayList<>();
templateList.add("vm/java/entity.java.vm");
templateList.add("vm/java/mapper.java.vm");
templateList.add("vm/java/entityPo.java.vm");
templateList.add("vm/java/entityDo.java.vm");
templateList.add("vm/java/entityRes.java.vm");
templateList.add("vm/java/entityReq.java.vm");
templateList.add("vm/java/service.java.vm");
templateList.add("vm/java/serviceImpl.java.vm");
templateList.add("vm/java/bizService.java.vm");
templateList.add("vm/java/bizServiceImpl.java.vm");
templateList.add("vm/java/controller.java.vm");
templateList.add("vm/xml/mapper.xml.vm");
templateList.add("vm/applet/applet-info.vue.vm");
templateList.add("vm/applet/applet-query.vue.vm");
templateList.add("vm/applet/applet-save.vue.vm");
templateList.add("vm/web/web-save.vue.vm");
templateList.add("vm/web/web-query.vue.vm");
templateList.add("vm/web/web-info.vue.vm");
return templateList;
}
vm文件:
package ${packageName}.entity.${moduleName};
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import javax.validation.constraints.NotNull;
import com.internetCafes.spms.common.valid.paramcontroller.ParamValid;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
#foreach($import in $importList)
import $import;
#end
import com.internetCafes.spms.common.base.entity.BaseEntity;
/**
* ${classNoteName} 对象
* <p>
* ${tableComment}
* </p>
*
* @author ${functionAuthor}
* @since ${datetime}
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("${tableName}")
public class ${className} extends BaseEntity{
private static final long serialVersionUID=1L;
#foreach ($column in $table.columnList)
/** $column.javaNoteName $column.columnComment */
#if($column.javaType == 'Date')
@JsonFormat(pattern = "yyyy-MM-dd")
#end
#set($orderNum = $column.sort - 1)
#if($column.listFlag == 1)
#if($column.javaType == 'Date')
@Excel(name = "${column.javaNoteName}", width = 15, needMerge = true, format = "yyyy-MM-dd", orderNum = "$orderNum")
#else
@Excel(name = "${column.javaNoteName}", width = 15, needMerge = true, orderNum = "$orderNum")
#end
#end
private $column.javaType $column.javaField;
#end
@Override
@NotNull(groups = {ParamValid.UpdateGroup.class})
public Long getId(){
return super.getId();
}
}
4、渲染模板
此处 template 为 第三步的模板地址
import org.apache.velocity.Template;
import java.io.StringWriter;
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);
5、这里文件输出到zip
import org.apache.commons.io.IOUtils;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
// 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, genTable)));
IOUtils.write(sw.toString(), zip, "UTF-8");
IOUtils.closeQuietly(sw);
zip.flush();
zip.closeEntry();
IOUtils.closeQuietly(zip);
6、写出文件信息
String filePath = ".zip";
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data, 0, data.length);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
更多推荐
已为社区贡献1条内容
所有评论(0)