Controller:
將頁面的數據顯示以及條件搜索功能帶到同一個接口,搜索條件全部封裝在keys中,后面通過Map將數據進行轉化解析
ServiceImpl:
@Override public Map<String, Object> getByPage(String keys, int length, int start) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); List<CustomsDeclareBill> list = new ArrayList<CustomsDeclareBill>(); try { CustomsDeclareBillExample customs = new CustomsDeclareBillExample();
//查詢總條數 int total = customsDeclareBillMapper.countByExample(customs); Page page = new Page(); page.setLength(length); page.setBegin(start); customs.setPage(page);
//將搜索條件轉換成Map並且傳到數據庫中進行查詢 Map<String, Object> returnMap = (Map<String, Object>)JSONObject.fromObject(keys); returnMap.put("start", start); returnMap.put("length", length); list = customsDeclareBillMapper.getSelectByPage(returnMap);
//判斷查詢條件是否為空,如果為空,則分頁也為0,返回的數據也為空 if (list.size() == 0) { map.put("recordsTotal", 0); map.put("recordsFiltered", 0); map.put("aaData", list); } if (list != null && !list.isEmpty()) { map.put("recordsTotal", total); map.put("recordsFiltered", total); // 設置返回的日期格式 JsonConfig config = new JsonConfig(); config.setIgnoreDefaultExcludes(false); config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd")); //將list集合轉換成json數組傳到頁面 JSONArray array = JSONArray.fromObject(list, config); map.put("aaData", array); } } catch (Exception e) { throw new Exception("查詢匹配無結果,分頁異常"); } return map; }
Mapper.java:
Mapper.xml:
前端傳來的日期格式為String類型,這里要轉換格式