Spring Boot項目中怎么維護全局json數據,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創新互聯公司是專業的上街網站建設公司,上街接單;提供成都網站設計、做網站,網頁設計,網站設計,建網站,PHP網站建設等專業做網站服務;采用PHP框架,可快速的進行上街網站開發網頁制作和功能擴展;專業做搜索引擎喜愛的網站,專業的做網站團隊,希望更多企業前來合作!
工具類.
public class JsonMsg { private int code; private String msg; private Map<String, Object> extendInfo = new HashMap<>(); public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getExtendInfo() { return extendInfo; } public void setExtendInfo(Map<String, Object> extendInfo) { this.extendInfo = extendInfo; } public static JsonMsg success(){ JsonMsg res = new JsonMsg(); res.setCode(100); res.setMsg("操作成功!"); return res; } public static JsonMsg fail(){ JsonMsg res = new JsonMsg(); res.setCode(200); res.setMsg("操作失敗!"); return res; } public JsonMsg addInfo(String key, Object obj){ this.extendInfo.put(key, obj); return this; } }
下面我給出具體的使用示例:
就是一個普通的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<Department> departments = departmentService.getDeptList(offset, limit); mv.addObject("departments", departments) .addObject("totalItems", totalItems) .addObject("totalPages", totalPages) .addObject("curPageNo", pageNo); return mv; } /** * 查詢所有部門名稱 * @return */ @RequestMapping(value = "/getDeptName", method = RequestMethod.GET) @ResponseBody public JsonMsg getDeptName(){ List<Department> departmentList = departmentService.getDeptName(); if (departmentList != null){ return JsonMsg.success().addInfo("departmentList", departmentList); } return JsonMsg.fail(); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創新互聯行業資訊頻道,感謝您對創新互聯的支持。
當前題目:SpringBoot項目中怎么維護全局json數據
標題鏈接:http://vcdvsql.cn/article10/iijggo.html
成都網站建設公司_創新互聯,為您提供動態網站、小程序開發、網站改版、品牌網站設計、微信小程序、品牌網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯