zoukankan      html  css  js  c++  java
  • 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服务要先启动。

  • 相关阅读:
    [找程序员代写推荐]jbpm4.4项目测试源码下载,效果图
    [原]强大的JQuery
    [找程序员代写推荐]SSH+jquery+springScurity权限管理+jasperreport报表+webService调用天气预报+完整分页 整合小型OA项目源码下载
    [找程序员代写推荐]Exception in thread &quot;http-apr-8080-exec-6&quot; java.lang.OutOfMemoryError: PermGen space 解决!
    [原]12-13年学习总结——路上的风景很美
    [找程序员代写推荐]jquery 弹出登陆框,简单易懂!修改密码效果代码
    [找程序员代写推荐]php和apache2的配置
    [找程序员代写推荐]myBatis 基础测试 表关联关系配置 集合 测试
    [找程序员代写推荐]myBatis 基础测试 增 删 改 查 用过hibrenate 之后,感觉很好理解
    JVM运行时数据区域
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10024816.html
Copyright © 2011-2022 走看看