温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpingBoot-Thymeleaf-bootstrapTable-分页之Service

发布时间:2020-07-12 01:47:58 来源:网络 阅读:1413 作者:douruanliang 栏目:开发技术

JAVA:

    public Map getPage(final Map searchParameters, String salt) {
            Map map = new HashMap();
            int page = 0;
            int pageSize = 10;
            Page<User> pageList;
            if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("page") != null) {
                page = Integer.parseInt(searchParameters.get("page").toString()) - 1;
            }
            if (searchParameters != null && searchParameters.size() > 0 && searchParameters.get("pageSize") != null) {
                pageSize = Integer.parseInt(searchParameters.get("pageSize").toString());
            }
            if (pageSize < 1)
                pageSize = 1;
            if (pageSize > 100)
                pageSize = 100;
            List<Map> orderMaps = (List<Map>) searchParameters.get("sort");
            List<Order> orders = new ArrayList<Order>();
            if (orderMaps != null) {
                for (Map m : orderMaps) {
                    if (m.get("field") == null)
                        continue;
                    String field = m.get("field").toString();
                    if (!StringUtils.isEmpty(field)) {
                        String dir = m.get("dir").toString();
                        if ("DESC".equalsIgnoreCase(dir)) {
                            orders.add(new Order(Direction.DESC, field));
                        } else {
                            orders.add(new Order(Direction.ASC, field));
                        }
                    }
                }
            }
            PageRequest pageable;
            if (orders.size() > 0) {
                pageable = new PageRequest(page, pageSize, new Sort(orders));
            } else {
                Sort s = new Sort(Direction.ASC, "userIndex");
                pageable = new PageRequest(page, pageSize, s);
            }
            Map filter = (Map) searchParameters.get("filter");
            if (filter != null) {
                final List<Map> filters = (List<Map>) filter.get("filters");
                Specification<User> spec = new Specification<User>() {
                    @Override
                    public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                        List<Predicate> pl = new ArrayList<Predicate>();
                        for (Map f : filters) {
                            String field = f.get("field").toString().trim();
                            String value = f.get("value").toString().trim();
                            if (value != null && value.length() > 0) {
                                if ("loginName".equalsIgnoreCase(field)) {
                                    pl.add(cb.equal(root.<String>get(field), value));
                                } else if ("userName".equalsIgnoreCase(field)) {
                                    pl.add(cb.like(root.<String>get(field), value + "%"));
                                } else if ("email".equalsIgnoreCase(field)) {
                                    pl.add(cb.like(root.<String>get(field), value + "%"));
                                } else if ("state".equalsIgnoreCase(field)) {
                                    pl.add(cb.equal(root.<Integer>get("state"), value));
                                }
                            }
                        }
                        // 查询出未删除的
                        pl.add(cb.equal(root.<Integer>get("flag"), 1));
                        // pl.add(cb.equal(root.<Integer>get("state"), 1));
                        return cb.and(pl.toArray(new Predicate[0]));
                    }
                };
                pageList = userRepository.findAll(spec, pageable);

            } else {
                Specification<User> spec = new Specification<User>() {
                    public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                        List<Predicate> list = new ArrayList<Predicate>();
                        // 查询出未删除的
                        list.add(cb.equal(root.<Integer>get("flag"), 1));
                        list.add(cb.equal(root.<Integer>get("state"), 1));
                        return cb.and(list.toArray(new Predicate[0]));
                    }
                };
                pageList = userRepository.findAll(spec, pageable);

            }

            map.put("total", pageList.getTotalElements());
            map.put("users", userList2DtoList(pageList.getContent(), salt));
            return map;
        }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI