PageHelper查询分页
页面加载,请求ajax,查询结果分页显示(解析数据和页码)。增加模糊条件查询,同样也要分页显示。
jar包:
applicationContext-mybatis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 开启注解扫描 --><context:annotation-config /><context:component-scan base-package="cn.com.how"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 加载数据库配置文件 --><context:property-placeholder file-encoding="utf-8" location="classpath:config/db.properties"/><!-- 连接数据库的bean --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${db.driver}" /><property name="url" value="${db.url}"/><property name="username" value="${db.uname}"/><property name="password" value="${db.pwd}"/></bean><!-- 会话工厂 --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><!--属性1: 添加别名 --><property name="typeAliasesPackage" value="cn.com.how.pojo" /><!--属性2:注入数据库连接对象 --><property name="dataSource" ref="dataSource"/><!--属性3:pojo类的xml的配置文件 --><property name="mapperLocations" value="classpath:cn/com/how/mapper/*.xml"/><!-- pageHelper配置 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!--使用下面的方式配置参数,一行配置一个 --><value></value></property></bean></array></property> </bean><!-- 扫描mapper类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.com.how.mapper"/></bean></beans>
<!-- pageHelper配置 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!--使用下面的方式配置参数,一行配置一个 --><value></value></property></bean></array></property> </bean><!-- 扫描mapper类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.com.how.mapper"/></bean></beans>
list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%pageContext.setAttribute("ctx", request.getContextPath());
%>
<title>用户管理</title>
<script type="text/javascript" src="${ctx}/js/jquery.min.js"></script>
<script type="text/javascript">$(function(){to_page(1);})//查询信息function to_page(pn){$.ajax({url:"${ctx}/ssm/listWithJson",type:"post",data:"pn="+pn,dataType:"json",success:function(result){build_emps_table(result);build_page_info(result);} ,});}//解析userfunction build_emps_table(result){$("#mytable tbody").empty();$.each(result.extend.pageInfo.list,function(index,current){var xuhao = $("<td></td>").append(index+1);var username = $("<td></td>").append(current.username);var userpwd = $("<td></td>").append(current.userpwd);var usersex =$("<td></td>").append(current.usersex=='1'?"男":"女");var userage =$("<td></td>").append(current.userage);$("<tr></tr>").append(xuhao).append(username).append(userpwd).append(usersex).append(userage).appendTo("#mytable tbody");})}//解析分页function build_page_info(result){$("#split").empty();var pageNum = result.extend.pageInfo.pageNum;var pages = result.extend.pageInfo.pages;var total = result.extend.pageInfo.total;$("#split").append("当前第"+pageNum+"页,共"+pages+"页,总"+total+"条数据").append("<br/>");var firstPageLi = $("<a></a>").append("首页");var prePageLi = $("<a></a>").append("上一页");if(result.extend.pageInfo.hasPreviousPage == false){firstPageLi.addClass("disabled");prePageLi.addClass("disabled");}else{//为元素添加点击翻页的事件firstPageLi.click(function(){to_page(1);});prePageLi.click(function(){to_page(result.extend.pageInfo.pageNum -1);});}var nextPageLi = $("<a></a>").append("下一页");var lastPageLi = $("<a></a>").append("末页");if(result.extend.pageInfo.hasNextPage == false){nextPageLi.addClass("disabled");lastPageLi.addClass("disabled");}else{nextPageLi.click(function(){to_page(result.extend.pageInfo.pageNum +1);});lastPageLi.click(function(){to_page(result.extend.pageInfo.pages);});}$("#split").append(firstPageLi).append(prePageLi).append(nextPageLi).append(lastPageLi);}function search(pn){var str = $("#search").serialize();$.ajax({url:"${ctx}/ssm/search",type:"post",data:"pn="+pn+"&"+str,success:function(result){//alert(1);build_emps_table2(result);build_page_info2(result);},});}function build_emps_table2(result){$("#mytable tbody").empty();$.each(result.extend.pageInfo.list,function(index,current){var xuhao = $("<td></td>").append(index+1);var username = $("<td></td>").append(current.username);var userpwd = $("<td></td>").append(current.userpwd);var usersex =$("<td></td>").append(current.usersex=='1'?"男":"女");var userage =$("<td></td>").append(current.userage);$("<tr></tr>").append(xuhao).append(username).append(userpwd).append(usersex).append(userage).appendTo("#mytable tbody");});}function build_page_info2(result){$("#split").empty();var pageNum = result.extend.pageInfo.pageNum;var pages = result.extend.pageInfo.pages;var total = result.extend.pageInfo.total;$("#split").append("当前第"+pageNum+"页,共"+pages+"页,总"+total+"条数据").append("<br/>");var firstPageLi = $("<a></a>").append("首页");var prePageLi = $("<a></a>").append("上一页");if(result.extend.pageInfo.hasPreviousPage == false){firstPageLi.addClass("disabled");prePageLi.addClass("disabled");}else{//为元素添加点击翻页的事件firstPageLi.click(function(){search(1);});prePageLi.click(function(){search(result.extend.pageInfo.pageNum -1);});}var nextPageLi = $("<a></a>").append("下一页");var lastPageLi = $("<a></a>").append("末页");if(result.extend.pageInfo.hasNextPage == false){nextPageLi.addClass("disabled");lastPageLi.addClass("disabled");}else{nextPageLi.click(function(){search(result.extend.pageInfo.pageNum +1);});lastPageLi.click(function(){search(result.extend.pageInfo.pages);});}$("#split").append(firstPageLi).append(prePageLi).append(nextPageLi).append(lastPageLi);}</script>
</head>
<body>
<div style="width:500px;margin:0px auto;text-align:center"><form id="search" > 账号:<input type="text" name="username" placeholder="请输入字段"><br>密码:<input type="text" name="userpwd" placeholder="请输入字段"><br><input type="button" value="搜索" onclick="search(1)"></form><table align='center' border='1' cellspacing='0' id="mytable"><thead><tr><!-- <td>USERID</td> --><td>序号</td><td>账号</td><td>密码</td><td>性别</td><td>年龄</td></tr></thead><tbody></tbody></table><div style="text-align:center" id="split"></div></div>
</body>
</html>
ajax返回的通用bean:
public class Msg {// 状态码 100-成功 200-失败private int code;// 提示信息private String msg;//用户返回给浏览器的数据private Map<String,Object> extend = new HashMap<String, Object>();//处理成功public static Msg success() {Msg result = new Msg();result.setCode(100);result.setMsg("处理成功!");return result; }//处理失败public static Msg fail() {Msg result =new Msg();result.setCode(200);result.setMsg("处理失败");return result;}//链式操作public Msg add(String key,Object value) {this.getExtend().put(key, value);return this;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Map<String, Object> getExtend() {return extend;}public void setExtend(Map<String, Object> extend) {this.extend = extend;}
}
controller方法:
@RequestMapping("/listWithJson")@ResponseBodypublic Msg listWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn) {PageHelper.startPage(pn,2);List<User> list = userService.list();PageInfo pageInfo = new PageInfo(list);return Msg.success().add("pageInfo", pageInfo);}@RequestMapping("/search")@ResponseBodypublic Msg search(@RequestParam(value="pn",defaultValue="1") Integer pn,User user) {PageHelper.startPage(pn, 2);List<User> list = userService.searchUser(user);System.out.println("有"+list+"个");PageInfo pageInfo = new PageInfo(list);return Msg.success().add("pageInfo", pageInfo);}
返回到页面:
@RequestMapping("/userList3")public String userList3(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) {PageHelper.startPage(pn, 2);List<User> list = userService.list();PageInfo page = new PageInfo(list);model.addAttribute("pageInfo", page);return "userList";}
PageInfo page = new PageInfo(emps,5);分页条
//解析显示分页条,点击分页要能去下一页....function build_page_nav(result){//page_nav_area$("#page_nav_area").empty();var ul = $("<ul></ul>").addClass("pagination");//构建元素var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href","#"));var prePageLi = $("<li></li>").append($("<a></a>").append("«"));if(result.extend.pageInfo.hasPreviousPage == false){firstPageLi.addClass("disabled");prePageLi.addClass("disabled");}else{//为元素添加点击翻页的事件firstPageLi.click(function(){to_page(1);});prePageLi.click(function(){to_page(result.extend.pageInfo.pageNum -1);});}var nextPageLi = $("<li></li>").append($("<a></a>").append("»"));var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href","#"));if(result.extend.pageInfo.hasNextPage == false){nextPageLi.addClass("disabled");lastPageLi.addClass("disabled");}else{nextPageLi.click(function(){to_page(result.extend.pageInfo.pageNum +1);});lastPageLi.click(function(){to_page(result.extend.pageInfo.pages);});}//添加首页和前一页 的提示ul.append(firstPageLi).append(prePageLi);//1,2,3遍历给ul中添加页码提示$.each(result.extend.pageInfo.navigatepageNums,function(index,item){var numLi = $("<li></li>").append($("<a></a>").append(item));if(result.extend.pageInfo.pageNum == item){numLi.addClass("active");}numLi.click(function(){to_page(item);});ul.append(numLi);});//添加下一页和末页 的提示ul.append(nextPageLi).append(lastPageLi);//把ul加入到navvar navEle = $("<nav></nav>").append(ul);navEle.appendTo("#page_nav_area");}
<select id="searchUser" resultType="user" parameterType="user">select * from users<where><if test="username!=null and username!=''">and username like concat('%',#{username},'%')</if><if test="userpwd!=null and userpwd!=''">and userpwd like concat('%',#{userpwd},'%')</if></where></select>
PageHelper查询分页相关推荐
- PageHelper 关闭COUNT(0)查询 以及PageHelper 的分页原理分析
pagehelper 关闭count(0)查询 以及pagehelper的分页原理分析 情景再现:在给移动端提供分页查询数据接口时,知道他们不需要总条数.但是使用pagehelper 分页查询打印的s ...
- Springboot Mybatis使用PageHelper实现分页查询
以下介绍实战中数据库框架使用的是mybatis,对整合mybatis此处不做介绍. 使用pageHelper实现分页查询其实非常简单,共两步: 一.导入依赖: pom.xml添加依赖: <!-- ...
- 使用PageHelper实现分页查询(详细)
使用PageHelper实现分页查询(详细): 实现环境: 语言 编程工具 框架 分页插件 前端 前端数据获取 Java IDEA SpringBoot pageHelper BootStrap Th ...
- PageHelper实现分页查询
文章目录 前言 一.PageHelper实现分页查询 二.PageHelper的基本使用 1. 先编写持久层 2.编写业务逻辑层 3..编写控制层 4.使用JsonPage返回结果 总结 前言 分页查 ...
- Springboot Mybatis使用pageHelper实现分页查询
以下介绍实战中数据库框架使用的是mybatis,对整合mybatis此处不做介绍. 使用pageHelper实现分页查询其实非常简单,共两步: 一.导入依赖: 二.添加配置: 那么开始, 第一步: p ...
- mybatis pagehelper实现分页
jar包的版本一定要对应,不然会出现一系列的问题 下载jar包 <properties> <!-- spring版本号 --> < ...
- springboot使用PageHelper实现分页
使用mybatis最头疼的就是写分页,需要先写一个查询count的select语句,在写一个真正的limit查询语句,所以花费很长的时间,这里咋们可以使用PageHelper实现分页. 1.首先引入p ...
- pagehelper 不分页的解决方法
pagehelper 不分页的解 pagehelper PageHelper.startPage(1, 10);只对该语句以后的第一个查询语句得到的数据进行分页, 就算你在PageInfo pa = ...
- springboot+mybatis 利用PageHelper插件分页,结果第二页的返回分页信息还是和第一页一样。
正常使用PageHelper来分页时可以的,但是如果在查询list后做了非常多的处理,即解包在装包操作.可能最后返回时分页的数据查询的对,但是分页信息就有问题了.有的甚至分页功能都不行.这里为避免几个 ...
最新文章
- python中文意思k-【Python】 汉字转化汉语拼音pinyin
- idea 升级到2020后 无法启动_i.MXRT软复位后无法从32MB Flash启动?
- 信息学奥赛一本通 1962:【13NOIP普及组】表达式求值 | 洛谷 P1981 [NOIP2013 普及组] 表达式求值
- 【华为云技术分享】ARMv8-A存储模型概述(2)
- java游戏鬼吹灯安卓版下载_鬼吹灯手机游戏下载
- 免费车型车系品牌api
- 毕业设计答辩PPT模板
- 北大的戴威,为何输给了三本的胡玮炜?
- Vi IMproved
- vue 实现html转图片和生成二维码
- c语言中对float保留固定3位,float保留三位小数 float,double 除法 保留 指定位
- LeetCode 13 罗马符号转化为数字(难度: Easy)
- RISC-V向量扩展指令(一)
- 网站服务器防火墙waf介绍
- 64匹马8个跑道需要多少轮才能选出最快的四匹
- OCJP题库1Z0-851(21/30)
- 基于SSH开发网上零食销售系统的设计与实现
- vue中引入jquery方法 或 $ is not defined错误解决方法
- 关于HC-SR501人体红外感应模块的光敏电阻取值到底为多少的终极研究
- 为什么这么努力还这么贫穷?
热门文章
- 微信可以识别哪些HTML语言,h5原本是一种制作万维网页面的标准计算机语言,由HTML5简化而来的词汇,HTML5的设计目的是为了在移动设备上支持多媒体。由于微信迅速的崛起,h5语言编写的界...
- linux for循环 | while循环 | until 循环 | 超详细
- 【阿里云生活物联网架构师专题 ④】分享可商用的ESP8266 SDK连接阿里云物联网生活平台的在线远程升级OTA笔记。
- 6. 弹性形变与胡克定律
- ouc-exp6-blog
- 字符串 leetcode 总结
- Spark 调研报告
- 视觉中国签约插画图库上传失败 图片显示“缺少分辨率信息”
- 关于javaScript
- 通过Spring来读取文件的各种方法