代码生成器
步骤一:在pom.xml中添加相应的依赖
<!--代码生成器--> <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.6.2</version> </dependency>
步骤二: 编写代码生成器
在项目中添加utils/CodeGeneratory.java
package com.itheima.sys_springboot.utils; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import java.util.Collections; /** * mp 代码生成器 * by zzq * @since 2023-3-12 */ public class CodeGeneratory { public static void main(String[] args) { generate(); } public static void generate(){ FastAutoGenerator.create("jdbc:mysql://localhost:3306/test01?serverTimezone=GMT%2b8", "root", "123456") .globalConfig(builder -> { builder.author("zzq") // 设置作者 // .enableSwagger() // 开启 swagger 模式 .fileOverride() // 覆盖已生成文件 .outputDir("D:\\zzqproject\\project\\sys_springboot\\src\\main\\java\\"); // 指定输出目录 }) .packageConfig(builder -> { builder.parent("com.itheima.sys_springboot") // 设置父包名 .moduleName("") // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\zzqproject\\project\\sys_springboot\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude("sys_user") // 设置需要生成的表名 .addTablePrefix("t_", "sys_"); // 设置过滤表前缀 }) // .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }
说明:其中每一个文件对应的路径,如下图所示:
步骤三:直接运行即可
步骤四: 添加controller的控制端模板(模板内容的具体位置信息),并把文件复制到resources/templates中
如图所示:
对其中的模板内容进行编写
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 $!{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}> index(){ return ${table.entityPath}Service.list(); } ## 根据id查询 @GetMapping("/{id}") public ${entity} findOne(@PathVariable Integer id){ return ${table.entityPath}Service.getById(id); } ## 分页查询 @GetMapping("/page") public Page<${entity}> fondPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){ return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize)); } } #end
步骤五:直接运行CodeGeneratory.java中的main函数,然后与生成的UserController进行依次对比
可以得到:
User <==> ${entity} userService <===> ${table.entityPath}Service 等等
最终生成UserController.java
package com.itheima.sys_springboot.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.itheima.sys_springboot.service.IUserService; import com.itheima.sys_springboot.entity.User; import org.springframework.stereotype.Controller; /** * <p> * 前端控制器 * </p> * * @author zzq * @since 2023-03-12 */ @Controller @RequestMapping("//user") public class UserController { @Resource private IUserService userService; @PostMapping public boolean save(@RequestBody User user){ return userService.saveOrUpdate(user); } @DeleteMapping("/{id}") public boolean delete(@PathVariable Integer id){ return userService.removeById(id); } @GetMapping("/") public List<User> index(){ return userService.list(); } @GetMapping("/{id}") public User findOne(@PathVariable Integer id){ return userService.getById(id); } @GetMapping("/page") public Page<User> fondPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){ return userService.page(new Page<>(pageNum, pageSize)); } }标签:代码生成,return,springboot,id,import,mybatis,table,public,entityPath From: https://www.cnblogs.com/Zhangzhiq/p/17208149.html