1.添加maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. 添加如下代码:
Configuration cfg = new Configuration(); cfg.setDefaultEncoding("utf-8"); Template template = null; //加载模板的路径 cfg.setDirectoryForTemplateLoading(new File(SecurityUtilities.getSystemProperty("user.dir")+File.separator+"src/main/resources/templates")); //模板名称获取 template = cfg.getTemplate("Request2.ftl"); //模板里的参数 Map<String, Object> map = new HashMap<>(); map.put("description","dddd"); map.put("paramsGenerator","kkkkkk"); map.put("dataType","666ddd"); map.put("description","dddd"); map.put("paramster","dduu"); map.put("fieldName","ddkk"); StringWriter result = new StringWriter(); //接收模板和参数渲染后的结果 template.process(map, result); //将结果输出到指定的的路径 OutputStreamWriter oWriter = new OutputStreamWriter(new FileOutputStream("D:\\data\\b.java"), "utf-8"); BufferedWriter bufferedWriter = new BufferedWriter(oWriter); bufferedWriter.write(result.toString()); bufferedWriter.close(); oWriter.close();
模板:
内容:
/**
* ${description}
*/
@ApiModelProperty(value = "${description}")
private ${paramsGenerator};
public ${dataType} get${fieldName}() {
return ${paramster};
}
最终输出结果:
标签:map,description,cfg,freemark,指定,put,new,模板 From: https://www.cnblogs.com/yangxiaohui227/p/17154673.html