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 = currentPagenull?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服务要先启动。
redis实现分页技术的更多相关文章
- Javaweb 第15天 web练习和分页技术
第15天 web练习和分页技术 复习day14内容: 学习新技术的思路? 分析功能的思路? 使用queryRunner操作数据库的步骤? ResultSetHandler接口常用实现类(三个重点)? ...
- tp中使用分页技术
1 public function showList() { $m_ld = D ( 'guangxi_ld' ); $page = I ( 'get.p', 1 ); // 在配置中获取分页值 $p ...
- Servlet分页技术
这是看韩顺平老师的servlet视频,自己动手写的,楼主看韩顺平老师的servlet是2006制作的,用的是sql server数据库,自己又用的是oracle数据库,所以怕有的同学遇到同样的问题,不 ...
- jsp-------------之分页技术(一)
jsp分页技术之: 如下图:百度的喵 看上图中卡哇伊的小苗的爪子下面的数字,就是分页啦!那我们如何做出这样一个效果呢? 下面我们来逐一分解: jsp分页技术一 : (算法) /* int pageS ...
- 分页技术之PageDataSource类
之前给大家介绍了分页技术之Gridview控件,今天给大家介绍另外一种分页技术,采用PageDataSource类 + Repeater控件来实现. 前台只需要拖出一个Repeater控件来绑定要显示 ...
- 分页技术之GridView控件
GridView控件实现分页技术 第一步:设置GridView控件的属性,跟分页相关的属性设置如下: AllowPaging="true":允许分页, PageSize=" ...
- redis预切片技术,实现
平时,我们将创建多个redis实例,以缓解单redis压力范例.但,作为高速缓存的数量增加.对redis对于扩展是一种非 - 不要无所谓.对redis有几种方法用于扩张可能.让我们添加每个redis最 ...
- 关于Ajax无刷新分页技术的一些研究 c#
关于Ajax无刷新分页技术的一些研究 c# 小弟新手,求大神有更好的解决方案,指教下~ 以前做项目,用过GridView的刷新分页,也用过EasyUI的封装好的分页技术,最近在老项目的基础上加新功能, ...
- 浅谈Java分页技术
话不多言.我们要实现java分页技术,我们首先就需要定义四个变量,他们是: int pageSize;//每页显示多少条记录 int pageNow;//希望现实第几页 int pageCount; ...
随机推荐
- 虚函数的特点就是执行的时候会下降到子类去执行同名覆盖函数 good
var t: TBitBtn;begin t:=TBitBtn.Create(nil); t.Name:='BitBtn100'; t.parent :=Self; // 这里下断点end; 一路跟踪 ...
- Harry Potter and the Order of the Phoenix
书名:Harry Potter and the Order of the Phoenix 作者:J.K. Rowling 篇幅: 870P 蓝思值:950L 用时: 22天 工具: 有道词典 [透析成 ...
- oc63--协议@protocol1
// // SportProtocol.h // day17 #import <Foundation/Foundation.h> @protocol SportProtocol <N ...
- hdoj--5612--Baby Ming and Matrix games(dfs)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 软件-版本控制:VCS(版本控制系统)
ylbtech-软件-版本控制:VCS(版本控制系统) 版本控制系统(Version Control System),是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统 ...
- Gold Balanced Lineup(hash)
http://poj.org/problem?id=3274 ***** #include <stdio.h> #include <iostream> #include < ...
- Windows:Word,PPT,EXCEL com+组件配置
本文所涉及到配置前提: 服务器必须安装Office套件(Word,PPT,Excel) 第一部分 Word Com+组件权限配置 1.cmd模式输入dcomcnfg 2.找到Microsoft Wor ...
- 5.23Java各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分
作者:https://www.cnblogs.com/lyjin/p/6389349.html PO:持久对象(persistent object):---po就是在Object/Relation M ...
- python--6、re模块
re模块 用于在正则表达式匹配操作. python中为了避免实现输出'\','\n'字符的转义问题(如正则表达式使用反斜杠" \ "来代表特殊形式或用作转义字符,这里跟Python ...
- AndroidStudio怎样导入library项目库
先打开一个Project,然后将libraryr的项目作为module进行导入: File菜单->import module菜单 以上只是导入进来,还没有作为与project真正有效的一部分.需 ...