idea配置SpringBoot+MybatisPlus的Controller模板,一键生成Controller
package ${PACKAGE_NAME};
#set($ch = "")
#set($first = true)
#set($EntityName = $NAME.replaceAll("Controller", ""))
#foreach($ch in $EntityName.toCharArray())
#set($ch = $ch + "")
#if($ch == $ch.toUpperCase())
#if($first)
#set($entityName = $ch.toLowerCase())
#set($entity_name = $ch.toLowerCase())
#set($first = false)
#else
#set($entityName = $entityName + $ch)
#set($entity_name = $entity_name + "_" + $ch.toLowerCase())
#end
#else
#set($entity_name = $entity_name + $ch)
#set($entityName = $entityName + $ch)
#end
#end
#set($PARENT_PACKAGE = "")
#set($lastDotIndex = $PACKAGE_NAME.lastIndexOf("."))
#if($lastDotIndex > 0)
#set($PARENT_PACKAGE = $PACKAGE_NAME.substring(0, $lastDotIndex))
#else
#set($PARENT_PACKAGE = $PACKAGE_NAME)
#end
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import ${PARENT_PACKAGE}.service.${EntityName}Service;
import ${PARENT_PACKAGE}.entity.${EntityName}Entity;
import ${PARENT_PACKAGE}.util.R;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
/**
* @author ${USER}
* @description 针对表【t_${entity_name}】的数据库操作Controller实现
* @createDate ${TIME}
*/
@Slf4j
@RestController
@RequestMapping("/${entity_name}")
public class ${EntityName}Controller {
@Resource
private ${EntityName}Service ${entityName}Service;
/**
* 新增或更新
*/
@PostMapping("/save")
public R save(@RequestBody ${EntityName}Entity ${entityName}){
log.info("${entityName}: {}", JSON.toJSONString(${entityName}));
if (${entityName}.getId() == null) {
boolean flag = ${entityName}Service.save(${entityName});
return flag ? R.ok() : R.error("新增失败");
} else {
${entityName}.setUpdateTime(new Date());
boolean flag = ${entityName}Service.updateById(${entityName});
return flag ? R.ok() : R.error("更新失败");
}
}
/**
* 删除
*/
@DeleteMapping("/{id}")
public R delete(@PathVariable Long id){
if (id == null) {
return R.error("删除失败");
}
boolean flag = ${entityName}Service.removeById(id);
return flag ? R.ok() : R.error("删除失败");
}
/**
* 查询
*/
@GetMapping("/{id}")
public R get(@PathVariable Long id){
if (id == null) {
return R.error("查询失败");
}
${EntityName}Entity ${entityName} = ${entityName}Service.getById(id);
return R.ok().data(${entityName});
}
/**
* 分页查询
*/
@PostMapping("/list")
public R list(@RequestParam(name = "current", defaultValue = "1") int current,
@RequestParam(name = "size", defaultValue = "10") int size) {
log.info("current: {}, size: {}", current, size);
Page<${EntityName}Entity> page = new Page<>(current, size);
${entityName}Service.page(page);
return R.ok().data(page);
}
}