按数组列表自定义分页 [英] Custom pagination by array list

查看:28
本文介绍了按数组列表自定义分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用java代码创建一个数组列表的自定义分页

I want to create, in java code, a custom pagination by an array list

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort.Direction;
...    

    int page = 0;
    int count = 8;
    String sortOrder = "desc";
    String sortBy = "id";

    Sort sort = new Sort(Direction.fromString(sortOrder), sortBy);
    PageRequest pageable = new PageRequest(page, count, sort);

    List<Impianto> impiantos = myService.findMyMethod(); // returned 30 objects 
    Page<Impianto> pageImpianto = new PageImpl<Impianto>(impiantos, pageable, impiantos.size()); 

上面的脚本不会返回包含 8 个元素的页面.为什么?

The script above doesn't return a page of 8 elements. Why?

注意列表没有从 db 返回

N.B. the list didn't return from db

你能帮我吗?

推荐答案

解决了这个解决方法.

    import org.springframework.data.domain.Sort;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.Sort.Direction;
    ...    

        int page = 0;
        int count = 8;
        String sortOrder = "desc";
        String sortBy = "id";

        Sort sort = new Sort(Direction.fromString(sortOrder), sortBy);
        PageRequest pageable = new PageRequest(page, count, sort);

        List<Impianto> impiantos = myService.findMyMethod(); // returned 30 objects 
        int max = (count*(page+1)>impiantos.size())? impiantos.size(): count*(page+1);

        Page<Impianto> pageImpianto = new PageImpl<Impianto>(impiantos.subList(page*count, max), pageable, impiantos.size());  

我实现了一个新的自定义比较器类

and I implemented a new custom comparator class

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.Date;

public class MyListComparator implements Comparator<Object> {

    final String sortBy;
    final String sortOrder;

    public MyListComparator(String sortBy, String sortOrder) {
        this.sortBy = sortBy;
        this.sortOrder = sortOrder;
    }

    @Override
    public int compare(Object o1, Object o2) {

        try {
            Field field1 = o1.getClass().getDeclaredField(sortBy);
            Field field2 = o2.getClass().getDeclaredField(sortBy);

            field1.setAccessible(true); // because the fields in Impianto entity has "private" 
            field2.setAccessible(true);

            if(o1.getClass().getDeclaredField(sortBy).getType() == Long.class){
                Long d1 = (Long) field1.get(o1);
                Long d2 = (Long) field2.get(o2);
                return (sortOrder.toLowerCase().equals("asc"))? d1.compareTo(d2) : d2.compareTo(d1);

            }else if(o1.getClass().getDeclaredField(sortBy).getType() == Date.class){
                Date d1 = (Date) field1.get(o1);
                Date d2 = (Date) field2.get(o2);
                return (sortOrder.toLowerCase().equals("asc"))? d1.compareTo(d2) : d2.compareTo(d1);

            }else{
                String d1 = (String) field1.get(o1);
                String d2 = (String) field2.get(o2);
                return (sortOrder.toLowerCase().equals("asc"))? d1.compareTo(d2) : d2.compareTo(d1);

            }
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Missing variable sortBy");
        }catch (ClassCastException e) {
            throw new RuntimeException("sortBy is not found in class list");
        } catch (IllegalArgumentException e) {
            //shoud not happen
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


}

这篇关于按数组列表自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆