java redis 分页查询数据

package com.liying.tiger.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import com.liying.monkey.core.util.JavaJsonConvert;
import com.liying.monkey.dao.pagination.PageRequestWrapper;
import com.liying.monkey.service.api.RedisService;

public class RedisServiceTest {
    private static ApplicationContext context = null;

    public static void main(String[] args) {
        context = new ClassPathXmlApplicationContext("spring-monkey.xml", "spring-redis-cluster.xml");
        RedisService redisService = context.getBean("redisServiceImpl", RedisService.class);

        int pageNum = 2; //当前页面
        int pageSize = 5; //每页大小
        int start = pageSize * (pageNum - 1); // 因为redis中list元素位置基数是0
        int end = start + pageSize - 1;
        
        long total = redisService.listSize("userList");
        List<String> dataList = redisService.range("userList", start, end);
        Page page = new PageImpl(dataList, new PageRequestWrapper(pageNum, pageSize), total);
        
        System.out.println(JavaJsonConvert.java2Json(page));
    }
}

 

posted on 2017-09-30 16:19  Ruthless  阅读(11610)  评论(1编辑  收藏  举报