加上这四种主解就已经代表你要做的是什么了,使用注解来区分;还有就是url中规范的是不能传动词 @GetMapping("/companies"):查询功能 @PostMapping("/companies"):提交增加 @PutMapping("/companies/{id}"):修改 @DeleteMapping("/companies/{id}"):删除
还有就是url中规范的是不能传动词,否则就偏离了restful风格,动词是通过url表明的,没必要再开启PUT、DELETE方法,而且对于大多数开发(包括前端和后端)来说,他们只知道GET、POST,其他HTTP方法也不太清楚怎么处理 例如: @PostMapping("/companies/add"):
这样你的路径可以写些重复的路径,但是会自动找到相关的方法来办事。
查询
/** * 查询 */ @GetMapping("/companies") public ResponseEntity findAll(){ List<Company> companies = companyService.findAll(); Page page = new Page<>(); page.setCurrent(1); page.setSize(3); return ResponseEntity.ok(companies); }添加
/** * 添加 */ @PostMapping("/companies") public ResponseEntity createCompany(@RequestBody Company company){ companyService.createCompany(company); return ResponseEntity.status(201).build(); }修改
/** * 修改 */ @PutMapping("/companies/{id}") public ResponseEntity updateCompany(@PathVariable Long id,@RequestBody Company company){ company.setId(id); companyService.updateCompany(company); return ResponseEntity.status(204).build(); }删除
/** * 删除 */ @DeleteMapping("/companies/{id}") public ResponseEntity delete(@PathVariable Long id){ companyService.delete(id); return ResponseEntity.status(204).build(); }也可以用pageHelper插件,根据业务需求来选择。 controller层
/** * 分页查询 */ public ResponseEntity search(String name,Page page){ IPage<Company> search = companyService.search(name, page); return ResponseEntity.ok(search); }server层
/** *分页查询 */ public IPage<Company> search(String name, Page page){ QueryWrapper<Company> queryWrapper = new QueryWrapper(); if(!StringUtils.isEmpty("name")){ queryWrapper.like("name", name); } return companyMapper.selectPage(page, queryWrapper); }dao层使用mybaitsplus
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.yhhl.bdp.entity.Company; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Mapper @Repository public interface CompanyMapper extends BaseMapper<Company> { }pageRequest封装的类
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public class PageRequest extends Page { private final int DEFAULT_PAGE = 1; private final int DEFAULT_SIZE = 10; public void setPage(Integer page) { if (page == null || page <= 1) { page = DEFAULT_PAGE; } super.setCurrent(page); } public void setSize(Integer size) { if (size == null || size <= 1) { size = DEFAULT_SIZE; } super.setSize(size); } }这样子实现分页