一、问题描述
在使用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>