SpringBoot Freemarker基础配置与使用

1.基础配置1.1引入依赖<!-- 引入freemarker模板引擎的依赖 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId>...
摘要由CSDN通过智能技术生成

1.基础配置

1.1引入依赖

<!-- 引入freemarker模板引擎的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

1.2在application.properties配置文件中添加如下配置:

# 是否开启thymeleaf缓存,本地为false,生产建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#类型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#
#文件后缀
spring.freemarker.suffix=.ftl
#路径 .ftl文件就创建在templates下面或者子目录
spring.freemarker.template-loader-path=classpath:/templates/

2.基础使用

首先我在controller类中编写一个测试接口用来测试使用,编写内容如下

@Controller
public class FreeMarkerController {
   

    Map<String ,Object> map = new HashMap<>();
    List<Object> list = new ArrayList();
    
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,我可以用mybatisplus+springboot+freemarker模板引擎写一个代码生成器,满足您的需求。 首先,我们需要在pom.xml文件中添加mybatis-plus、freemarker和分页插件的依赖。 ```xml <!-- mybatis-plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- freemarker依赖 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <!-- mybatis-plus分页插件依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.0</version> </dependency> ``` 然后,我们需要定义一个模板,用于生成代码。模板中可以使用freemarker的语法,通过变量来传递参数。例如,我们可以定义一个controller模板: ```java package ${basePackage}.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import ${basePackage}.entity.${className}; import ${basePackage}.service.${className}Service; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController @RequestMapping("/${basePath}/${className}") @RequiredArgsConstructor public class ${className}Controller { private final ${className}Service ${lowerClassName}Service; @GetMapping("/{id}") public ${className} getById(@PathVariable Long id) { return ${lowerClassName}Service.getById(id); } @GetMapping public List<${className}> getAll() { return ${lowerClassName}Service.list(); } @GetMapping("/page") public Page<${className}> getPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String keyword) { return ${lowerClassName}Service.getPage(pageNum, pageSize, keyword); } @PostMapping public void add(@RequestBody @Valid ${className} ${lowerClassName}) { ${lowerClassName}Service.save(${lowerClassName}); } @PutMapping public void update(@RequestBody @Valid ${className} ${lowerClassName}) { ${lowerClassName}Service.updateById(${lowerClassName}); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { ${lowerClassName}Service.removeById(id); } } ``` 接下来,我们可以编写一个代码生成器的工具类,来生成代码。该工具类需要实现以下功能: 1. 解析模板文件,获取模板内容。 2. 根据模板内容,替换变量,生成代码文件。 3. 将生成的代码文件写入磁盘。 代码生成器的实现如下: ```java package com.example.codegenerator; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import lombok.RequiredArgsConstructor; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @RequiredArgsConstructor public class CodeGenerator { private final String basePackage; // 基础包名 private final String basePath; // 基础路径 private final String templatePath; // 模板路径 private final String templateName; // 模板名称 private final String className; // 类名 public void generate() throws IOException, TemplateException { // 创建freemarker配置对象 Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); // 设置模板加载路径 configuration.setClassForTemplateLoading(getClass(), "/"); // 设置编码方式 configuration.setDefaultEncoding(StandardCharsets.UTF_8.name()); // 加载模板文件 Template template = configuration.getTemplate(templatePath + "/" + templateName); // 设置模板变量 Map<String, Object> model = new HashMap<>(); model.put("basePackage", basePackage); model.put("basePath", basePath); model.put("className", className); model.put("lowerClassName", className.substring(0, 1).toLowerCase() + className.substring(1)); // 生成代码文件 String code = processTemplate(template, model); writeToFile(basePath + "/" + className + ".java", code); } private String processTemplate(Template template, Map<String, Object> model) throws IOException, TemplateException { StringBuilder sb = new StringBuilder(); template.process(model, new FileWriter(sb)); return sb.toString(); } private void writeToFile(String fileName, String content) throws IOException { File file = new File(fileName); file.getParentFile().mkdirs(); try (FileWriter writer = new FileWriter(file)) { writer.write(content); } } } ``` 最后,我们可以在main方法中调用代码生成器,来生成代码: ```java public static void main(String[] args) throws IOException, TemplateException { // 基础包名 String basePackage = "com.example"; // 基础路径 String basePath = "src/main/java/com/example"; // 模板路径 String templatePath = "templates"; // 模板名称 String templateName = "controller.ftl"; // 类名 String className = "User"; CodeGenerator generator = new CodeGenerator(basePackage, basePath, templatePath, templateName, className); generator.generate(); } ``` 通过以上步骤,我们就可以生成一个UserController。您可以根据需要编写其他模板,生成其他文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值