声明:原博客在这里,谢谢哥们提供,尊重原创。
本人是在原有的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")@Controllerpublic 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); ListnameList = 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" %>用户名称:测试
- ${n}
上一页当前第${currentPage+1}页,共${total}页下一页
如果已经执行了步骤一,那么可以直接启动整个项目,输入http://localhost:8080/redisPage/paging,就可以看到界面了。
很简单吧!
记住!本地redis服务要先启动。