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();
? ? }
}二、測試
- 模板文件: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});
}- 測試代碼:
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頁面靜態(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頁面靜態(tài)化生成的實(shí)現(xiàn)
相關(guān)文章
關(guān)于泛型擦除問題的解決--Mybatis查詢類型轉(zhuǎn)換
這篇文章主要介紹了關(guān)于泛型擦除問題的解決--Mybatis查詢類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
在SpringBoot中使用lombok的注意事項(xiàng)
這篇文章主要介紹了在SpringBoot中使用lombok的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Maven Spring jar包啟動(dòng)報(bào)錯(cuò)問題解決方案
maven 編譯jar包,放在linux服務(wù)器啟動(dòng)不起來,提示:xxxx-0.0.1-SNAPSHOT.jar中沒有主清單屬性,接下來通過本文給大家分享問題原因及解決方案,感興趣的朋友跟隨小編一起看看吧2023-10-10
解決引入spring-cloud-starter-openfeign后部分類找不到的問題
這篇文章主要介紹了解決引入spring-cloud-starter-openfeign后部分類找不到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot如何添加server.servlet.context-path相關(guān)使用
這篇文章主要介紹了Springboot如何添加server.servlet.context-path相關(guān)使用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java?實(shí)現(xiàn)判定順序表中是否包含某個(gè)元素(思路詳解)
這篇文章主要介紹了Java?實(shí)現(xiàn)判定順序表中是否包含某個(gè)元素,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
出現(xiàn)次數(shù)超過一半(50%)的數(shù)
給出n個(gè)數(shù),需要我們找出出現(xiàn)次數(shù)超過一半的數(shù),下面小編給大家分享下我的實(shí)現(xiàn)思路及關(guān)鍵代碼,感興趣的朋友一起學(xué)習(xí)吧2016-07-07

