Java超級(jí)實(shí)用的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配置實(shí)例 ? ? ? ? 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(); ? ? } }
二、測(cè)試
- 模板文件: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}(); ? ? /** ? ? * 改變指定編號(hào)的${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}); }
- 測(cè)試代碼:
public class GenApplication { ? ? private static String className = "Project"; ? ? private static String objName = "project"; ? ? private static String comment = "期日經(jīng)驗(yàn)"; ? ? 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超級(jí)實(shí)用的Freemarker工具類的文章就介紹到這了,更多相關(guān)實(shí)用的Freemarker工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java Spring整合Freemarker的詳細(xì)步驟
- Java操作FreeMarker模板引擎的基本用法示例小結(jié)
- 使用Java進(jìn)行FreeMarker的web模板開發(fā)的基礎(chǔ)教程
- 基于Java的Spring框架來操作FreeMarker模板的示例
- java Freemarker頁(yè)面靜態(tài)化實(shí)例詳解
- Java實(shí)現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片)
- 基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word
- JAVA集成Freemarker生成靜態(tài)html過程解析
- 在Java中FreeMarker?模板來定義字符串模板
- Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn)
相關(guān)文章
JavaWeb實(shí)現(xiàn)注冊(cè)用戶名檢測(cè)
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)注冊(cè)用戶名檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08idea2023設(shè)置啟動(dòng)參數(shù)、單元測(cè)試啟動(dòng)參數(shù)
在使用IDEA進(jìn)行開發(fā)時(shí),我們可以通過設(shè)置一些啟動(dòng)參數(shù)來優(yōu)化開發(fā)環(huán)境的性能和體驗(yàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Spring下Filter過濾器配置全局異常處理的詳細(xì)步驟
這篇文章主要介紹了Spring下Filter過濾器配置全局異常處理的詳細(xì)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12http basic authentication通過post方式訪問api示例分享 basic認(rèn)證示例
在HTTP中,基本認(rèn)證是一種用來允許Web瀏覽器或其他客戶端程序在請(qǐng)求時(shí)提供以用戶名和口令形式的憑證,這篇文章主要介紹了http basic authentication通過post方式訪問api示例,大家參考使用吧2014-01-01Spring JDK動(dòng)態(tài)代理實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring JDK動(dòng)態(tài)代理實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02