欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論