redis实现分页技术
Posted 小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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服务要先启动。
以上是关于redis实现分页技术的主要内容,如果未能解决你的问题,请参考以下文章