1. 程式人生 > >SpingBoot-Thymeleaf-bootstrapTable-分頁之Service

SpingBoot-Thymeleaf-bootstrapTable-分頁之Service

springboot

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;
        }

SpingBoot-Thymeleaf-bootstrapTable-分頁之Service