溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpingBoot-Thymeleaf-bootstrapTable-分頁之Service

發(fā)布時間:2020-07-12 01:47:58 來源:網(wǎng)絡(luò) 閱讀:1413 作者:douruanliang 欄目:開發(fā)技術(shù)

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問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI