controller
作者:互联网
下面展示一些 内联代码片
。
package cn.cxjcloud.org.controller;
import cn.cxjcloud.basic.util.AjaxResult;
import cn.cxjcloud.basic.util.PageList;
import cn.cxjcloud.org.domain.Department;
import cn.cxjcloud.org.query.DepartmentQuery;
import cn.cxjcloud.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 以后叫写接口
* 部门接口 restFull风格的接口
* 以后写接口需要考虑
* 1.请求地址是什么 URL
* 2.做什么操作
* 3.返回什么值
*/
@RestController
@RequestMapping("/dept")
/*@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping*/
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
@Autowired
IDepartmentService departmentService;
/**
*
新增和修改使用:@PutMapping 通过有没有Id来确定是不是修改
*/
@PutMapping
@ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
public AjaxResult addOrUpdate(@RequestBody Department department){
try {
if (department.getId() != null) {
departmentService.update(department);
}else{
departmentService.add(department);
}
return AjaxResult.me();
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.me()
.setMessage("操作失败!"+e.getMessage());
}
}
//删除数据 @DeleteMapping("/{id}")
@DeleteMapping("/{id}")
@ApiOperation(value = "删除部门",notes = "需要传递ID")
public AjaxResult deleteById(@PathVariable("id") Long id){
try {
departmentService.delete(id);
return AjaxResult.me();
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.me()
.setMessage("操作失败!"+e.getMessage());
}
}
/**
* 在restFull风格接口中,使用{}占位
* 那么在取值的时候,使用@PathVariable来获取占位的值
* @param id
* @return
*/
@GetMapping("/{id}")//http://127.0.0.1:8080/dept/1
public Department queryById(@PathVariable("id") Long id){
return departmentService.queryById(id);
}
//查询所有 @GetMapping
@GetMapping//http://127.0.0.1:8080/dept/
public List<Department> queryAll(){
return departmentService.queryAll();
}
//分页查询
@PostMapping
public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
return departmentService.queryPage(query);
}
@PatchMapping
public AjaxResult patchRemove(@RequestBody List<Long> ids){
try {
departmentService.patchRemove(ids);
return AjaxResult.me();
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.me()
.setMessage("操作失败!"+e.getMessage());
}
}
//查询部门数
@GetMapping("/deptTree")
public List<Department> getDeptTree(){
return departmentService.getDeptTree();
}
}
标签:AjaxResult,return,departmentService,public,controller,import,id 来源: https://blog.csdn.net/weixin_44054270/article/details/114159775