myabtis-plus 代码生成器自定义模板

        mybatis-plus代码生成器默认生成的controller是下面这样的,一个空的controller。

@RestController
@RequestMapping("/sysUserRoleRelevance")
public class SysUserRoleRelevanceController {

}

为了方便编码,可以给controller模板添加自定义内容,重复编码,其他类文件一样,看个人需求。

1、找到原生模板,我们选择ftl类型的模板

myabtis-plus 代码生成器自定义模板_第1张图片

 2、复制原生模板到我们的自定义目录

myabtis-plus 代码生成器自定义模板_第2张图片

 3、给模板添加自定义内容

package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;

<#if superControllerClassPackage??>
import ${superControllerClassPackage};


/**
 * 

* ${table.comment!} 前端控制器 *

* * @author ${author} * @since ${date} */ @Api(tags = "${table.comment!}") <#if restControllerStyle> @RestController <#else> @Controller @RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}") <#if kotlin> class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() <#else> <#if superControllerClass??> public class ${table.controllerName} extends ${superControllerClass} { <#else> public class ${table.controllerName} { @ApiOperation("创建") @PostMapping("/create") public void create(@RequestBody ${table.entityName} <#if package.ModuleName?? && package.ModuleName != "">${package.ModuleName}<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}){} @ApiOperation("修改") @PostMapping("/update") public void update(@RequestBody ${table.entityName} <#if package.ModuleName?? && package.ModuleName != "">${package.ModuleName}<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}){} @ApiOperation("删除") @PostMapping("/delete") public void delete(@RequestBody List idList){} @ApiOperation("查询详情") @PostMapping("/details") public ResponseEntity<${table.entityName}> details(@RequestParam("id") Long id){ return ResponseEntity.ok(null); } @ApiOperation("查询") @PostMapping("/list") public ResponseEntity list(@RequestBody Object dto){ return ResponseEntity.ok(null); } @ApiOperation("分页") @PostMapping("/page") public ResponseEntity page(@RequestBody Object dto){ return ResponseEntity.ok(null); } }

4、在代码生成器中配置自定义模板路径

            //模板配置
            .templateConfig(builder -> {
                builder.controller("/templates/generator/controller.java");
            })

5、运行后效果

@Api(tags = "用户-角色关联表")
@RestController
@RequestMapping("/sysUserRoleRelevance")
public class SysUserRoleRelevanceController {

  @ApiOperation("创建")
  @PostMapping("/create")
  public void create(@RequestBody SysUserRoleRelevance sysUserRoleRelevance){}

  @ApiOperation("修改")
  @PostMapping("/update")
  public void update(@RequestBody SysUserRoleRelevance sysUserRoleRelevance){}

  @ApiOperation("删除")
  @PostMapping("/delete")
  public void delete(@RequestBody List idList){}

  @ApiOperation("查询详情")
  @PostMapping("/details")
  public ResponseEntity details(@RequestParam("id") Long id){
    return ResponseEntity.ok(null);
  }

  @ApiOperation("查询")
  @PostMapping("/list")
  public ResponseEntity list(@RequestBody Object dto){
    return ResponseEntity.ok(null);
  }

  @ApiOperation("分页")
  @PostMapping("/page")
  public ResponseEntity page(@RequestBody Object dto){
    return ResponseEntity.ok(null);
  }
}

你可能感兴趣的:(mybatis-plus笔记,java,mybatis,开发语言)