SSM实现条件搜索查询

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类型,这里要转换格式

原文地址:https://www.cnblogs.com/jbml-154312/p/7640069.html