Java超級實用的Freemarker工具類
更新時間:2022年02月18日 17:19:59 作者:梁云亮
這篇文章主要介紹了Java超級實用的Freemarker工具類,文章圍繞相關(guān)資料介紹以及代碼描述非常詳細,需要的小伙伴可以參考一下,希望對你得學(xué)習(xí)有所幫助
一、工具類
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); ? ? } }
到此這篇關(guān)于Java超級實用的Freemarker工具類的文章就介紹到這了,更多相關(guān)實用的Freemarker工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- java Spring整合Freemarker的詳細步驟
- Java操作FreeMarker模板引擎的基本用法示例小結(jié)
- 使用Java進行FreeMarker的web模板開發(fā)的基礎(chǔ)教程
- 基于Java的Spring框架來操作FreeMarker模板的示例
- java Freemarker頁面靜態(tài)化實例詳解
- Java實現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片)
- 基于Freemarker和xml實現(xiàn)Java導(dǎo)出word
- JAVA集成Freemarker生成靜態(tài)html過程解析
- 在Java中FreeMarker?模板來定義字符串模板
- Java使用Freemarker頁面靜態(tài)化生成的實現(xiàn)
相關(guān)文章
idea2023設(shè)置啟動參數(shù)、單元測試啟動參數(shù)
在使用IDEA進行開發(fā)時,我們可以通過設(shè)置一些啟動參數(shù)來優(yōu)化開發(fā)環(huán)境的性能和體驗,具有一定的參考價值,感興趣的可以了解一下2023-11-11http basic authentication通過post方式訪問api示例分享 basic認證示例
在HTTP中,基本認證是一種用來允許Web瀏覽器或其他客戶端程序在請求時提供以用戶名和口令形式的憑證,這篇文章主要介紹了http basic authentication通過post方式訪問api示例,大家參考使用吧2014-01-01Spring JDK動態(tài)代理實現(xiàn)過程詳解
這篇文章主要介紹了Spring JDK動態(tài)代理實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02