【沉浸式解决问题】Caused by: java.lang.ClassNotFoundException: freemarker.template.Configuration

一、问题描述

在使用Mybatis-Plus的代码生成器时报错:

Caused by: java.lang.ClassNotFoundException: freemarker.template.Configuration

在这里插入图片描述


二、场景还原

1. MybatisPlusGenerator.java

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
import org.apache.ibatis.annotations.Mapper;

import java.util.*;

public class MybatisPlusGenerator {

    /**
     * Spring Boot项目生成全部表示例,有调整可以自己修改
     */
    public static void generator() {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8","root","root")
                .globalConfig(builder -> {
                    builder.outputDir((System.getProperty("user.dir")+"/src/main/java"))
                            .disableOpenDir()
                            .author("baomidou")
                            .enableSwagger();
                })
                .packageConfig(builder -> {
                    builder.parent("com.baomidou")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"/src/main/resources/mapper"));
                })
                .strategyConfig(builder -> {
                    builder.enableSkipView()
                            .entityBuilder().enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))
                            .mapperBuilder().mapperAnnotation(Mapper.class);
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }

    public static void main(String[] args) {
         generator()
    }
}

三、原因分析

原因十分的简单,哈哈哈,就是mybaits-plus虽然引入了各模板引擎的依赖,但是设置了不传递,pom文件如下,可以看到所有的依赖都设置了<optional>true</optional>

  <dependencies>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-spring</artifactId>
      <version>3.5.12</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-engine-core</artifactId>
      <version>2.4.1</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.33</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>com.ibeetl</groupId>
      <artifactId>beetl</artifactId>
      <version>3.17.0.RELEASE</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>enjoy</artifactId>
      <version>5.2.2</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.6.14</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>3.0.0</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib-jdk8</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
  </dependencies>

四、解决方案

再次显示声明freemarker的依赖就可以

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.5.12</version>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<scope>compile</scope>
		</dependency>
### 问题分析 在使用 FreeMarker 模板引擎时,出现 `java.lang.ClassNotFoundException: freemarker.template.Configuration` 错误通常表明项目中缺少必要的 FreeMarker 依赖库。该异常意味着 JVM 在运行时无法找到 `freemarker.template.Configuration` 类,这通常是由于构建配置中未正确引入相关依赖导致的。 ### 解决方法 为了解决该问题,需要在项目的依赖管理配置中添加 FreeMarker 的核心依赖。以 Maven 为例,可以在 `pom.xml` 文件中添加如下依赖声明: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> ``` 如果使用的是 MyBatis-Plus Generator,则还需要确保引入其依赖,因为它在内部使用了 FreeMarker 模板引擎进行代码生成: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.12</version> </dependency> ``` 此外,可以显式指定 FreeMarker 依赖的 `scope` 为 `compile`,以确保其在编译和运行时都可用: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> <scope>compile</scope> </dependency> ``` 完成上述配置后,重新构建项目以确保依赖被正确下载并加入到类路径中。 ### 注意事项 - 确保 Maven 或 Gradle 的配置文件中没有拼写错误。 - 如果使用的是旧版本的 Java(如 Java 8),应确认所选 FreeMarker 版本是否兼容。 - 清理项目并重新构建,以确保所有依赖更新生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝吻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值