项目搭建 Day07 -mybatis-plus模板生成controller

项目练习 / 2021-12-24

mall Day07

mall-learning
修改 controller的模板使其自动生成对应的crud
Idea设置java类方法注释模板
controller.java.ftl

package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

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

        @Autowired
        private I${entity}Service demoService;


        @ApiOperation(value = "集合", notes = "集合")
        @RequestMapping(value = "listAll", method = RequestMethod.GET)
        @ResponseBody
        public Result<List<${entity}>> getBrandList() {
        return Result.OK(demoService.list());
        }

        @ApiOperation(value = "添加", notes = "添加")
        @RequestMapping(value = "/create", method = RequestMethod.POST)
        @ResponseBody
        public Result<?> createBrand(@RequestBody ${entity} ${entity}) {
            boolean save = demoService.save(${entity});
            if (save) {
                log.info("添加成功:{}",${entity});
                return Result.OK("添加成功");
            } else {
                log.info("createBrand failed:{}", ${entity});
                return Result.error("操作失败");
            }
        }

        @ApiOperation(value = "编辑", notes = "编辑")
        @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
        @ResponseBody
        public Result<?> updateBrand( @RequestBody ${entity} ${entity}Dto, BindingResult result) {
            boolean update = demoService.updateById(${entity}Dto);
            if (update ) {
                log.info("updateBrand success:{}", ${entity}Dto);
                return Result.OK("编辑成功");
            } else {
                log.info("updateBrand failed:{}", ${entity}Dto);
                return Result.error("编辑失败");
            }
        }

        @ApiOperation(value = "删除", notes = "删除")
        @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
        @ResponseBody
        public Result<?> deleteBrand(@PathVariable("id") String id) {
            boolean count = demoService.removeById(id);
            if (count) {
                 log.info("deleteBrand success :id={}", id);
                 return Result.OK("删除成功");
            } else {
                 log.info("deleteBrand failed :id={}", id);
                 return Result.error("操作失败");
            }
        }

        @ApiOperation(value = "分页", notes = "分页")
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        @ResponseBody
        public Result<Page<${entity}>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
            Page<${entity}> page = new Page<>(pageNum,pageSize);
            Page<${entity}> page1 = demoService.page(page);
            return Result.OK(page1);
        }

        @ApiOperation(value = "获取单个", notes = "获取单个")
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        @ResponseBody
            public Result<${entity}> brand(@PathVariable("id") String id) {
            return Result.OK(demoService.getById(id));
        }
    </#if>

    }
</#if>


```xml