Java超級實用的Freemarker工具類
更新時間:2022年02月18日 17:19:59 作者:梁云亮
這篇文章主要介紹了Java超級實用的Freemarker工具類,文章圍繞相關資料介紹以及代碼描述非常詳細,需要的小伙伴可以參考一下,希望對你得學習有所幫助
一、工具類
public class FreemarkerUtil {
? ? /**
? ? ?* 根據(jù)模板,利用提供的數(shù)據(jù),生成文件
? ? ?* @param ftlNameWithPath 模板文件
? ? ?* @param data 數(shù)據(jù)
? ? ?* @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);//創(chuàng)建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();
? ? }
}二、測試
- 模板文件:service.ftl
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}的狀態(tài)
? ? *
? ? * @param id
? ? * @param status
? ? * @return 返回值表示受影響的記錄的行數(shù)
? ? */
? ? boolean modify${className}Status(Integer id, Integer status);
? ? /**
? ? * 根據(jù)條件修改${comment}信息
? ? * @param ${objName}
? ? * @return
? ? */
? ? boolean modify(${className} ${objName});
}- 測試代碼:
public class GenApplication {
? ? private static String className = "Project";
? ? private static String objName = "project";
? ? private static String comment = "期日經(jīng)驗";
? ? 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);
? ? }
}到此這篇關于Java超級實用的Freemarker工具類的文章就介紹到這了,更多相關實用的Freemarker工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- java Spring整合Freemarker的詳細步驟
- Java操作FreeMarker模板引擎的基本用法示例小結
- 使用Java進行FreeMarker的web模板開發(fā)的基礎教程
- 基于Java的Spring框架來操作FreeMarker模板的示例
- java Freemarker頁面靜態(tài)化實例詳解
- Java實現(xiàn)用Freemarker完美導出word文檔(帶圖片)
- 基于Freemarker和xml實現(xiàn)Java導出word
- JAVA集成Freemarker生成靜態(tài)html過程解析
- 在Java中FreeMarker?模板來定義字符串模板
- Java使用Freemarker頁面靜態(tài)化生成的實現(xiàn)
相關文章
解決引入spring-cloud-starter-openfeign后部分類找不到的問題
這篇文章主要介紹了解決引入spring-cloud-starter-openfeign后部分類找不到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot如何添加server.servlet.context-path相關使用
這篇文章主要介紹了Springboot如何添加server.servlet.context-path相關使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Java?實現(xiàn)判定順序表中是否包含某個元素(思路詳解)
這篇文章主要介紹了Java?實現(xiàn)判定順序表中是否包含某個元素,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
出現(xiàn)次數(shù)超過一半(50%)的數(shù)
給出n個數(shù),需要我們找出出現(xiàn)次數(shù)超過一半的數(shù),下面小編給大家分享下我的實現(xiàn)思路及關鍵代碼,感興趣的朋友一起學習吧2016-07-07

