1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class FreemarkerUtil { /** * 根据模板,利用提供的数据,生成文件 * @param ftlNameWithPath 模板文件 * @param data 数据 * @param aimFileName 最终生成的文件 * @throws IOException * @throws TemplateException */ public static void execute(String ftlNameWithPath, Map<String, Object> data, String aimFileName) throws IOException, TemplateException { Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);//创建Freemarker配置实例
int i = ftlNameWithPath.lastIndexOf("/") == -1 ? ftlNameWithPath.lastIndexOf("\\") : ftlNameWithPath.lastIndexOf("/");
cfg.setDirectoryForTemplateLoading(new File(ftlNameWithPath.substring(0, i + 1)));
cfg.setDefaultEncoding("UTF-8");
Template t1 = cfg.getTemplate(ftlNameWithPath.substring(i + 1));//加载模板文件 Writer out = new FileWriter(new File(aimFileName)); t1.process(data, out); out.flush(); out.close(); }
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.resume.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.resume.domain.${className};
import java.util.List;
/** * @Author: 梁云亮 * @Date: 2021/7/14 13:51 * @Describe: */ public interface ${className}Service extends IService<${className}> {
/** * 查询出所有的可以使用的${comment}信息 * * @return */ List<${className}> listAllUsable${className}();
/** * 改变指定编号的${comment}的状态 * * @param id * @param status * @return 返回值表示受影响的记录的行数 */ boolean modify${className}Status(Integer id, Integer status);
/** * 根据条件修改${comment}信息 * @param ${objName} * @return */ boolean modify(${className} ${objName});
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public class GenApplication { private static String className = "Project"; private static String objName = "project"; private static String comment = "期日经验";
private static String basePath = "src/main/java/com/resume/";
public static void main(String[] args) throws IOException, TemplateException { // 生成Service接口 genService(className, objName, comment); }
/** * 生成Service接口 * * @param className * @param objName * @throws IOException * @throws TemplateException */ private static void genService(String className, String objName, String comment) throws IOException, TemplateException { String ftlNameWithPath = basePath + "utils/gen/ftl/service.ftl"; String aimFileName = basePath + "service/" + className + "Service.java"; Map<String, Object> map = new HashMap<>(); map.put("objName", objName); map.put("className", className); map.put("comment", comment); FreemarkerUtil.execute(ftlNameWithPath, map, aimFileName); }
} |