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/size0){
    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",currentPagesize,(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服务要先启动。

  • 相关阅读:
    [原创]项目管理知识体系指南之 10项目沟通管理思维导图
    [原创]项目管理知识体系指南之 9项目人力资源管理思维导图
    [原创]项目管理知识体系指南之 5范围管理思维导图
    [原创]项目管理知识体系指南之 6项目时间管理思维导图
    [原创]2013年测试人员薪水分布图
    [原创]项目管理知识体系指南之 11项目风险管理维导图
    [原创]项目管理知识体系指南之 8项目质量管理思维导图
    [原创]2013年上半年测试技术学习文档任务
    [原创]Linux下网络性能测试Netperf工具介绍及安装
    [原创]数据库安全思维导图
  • 原文地址:https://www.cnblogs.com/jpfss/p/10836806.html
Copyright © 2011-2022 走看看