Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Freemarker class of Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use the Freemarker class of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Tool class public class FreemarkerUtil {/ * according to the template, using the data provided Generate file * @ param ftlNameWithPath template file * @ param data data * @ param aimFileName final generated file * @ throws IOException * @ throws TemplateException * / public static void execute (String ftlNameWithPath, Map data, String aimFileName) throws IOException, TemplateException {Configuration cfg = new Configuration (Configuration.VERSION_2_3_25) / / create a Freemarker configuration instance 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)); / / load template file Writer out = new FileWriter (new File (aimFileName)); t1.process (data, out); out.flush () Out.close ();}} II. Testing

Template file: service.ftl

Package com.resume.service;import com.baomidou.mybatisplus.extension.service.IService;import com.resume.domain.$ {className}; import java.util.List;/*** @ Author: Liang Yunliang * @ Date: 2021-7-14 1315 Author 51* @ Describe:*/public interface ${className} Service extends IService {/ * query all available ${comment} information * * @ return * / List listAllUsable$ {className} () / * change the state of ${comment} with the specified number * * @ param id * @ param status * @ return the return value indicates the number of rows of the affected record * / boolean modify$ {className} Status (Integer id, Integer status) / * modify ${comment} information * @ param ${objName} * @ return * / boolean modify (${className} ${objName});}

Test the code:

Public class GenApplication {private static String className = "Project"; private static String objName = "project"; private static String comment = "Future experience"; private static String basePath = "src/main/java/com/resume/"; public static void main (String [] args) throws IOException, TemplateException {/ / generate Service interface genService (className, objName, comment) } / * generate Service interface * * @ 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 map = new HashMap () Map.put ("objName", objName); map.put ("className", className); map.put ("comment", comment); FreemarkerUtil.execute (ftlNameWithPath, map, aimFileName);}} "how to use the Freemarker class of Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report