【沉浸式解决问题】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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值