MyBatis Plus Generator——基于Velocity的Controller参考模板(集成MyBatis PlusSwagger2自封装Response分页)

Posted Starzkg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis Plus Generator——基于Velocity的Controller参考模板(集成MyBatis PlusSwagger2自封装Response分页)相关的知识,希望对你有一定的参考价值。

代码生成器

MyBatis Plus Generator——MyBatis Plus代码生成器DEMO

解决方案

包含:MyBatis Plus、Swagger2、自封装Response、MyBatis Plus分页插件、主键类型判断

package $package.Controller;

import com.hailiu.web.bean.ResponseBean;
import $package.Entity.$entity;
import $package.Service.$table.serviceName;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#if($restControllerStyle)
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if($superControllerClassPackage)
import $superControllerClassPackage;
#end
import static com.hailiu.web.common.Constants.PAGE_LIMIT;
import static com.hailiu.web.common.Constants.DEFAULT_PAGE_NUM;

/**
 * $!table.comment 前端控制器
 *
 * @author $author
 * @version 1.0.0
 * @date $date $cfg.time
 */
#if($restControllerStyle)
@RestController
#else
@Controller
#end
@RequestMapping("#if($package.ModuleName)/$package.ModuleName#end/#if($controllerMappingHyphenStyle)$controllerMappingHyphen#else$table.entityPath#end")
@Api(tags = "$entity前端控制器")
#if($kotlin)
class $table.controllerName#if($superControllerClass) : $superControllerClass()#end

#else
#if($superControllerClass)
public class $table.controllerName extends $superControllerClass 
#else
public class $table.controllerName 
#end
    private final $table.serviceName i$entityService;

    public $table.controllerName($table.serviceName i$entityService) 
        this.i$entityService = i$entityService;
    
    
    /**
    * $!table.comment列表
    * @return ResponseBean
    */
    @ApiOperation(value = "获取所有$!table.comment",notes = "获取所有$!table.comment")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResponseBean list()
        return ResponseBean.success(i$entityService.list());
    

    /**
     * $!table.comment分页
     * @return ResponseBean
     */
    @ApiOperation(value = "$!table.comment分页查询",notes = "$!table.comment分页查询")
    @ResponseBody
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResponseBean page(@ApiParam(name = "page",value = "页数")
                            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer page,
                            @ApiParam(name = "size",value = "记录数")
                            @RequestParam(defaultValue = PAGE_LIMIT) Integer size)
        return ResponseBean.success(i$entityService.page(new Page<>(page,size)));
    
    
    #foreach($field in $table.fields)
    #if($field.keyFlag)
    /**
     * 获取指定$!table.comment
     * @param id $!table.commentID
     * @return ResponseBean
     */
    @ApiOperation(value = "获取指定$!table.comment",notes = "获取指定$!table.comment")
    @RequestMapping(value = "/id",method = RequestMethod.GET)
    public ResponseBean get(@PathVariable(value = "id") $field.propertyType id)
        $entity $table.entityPath = i$entityService.getById(id);
        if($table.entityPath != null)
            return ResponseBean.success("查询成功",$table.entityPath);
        else
            return ResponseBean.error("查询失败");
        
    

    /**
     * 添加指定$!table.comment
     * @param $table.entityPath $!table.comment
     * @return ResponseBean
     */
    @ApiOperation(value = "添加指定$!table.comment",notes = "添加指定$!table.comment")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseBean post(@RequestBody $entity $table.entityPath)
        $table.entityPath.setId(null);
        if(i$entityService.save($table.entityPath))
            return ResponseBean.success("添加成功", $table.entityPath);
        else
            return ResponseBean.error("添加失败");
        
    

    /**
     * 更新指定$!table.comment
     * @param id $!table.commentID
     * @param $table.entityPath $!table.comment
     * @return ResponseBean
     */
    @ApiOperation(value = "更新指定$!table.comment",notes = "更新指定$!table.comment")
    @RequestMapping(value = "/id",method = RequestMethod.PUT)
    public ResponseBean put(@PathVariable(value = "id") $field.propertyType id,@RequestBody $entity $table.entityPath)
        $table.entityPath.setId(id);
        if(i$entityService.updateById($table.entityPath))
            return ResponseBean.success("更新成功");
        else
            return ResponseBean.error("更新失败");
        
    

    /**
     * 删除指定$!table.comment
     * @param id $!table.commentID
     * @return ResponseBean
     */
    @ApiOperation(value = "删除指定$!table.comment",notes = "删除指定$!table.comment")
    @RequestMapping(value = "/id",method = RequestMethod.DELETE)
    public ResponseBean delete(@PathVariable(value = "id") $field.propertyType id)
        if(i$entityService.removeById(id))
            return ResponseBean.success("删除成功");
        else
            return ResponseBean.error("删除失败");
        
    
    #end
    #end


#end

参考文章

基于mybatis-plus的代码自动生成工具(自定义模板)

MyBatis Plus Generator——MyBatis Plus代码生成器DEMO

以上是关于MyBatis Plus Generator——基于Velocity的Controller参考模板(集成MyBatis PlusSwagger2自封装Response分页)的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis-Plus开发提速器mybatis-plus-generator-ui

使用mybatis-plus-generator进行代码自动生成

mybatis-plus-generator 模板生成代码

springboot+mybatis-plus-generator3.5.1代码生产

mybatis-plus generator代码生成器使用教程

mybatis-plus-generator的使用