redis實現分頁技術


聲明:原博客在這里https://www.cnblogs.com/find-the-right-direction/p/8465011.html,謝謝哥們提供,尊重原創。

本人是在原有的springboot2.0項目中實現,其中Jedis jar包可以在這里下載,當然你也可以在pom.xml中添加 spring-boot-starter-data-redis

1、先在redis中插入數據,所以新建一個RedisUtil.java

package com.cn.commodity.utils;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisUtil {
    @Test
    public void testJedisPool1(){
        Jedis jedis = new Jedis("localhost",6379);
        try {
            for (int i = 1; i <= 100000; i++) {
                jedis.rpush("nameList","zl"+i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }
}

 

2、新建PagingController.java

package com.cn.commodity.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;

import javax.sound.midi.Soundbank;
import java.util.List;

@RequestMapping("/redisPage")
@Controller
public class PagingController {
    @RequestMapping("/paging")
    public String paging(Model model, Long currentPage){
        //create a simple and not-safe pool
        Jedis jedis = new Jedis("localhost",6379);
        try {
            //total
            long total = jedis.llen("nameList");
            //size
            long size = 10L;
            if (total/size==0){
                total = total/size;
            }else {
                total = total/size + 1;
            }
            // set currentPage
            currentPage = currentPage==null?0L:currentPage;
            System.out.println(total);
            List<String> nameList = jedis.lrange("nameList",currentPage*size,(currentPage+1)*size);
            model.addAttribute("nameList",nameList);
            model.addAttribute("total",total);
            model.addAttribute("currentPage",currentPage);
            for (String name : nameList) {
                System.out.println(name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (jedis != null){
                    jedis.close();
                }
            }catch (JedisException e){
                e.printStackTrace();
            }
        }
        return "redisPaging";
    }
}

 

3、寫一個redisPaging.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>測試</title>
</head>
<style>
    ul{
        list-style: none;
        float: left;
    }
    li{
        width: 50px;
        height: 50px;
    }
</style>
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/jquery.min.js"></script>
<body>
<form action="${pageContext.request.contextPath}/milu/paging">
    按頁數查詢:<input class="pageNum" name="currentPage" maxlength="10" value="輸入要查詢的頁數">
    <input type="submit" value="查詢"><br><hr>
</form>
<strong>用戶名稱:</strong><br><hr>
<ul>
    <c:forEach items="${nameList}" var="n">
        <li>${n}</li>
    </c:forEach>
</ul>
<br><hr>
<a href="${pageContext.request.contextPath}/milu/paging?currentPage=${currentPage-1}">上一頁</a>
當前第${currentPage+1}頁,共${total}頁
<a href="${pageContext.request.contextPath}/milu/paging?currentPage=${currentPage+1}">下一頁</a>
</body>
</html>

如果已經執行了步驟一,那么可以直接啟動整個項目,輸入http://localhost:8080/redisPage/paging,就可以看到界面了。

很簡單吧!

記住!本地redis服務要先啟動。

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com