SpringBoot高度封裝增刪改查介面
阿新 • • 發佈:2021-12-21
封裝了一個科室管理的增刪改查介面:
科室列表、科室詳情、新增科室、修改科室、刪除科室
service層:
package com.yutangzongcai.demo.service; import com.alibaba.fastjson.JSONObject; import com.yutangzongcai.demo.entity.DepartmentEntity; import com.yutangzongcai.demo.mapper.DepartmentMapper; import com.yutangzongcai.demo.tool.Page; import com.yutangzongcai.demo.tool.Res;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("department") public class DepartmentService { @Autowired private DepartmentMapper departmentMapper; @PostMapping("list") publicJSONObject list(@RequestBody JSONObject jsonObject) { return Res.list(departmentMapper.count(jsonObject), departmentMapper.list(Page.service(jsonObject))); } @PostMapping("detail") @Transactional(rollbackFor = Exception.class) public JSONObject detail(@Validated(value = {DepartmentEntity.Detail.class}) @RequestBody DepartmentEntity departmentEntity) { return Res.detail(departmentMapper.detail((JSONObject) JSONObject.toJSON(departmentEntity))); } @PostMapping("create") @Transactional(rollbackFor = Exception.class) public JSONObject create(@Validated(value = {DepartmentEntity.Create.class}) @RequestBody DepartmentEntity departmentEntity) { return Res.row(departmentMapper.create((JSONObject) JSONObject.toJSON(departmentEntity)), "新增"); } @PostMapping("update") @Transactional(rollbackFor = Exception.class) public JSONObject update(@Validated(value = {DepartmentEntity.Update.class}) @RequestBody DepartmentEntity departmentEntity) { return Res.row(departmentMapper.update((JSONObject) JSONObject.toJSON(departmentEntity)), "修改"); } @PostMapping("delete") @Transactional(rollbackFor = Exception.class) public JSONObject delete(@Validated(value = {DepartmentEntity.Detail.class}) @RequestBody DepartmentEntity departmentEntity) { return Res.row(departmentMapper.delete((JSONObject) JSONObject.toJSON(departmentEntity)), "刪除"); } }
dto層、entity層、mapper層、可根據需求自行定義