PixelAI-backend/target/classes/templates/controller.java.vm
2024-10-28 15:31:30 +08:00

154 lines
6.3 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ${package.Controller};
import org.springframework.web.bind.annotation.RequestMapping;
import com.base.annotation.SysLog;
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
import com.base.helper.Result;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* <p>
* $!{table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
@Api(tags = "${entity}")
#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
private static final Logger LOG = LoggerFactory.getLogger(${table.controllerName}.class);
@Autowired
private ${table.serviceName} entityService;
@SysLog(action = "findByPage", value = "分页查询实体")
@ApiOperation(value = "分页查询实体", notes = "分页查询实体")
@RequestMapping(method = RequestMethod.GET)
@ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
@ApiImplicitParam(name = "current", value = "当前页面从1开始", paramType = "query")})
public Result<IPage<${entity}>> findByPage(final ${entity} example, final Page page) {
IPage<${entity}> records = entityService.page(example,page);
return new Result(true, records);
}
@SysLog(action = "delete", value = "删除实体")
@ApiOperation(value = "删除实体")
@RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
@ApiImplicitParam(name = "id", value = "实体ID", required = true, paramType = "path")
public Result delete(@PathVariable final Integer id) {
try {
entityService.removeById(id);
return new Result(true, "成功删除实体", null);
} catch (DataIntegrityViolationException e) {
LOG.error("删除实体失败", e);
return new Result(false, "删除实体失败", "该实体不能删除,存在其他关联数据");
} catch (Exception e) {
LOG.error("删除实体失败", e);
return new Result(false, "删除实体失败", e.getMessage());
}
}
@SysLog(action = "one", value = "查询单个实体")
@ApiOperation(value = "查询单个实体")
@RequestMapping(value = "{id}", method = {RequestMethod.GET})
@ApiImplicitParam(name = "id", value = "实体ID", required = true, paramType = "path")
public Result one(@PathVariable final Integer id) {
try {
${entity} entity = entityService.getById(id);
return new Result(true, entity);
} catch (Exception e) {
LOG.error("查询单个实体失败", e);
return new Result(false, new ${entity}());
}
}
@SysLog(action = "add", value = "添加实体")
@ApiOperation(value = "添加实体")
@RequestMapping(method = {RequestMethod.POST})
public Result add(@Valid @RequestBody final ${entity} entity, final BindingResult result) {
try {
if (result.hasErrors()) {
Map<String, String> map = this.getErrors(result);
String errorMsg = map.entrySet().iterator().next().getValue();
return new Result(false, "保存实体失败", errorMsg, map);
} else {
entityService.save(entity);
return new Result(true, "成功保存实体", null);
}
} catch (Exception e) {
LOG.error("添加实体失败", e);
return new Result(false, "保存实体失败", e.getMessage());
}
}
@SysLog(action = "update", value = "修改实体")
@ApiOperation(value = "修改实体")
@RequestMapping(method = {RequestMethod.PUT})
public Result update(@Valid @RequestBody final ${entity} entity, final BindingResult result) {
try {
if (result.hasErrors()) {
Map<String, String> map = this.getErrors(result);
String errorMsg = map.entrySet().iterator().next().getValue();
return new Result(false, "修改实体失败", errorMsg, map);
} else {
if (null == entity.getId()) {
throw new RuntimeException("id不能为空");
}
entityService.updateById(entity);
return new Result(true, "成功修改实体", null);
}
} catch (Exception e) {
LOG.error("修改实体失败", e);
return new Result(false, "修改实体失败", e.getMessage());
}
}
@ApiOperation(value = "全部实体")
@RequestMapping(value = "all", method = RequestMethod.GET)
public Result<List<${entity}>> all(${entity} example) {
List<${entity}> entitys = entityService.list(example);
if (null != entitys) {
return new Result<>(true, entitys);
}
return new Result<>(true, Collections.emptyList());
}
}
#end