环境说明
- Mybatis Plus 3.5.2
- Spring Boot 2.3.12.RELEASE
步骤说明
使用
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
|
这里 mybatis-plus-generator
与 mybatis-plus-boot-starter
版本应一致。
CodeGenerator 示例
package com.axinblog.mall;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Arrays; import java.util.Collections; import java.util.List;
/** * Mybatis Plus 代码生成器(新版) */ public class CodeGenerator {
public static void main(String[] args) { // 数据库配置 DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig .Builder("jdbc:mysql://192.168.10.10:3306/axinblog" + "?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true" , "homestead", "secret"); FastAutoGenerator.create(dataSourceConfigBuilder) // 全局配置 .globalConfig((scanner, builder) -> { builder.author(scanner.apply("请输入作者名称?")) // 覆盖已生成文件 .fileOverride() // 指定输出目录 .outputDir(System.getProperty("user.dir") + "/axin-core/src/main/java/") // 开启 swagger 模式 .enableSwagger() // 禁止打开输出目录 .disableOpenDir() // 时间策略 .dateType(DateType.ONLY_DATE) // 类注释日期的格式 .commentDate("yyyy-MM-dd HH:mm:ss") .build(); }) // 包配置 .packageConfig((scanner, builder) -> { // 父包名 builder.parent(scanner.apply("请输入父包名?")) // 模块名 .moduleName(scanner.apply("请输入模块名?")) // Entity 包名 .entity("entity") // Service 包名 .service("service") // Service Impl 包名 .serviceImpl("service.impl") // Controller 包名 .controller("controller") // Mapper 包名 .mapper("mapper") // MapperXML 包名 .xml(scanner.apply("请输入MapperXML包名?")) // 路径配置信息 .pathInfo( Collections.singletonMap( OutputFile.xml, System.getProperty("user.dir") + "/axin-core/src/main/resources/mapper/" + scanner.apply("请再次输入MapperXML包名~") ) ); }) //策略配置 .strategyConfig((scanner, builder) -> { // 增加表匹配(内存过滤), include 与 exclude 只能配置一项 builder.addInclude(getTables(scanner.apply("请输入要生成的表名,多个英文逗号分隔?所有输入 all"))) // 增加表排除匹配(内存过滤), include 与 exclude 只能配置一项 // .addExclude(scanner.apply("请输入要忽略的表名,多个英文逗号分隔?")) // 增加过滤表后缀 .addTableSuffix("") // 增加过滤表前缀 .addTablePrefix("") // service 策略配置 .serviceBuilder() // 格式化文件名称 // .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl") // 实体策略配置 .entityBuilder() // 开启 lombok 模型 .enableLombok() // 开启生成实体时生成字段注解 .enableTableFieldAnnotation() // controller 策略配置 .controllerBuilder() .formatFileName("%sController") // 开启生成@RestController 控制器 .enableRestStyle() // mapper 策略配置 .mapperBuilder() // 设置父类 .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper"); }) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .templateEngine(new FreemarkerTemplateEngine()) // 不生成控制器代码 .templateConfig(builder -> builder.controller("")) .execute(); }
/** * 处理 all 情况 */ protected static List<String> getTables(String tables) { return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(",")); } }
|
问题解决
不生成Controller
设置 Controller
模板为空即可
// 不生成控制器代码 .templateConfig(builder -> builder.controller(""))
|
Error attempting to get column ‘create_time’ from result set. Cause: java.sql.SQLFeatureNotSupported
更改entity
类中 LocalDateTime
为 Date
因代码为Mybatis Plus
生成,配置代码生成为如下
// 时间策略 .dateType(DateType.ONLY_DATE)
|
mybatis plus net.sf.jsqlparser.schema.Column.withColumnName
Mybatis Plus 与 PageHelper jsqlparser 依赖版本冲突
解决如下
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.2</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <artifactId>mybatis-spring</artifactId> <groupId>org.mybatis</groupId> </exclusion> <exclusion> <artifactId>jsqlparser</artifactId> <groupId>com.github.jsqlparser</groupId> </exclusion> </exclusions> </dependency>
|
我这还出现因老项目神奇的引入了多个PageHelper也会出现该问题
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.2</version> </dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.8</version> </dependency>
|
删除其中多余的即可
全部报错信息如下
*************************** APPLICATION FAILED TO START ***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor.<clinit>(PaginationInnerInterceptor.java:70)
The following method did not exist:
net.sf.jsqlparser.schema.Column.withColumnName(Ljava/lang/String;)Lnet/sf/jsqlparser/schema/Column;
The method's class, net.sf.jsqlparser.schema.Column, is available from the following locations:
jar:file:/C:/Program%20Files/maven/apache-maven-3.6.3/repository/com/github/jsqlparser/jsqlparser/1.2/jsqlparser-1.2.jar!/net/sf/jsqlparser/schema/Column.class
The class hierarchy was loaded from the following locations:
net.sf.jsqlparser.schema.Column: file:/C:/Program%20Files/maven/apache-maven-3.6.3/repository/com/github/jsqlparser/jsqlparser/1.2/jsqlparser-1.2.jar net.sf.jsqlparser.parser.ASTNodeAccessImpl: file:/C:/Program%20Files/maven/apache-maven-3.6.3/repository/com/github/jsqlparser/jsqlparser/1.2/jsqlparser-1.2.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of net.sf.jsqlparser.schema.Column
Disconnected from the target VM, address: '127.0.0.1:63407', transport: 'socket'
Process finished with exit code 1
|
参考文献
代码生成器配置新