ps:用了20多天以后更新。
注意在实际使用中。3.5.1这个版本,多节点多线程批量插入操作时会报主键冲突。
https://github.com/baomidou/mybatis-plus/issues/3289#issue-791890470

------------------------------新更新↑---------------------------------

建议直接复制这里贴的代码,自己对比改的话,尤其注意标注有‘注意’的地方。

版本

版本"3.5.1" 而不是"最新版本"

pom.xml

<!-- mybatis-plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>
<!-- mybatis-plus 代码生成器 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
<!--  模版引擎  -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>

CodeGenerator.java

位置:src/main/java/com/xx/xx/utils/CodeGenerator.java
注意!!!复制官方代码时需要注释掉 templateEngine,模块名需要修改为空moduleName(null),注意相关的策略配置StrategyConfig

package com.xx.xx.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Collections;


public class CodeGenerator {
    public static void main(String[] args) {
        generate();
    }

    private static void generate() {
        FastAutoGenerator.create("数据库地址", "用户名", "密码")
            .globalConfig(builder -> {
                builder.author("xx") // 设置作者
                        .enableSwagger() // 开启 swagger 模式
                        .fileOverride() // 覆盖已生成文件
                        .outputDir("D:\\xx\\xx\\src\\main\\java\\"); // 指定输出目录
            })
            .packageConfig(builder -> {
                builder.parent("com.xx.xx") // 设置父包名
                        .moduleName(null) // 设置父包模块名(空字符串"",地址会变成//)
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\xx\\src\\main\\java\\com\\xx\\xx\\mapper\\")); // 设置mapperXml生成路径
            })
            .strategyConfig(builder -> {
                builder.entityBuilder().enableLombok();
//                builder.mapperBuilder().enableMapperAnnotation().build();  MybatisPlusCong.java文件里加过MapperScan,这里就不需要开启
                builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                builder.addInclude("xx") // 设置需要生成的表名
                        .addTablePrefix("t_", "c_"); // 设置过滤表前缀
            })
//            .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
            .execute();
    }

}

controller.java.vm

位置:src/main/resources/templates/controller.java.vm

QueryWrapper<${entity}>需要修改到

package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end

/**
 * <p>
 * $!{table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end

#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end

    @Resource
    private ${table.serviceName} ${table.entityPath}Service;

    @PostMapping
    public Boolean save(@RequestBody ${entity} ${table.entityPath}) {
        return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return ${table.entityPath}Service.removeById(id);
    }

    @GetMapping
    public List<${entity}> findAll() {
        return ${table.entityPath}Service.list();
    }

    @GetMapping("/{id}")
    public ${entity} findOne(@PathVariable Integer id) {
        return ${table.entityPath}Service.getById(id);
    }

    @GetMapping("/page")
    public Page<${entity}> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize) {
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize));
    }

}

#end

注意:如果刚刚CodeGenerator.java里的没有开启enableRestStyle的话需要:

修改生成以后的UserController

@Controller
改为:
@RestController

Adele0
44 声望3 粉丝