springboot+mybatis-plus代码生成器
首先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>
加入MybatisPlusConfig

package com.xxxx.demo.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @ClassName MybatisPlusConfig * @Description * @Author Lishipu * @Date 2023/1/9 22:51 * Version 1.0 **/ @Configuration @MapperScan("com.xxxx.demo.mapper") public class MybatisPlusConfig{ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
模板
代码完整是这样的:

package com.xxxx.demo.utils; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import java.util.Collections; /** * @ClassName CodeGenerator * @Description 代码生成器 * @Author Lishipu * @Date 2023/1/10 23:05 * Version 1.0 **/ public class CodeGenerator { public static void main(String[] args) { generate(); } private static void generate() { FastAutoGenerator.create("jdbc:mysql://localhost:3306/qing?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "123456") .globalConfig(builder -> { builder.author("lishipu") // 设置作者 // .enableSwagger() // 开启 swagger 模式 .fileOverride() // 覆盖已生成文件 .outputDir("D:\\项目\\javaee+vue\\vue_01\\vue_springboot02\\src\\main\\java\\"); // 指定输出目录,注意是那个目录加个\\ }) .packageConfig(builder -> { builder.parent("com.xxxx.demo") // 设置父包名 .moduleName(null) // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "E:\\vue_01\\vue_springboot\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径注意加个\\ }) .strategyConfig(builder -> { builder.entityBuilder().enableLombok(); // builder.mapperBuilder().enableMapperAnnotation().build(); builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符 .enableRestStyle(); // 开启生成@RestController 控制器 builder.addInclude("t_user") // 设置需要生成的表名 .addTablePrefix("t_", "sys_"); // 设置过滤表前缀 }) // .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }
第一
数据库名称和密码改一下,
第二
是这个地方,指定完路径之后要加个\\
第三
然后是这个地方,指定完包名
第四
然后是这个地方:
第五
就是修改要生成的表名
最后
也是最主要的,就是新的代码生成器可以定制controller中的增删改查的代码
代码:

package ${package.Controller}; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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); } @PostMapping("/del/batch") public boolean deleteBatch(@RequestBody List<Integer> ids) { return ${table.entityPath}Service.removeByIds(ids); } @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), queryWrapper); } } #end
这个代码的来源是:
这些entity和mapper,service都可以定制