企业在数字化转型中如何利用常用前端框架提高开发效率并确保安全合规?
758
2022-09-01
Spring Boot 项目维护全局json数据
1:概述
过去 我们在每一个方法中处理前端发过来的请求,需要自己构造请求数据,然后通过spring 提供的@ResponseBody 强制转为JSON数据吗,实际上出现了很多重复的代码,我么亦可以通过构造一个
工具类,实现只关注需要改变的数据. 下面给出这个工具类.
public class JsonMsg { private int code; private String msg; private Map
下面我给出具体的使用示例:
就是一个普通的CRUD代码
package com.hrms.controller;import com.hrms.bean.Department;import com.hrms.service.DepartmentService;import com.hrms.util.JsonMsg;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import org.springframework.web.servlet.ModelAndView;import java.util.List;/** * @author GenshenWang.nomico * @date 2018/3/8. */@Controller@RequestMapping(value = "/hrms/dept")public class DepartmentController { @Autowired DepartmentService departmentService; /** * 删除 * @param deptId * @return */ @RequestMapping(value = "/delDept/{deptId}", method = RequestMethod.DELETE) @ResponseBody public JsonMsg deleteDept(@PathVariable("deptId") Integer deptId){ int res = 0; if (deptId > 0){ res = departmentService.deleteDeptById(deptId); } if (res != 1){ return JsonMsg.fail().addInfo("del_dept_error", "删除异常"); } return JsonMsg.success(); } /** * 部门更改 * @param deptId * @param department * @return */ @RequestMapping(value = "/updateDept/{deptId}", method = RequestMethod.PUT) @ResponseBody public JsonMsg updateDeptById(@PathVariable("deptId") Integer deptId, Department department){ int res = 0; if (deptId > 0){ res = departmentService.updateDeptById(deptId, department); } if (res != 1){ return JsonMsg.fail().addInfo("update_dept_error", "部门更新失败"); } return JsonMsg.success(); } /** * 新增部门 * @param department * @return */ @RequestMapping(value = "/addDept", method = RequestMethod.PUT) @ResponseBody public JsonMsg addDept(Department department){ int res = departmentService.addDept(department); if (res != 1){ return JsonMsg.fail().addInfo("add_dept_error", "添加异常!"); } return JsonMsg.success(); } /** * 查询部门信息总页码数 * @return */ @RequestMapping(value = "/getTotalPages", method = RequestMethod.GET) @ResponseBody public JsonMsg getTotalPages(){ //每页显示的记录行数 int limit = 5; //总记录数 int totalItems = departmentService.getDeptCount(); int temp = totalItems / limit; int totalPages = (totalItems % limit== 0) ? temp : temp+1; return JsonMsg.success().addInfo("totalPages", totalPages); } /** * */ @RequestMapping(value = "/getDeptById/{deptId}", method = RequestMethod.GET) @ResponseBody public JsonMsg getDeptById(@PathVariable("deptId") Integer deptId){ Department department = null; if (deptId > 0){ department = departmentService.getDeptById(deptId); } if (department != null){ return JsonMsg.success().addInfo("department", department); } return JsonMsg.fail().addInfo("get_dept_error", "无部门信息"); } /** * 分页查询:返回指定页数对应的数据 * @param pageNo * @return */ @RequestMapping(value = "/getDeptList", method = RequestMethod.GET) public ModelAndView getDeptList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo){ ModelAndView mv = new ModelAndView("departmentPage"); //每页显示的记录行数 int limit = 5; //总记录数 int totalItems = departmentService.getDeptCount(); int temp = totalItems / limit; int totalPages = (totalItems % limit== 0) ? temp : temp+1; //每页的起始行(offset+1)数据,如第一页(offset=0,从第1(offset+1)行数据开始) int offset = (pageNo - 1)*limit; List
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~