[置顶]mybatis分页插件实现分页...
1.了解过程:在数据库服务器中,sql语句实现分页便要每个查询语句都要写上limit(开始,结束),并且不能灵活的随前端变化,为此使用拦截器的方法,过程:拦截器拦截请求的sql语句(根据需要拦截的ID(正则匹配),进行拦截),并对根据前端传过来的页数,和每页的条数,计算出limit(开始,结束),总条数,然后,拼接到sql语句后边。其中这个处理过程,已经封装到了,分页插件中,可以不用理解,直接使用。
条件:maven下:
1.pom.xml中导入依赖:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.4</version>
</dependency>
2.在mybatis-conf.xml中添加拦截器:
<plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/></plugin></plugins>
3.编写page实例:
package com.tc.tccp.core.page;/** * @author wangpei * @version *创建时间:2016年10月12日 下午10:52:29 * 拦截器实现mybatis的拦截*/
import java.util.List;public class PageParameter {public static final int DEFAULT_PAGE_SIZE = 10;private int pageSize;// 页面大小private int currentPage;// 当前页的位置private int prePage;// 上一页private int nextPage;// 下一页private int totalPage;// 总页数private int totalCount;// 总条数/*** 当前页的数据*/private List<?> list;/*** 获得分页内容** @return*/public List<?> getList() {return list;}public PageParameter(int pageSize, int currentPage, int prePage,int nextPage, int totalPage, int totalCount, List<?> list) {this.pageSize = pageSize;this.currentPage = currentPage;this.prePage = prePage;this.nextPage = nextPage;this.totalPage = totalPage;this.totalCount = totalCount;this.list = list;}/*** 设置分页内容** @param list*/@SuppressWarnings("unchecked")public void setList(List list) {this.list = list;}public PageParameter() {this.currentPage = 1;this.pageSize = DEFAULT_PAGE_SIZE;}/**** @param currentPage* @param pageSize*/public PageParameter(int currentPage, int pageSize) {this.currentPage = currentPage;this.pageSize = pageSize;}public int getCurrentPage() {return currentPage;}public void setCurrentPage(int currentPage) {this.currentPage = currentPage;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPrePage() {return prePage;}public void setPrePage(int prePage) {this.prePage = prePage;}public int getNextPage() {return nextPage;}public void setNextPage(int nextPage) {this.nextPage = nextPage;}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {this.totalCount = totalCount;}public String toString() {return "pageSize=" + pageSize + "currentPage=" + currentPage+ "prePage=" + prePage + "nextPage=" + nextPage + "totalPage="+ totalPage + "totalCount=" + totalCount;}}
4.编写操做的dao层,和dao.xml
其中,需要拦截的方法,需要以ID(正则匹配),我以.page结尾。
5.controller层
@RequestMapping("getAll")@ResponseBodypublic Map<String, Object> get(HttpServletRequest request,HttpServletResponse response, Integer page/* 当前页 */, Integer rows/* 每页显示的数量 */) {// 当前页int intPage = page == null || page <= 0 ? 1 : page;// 设置每页显示的数量int intPageSize = rows == null || rows <= 0 ? 10 : rows;PageParameter page1 = new PageParameter();System.out.println("当前页="+intPage);System.out.println("一页的条数="+intPageSize);page1.setCurrentPage(intPage);page1.setPageSize(intPageSize);PageHelper.startPage(intPage, intPageSize);List<Studio> d = studioService.findAllStudioByPage(page1); // 取商品列表PageInfo<Studio> pageInfo = new PageInfo<Studio>(d);Map<String, Object> reMap = new HashMap<String, Object>();// 取分页信息reMap.put("total", pageInfo.getTotal()); // 总条数reMap.put("pageSize", intPageSize); // 每页显示的数量reMap.put("pageNo", intPage); // 当前页数reMap.put("rows", d); // 从数据库中获取的列表信息return reMap;}
将获取到的查新结果放入reMap,然后前端获取到reMap的值,类似json格式的数据。
前端jsp页面自己完成,我是用easyui做的,
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%String path = request.getContextPath();//System.out.println("path===" + path);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LayOut</title><script src="<%=request.getContextPath()%>/css/easyui/jquery.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/css/easyui/jquery.easyui.min.js" type="text/javascript"></script>
<link href="<%=request.getContextPath()%>/css/easyui/themes/default/easyui.css" rel="stylesheet"ntype="text/css" />
<link href="<%=request.getContextPath()%>/css/easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">$(function() {$('#dg').datagrid({title : '结果列表',pageSize : 10,//默认选择的分页是每页10行数据pageList : [ 5, 10, 15, 20 ],//可以选择的分页集合nowrap : true,//设置为true,当数据长度超出列宽时将会自动截取striped : true,//设置为true将交替显示行背景。collapsible : true,//显示可折叠按钮url : '/tccp/studio/getAll',//url调用Action方法loadMsg : '数据装载中......',singleSelect : false,//为true时只能选择单行fitColumns : false,remoteSort : false,onClickRow : function(rowIndex, rowData) {//单击事件},onDblClickRow : function(rowIndex, rowData) {//双击事件},toolbar : [ {// 工具栏 text : '添加', iconCls : 'icon-add', // 图标 handler : function() { // 处理函数 addBook(); } }, { text : '删除', iconCls : 'icon-cancel', // 图标 handler : function() { // 处理函数 deleteBook(); } }, { text : '编辑', iconCls : 'icon-edit',// 图标 handler : function() {// 处理函数 editBook(); } } ], frozenColumns : [ [ {field : 'id',checkbox : true}, {field : 'studio_id',align : 'center',title : '厅号',width : 200}, {field : 'studio_name',align : 'center',title : '厅名',width : 200} ] ],columns : [ [ {field : 'studio_row_count',align : 'center',title : '行数',width : 200}, {field : 'studio_col_count',align : 'center',title : '列数',width : 200},{field : 'studio_introduction',align : 'center',title : '介绍',width : 200},{field : 'studio_flag',align : 'center',title : '是否安排座位',width : 220}] ],pagination : true,//分页rownumbers : true//行数});var p = $('#dg').datagrid('getPager'); $(p).pagination({ pageSize: 10,//每页显示的记录条数,默认为10 pageList: [5,10,15,20],//可以设置每页记录条数的列表 beforePageText: '第',//页数文本框前显示的汉字 afterPageText: '页 共 {pages} 页', displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录', });//在datagrid实例化之后调用这个方法。$("#dg").datagrid({}).datagrid("page");});function showEditForm() { $('#EditForm').form('submit', {url: "/tccp/studio/add",onSubmit: function () { //表单提交前的回调函数var isValid = $(this).form('validate');//验证表单中的一些控件的值是否填写正确,比如某些文本框中的内容必须是数字if (!isValid) {}return isValid; // 如果验证不通过,返回false终止表单提交},success: function (data) { //表单提交成功后的回调函数,里面参数data是我们调用/BasicClass/ModifyClassInfo方法的返回值。if (data > 0) {$.messager.show({title: '提示消息',msg: '提交成功',showType: 'show',timeout: 1000,style: {right: '',bottom: ''}});$('#dg').datagrid('reload'); // 重新载入当前页面数据 $('#Editwin').window('close'); //关闭窗口}else {$.messager.alert('提示信息', '提交失败,请联系管理员!', 'warning');}}});} // 关闭窗口 function closeForm() { $("#frmEdit").form('clear'); $('#tabEdit').dialog('close'); } // 添加的函数 function addBook() { // 清空原有的数据 $('#frmEdit').form('clear'); // 显示添加对话框 showEditForm(); } </script>
</head><body class="easyui-layout" id="cc"><div id="tabEdit"> <form id="frmEdit"> <input type="hidden" id="id" name="book.id" /> <dl> <dd> 演出厅编号: </dd> <dd> <input type="text" style="width: 150px" id="isbn" name="studioId" /> </dd> </dl> <dl> <dd> 演出厅名称: </dd> <dd> <input type="text" style="width: 150px" id="title" name="studioName" /> </dd> </dl> <dl> <dd> 演出厅行号: </dd> <dd> <input type="text" style="width: 150px" id="price" name="studioRow" /> </dd> </dl> <dl> <dd> 演出厅列号: </dd> <dd> <input type="text" style="width: 150px" id="pubdate" name="studioCol" /> </dd> </dl> <dl> <dd> 是否安排座位: </dd> <dd> <input type="text" style="width: 150px" id="pubdate" name="flag" /> </dd> </dl> <dl> <dd> 演出厅介绍: </dd> <dd> <textarea cols="45" rows="3" id="intro" name="studioIntroduce"></textarea> </dd> </dl> </form> </div> <div region="north" border="true" split="true"style="overflow: hidden; height: 65px;"><form id="searchForm" name="searchForm"><table id="searchTable" width=100%><tr> <td>当前位置:<a href="javascript:void(0)" style="color:#6E6E6E;">影厅管理</a> > 内容列表<!-- <input class="easyui-textbox" type="text" name="bh" id="bh">--></td> <td>厅名:<input class="easyui-textbox" type="text" name="bjmc" id="bjmc"> <a href="#" class="easyui-linkbutton" icon="icon-search"onclick="doSearch()">查询</a></td><td> <td><a href="#" class="easyui-linkbutton" icon="icon-search"onclick="doSearch()">退出</a></td> </tr></table></form></div><div region="center" border="true" split="true"style="overflow: hidden;"><table id="dg" title="My Users" class="easyui-datagrid" width="100%"height="99%" toolbar="#toolbar" rownumbers="true" fitColumns="true"singleSelect="true"></table></div></body>
</html>
代码写的有点乱,我会继续改的,原谅小白一枚!!!!,有什么问题就问,要源码的,都可以。
转载于:https://www.cnblogs.com/wangxiaopei/p/8551181.html
[置顶]mybatis分页插件实现分页...相关推荐
- 使用PageHelper分页插件手动分页,其他查询数据出现串连问题
在一个需求中,需要在代码中进行分页,于是就选用了PageHelper分页插件进行分页,刚开始的时候忘记了PageHelper分页插件的用法于是我就用: // 设置分页查询条件 PageHelper.s ...
- mybatis分页插件_MyBatis 分页插件 5.2.0 发布
时隔整整8个月,分页插件这次带来了一次大的更新. 5.2.0 - 2020-07-26 •jsqlparser升级到3.2版本,sql解析更好,对sqlserver支持更好.•修改 sqlserver ...
- Mybatis的插件 PageHelper 分页查询使用方法
Mybatis的一个插件,PageHelper,非常方便mybatis分页查询.国内牛人的一个开源项目,有兴趣的可以去看源码,都有中文注释(ps:某些源码一大堆英文,痛哭流涕!) 在github上仓库 ...
- Mybatis采用分页插件实现分页
前言 小编我将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注一下! 也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!让我们在成长的道路上互相学习,让 ...
- spring boot+mybatis+thymeleaf+pagehelper分页插件实现分页功能
文章目录 前言 正文 业务场景 后端 pom.xml application.yml 实体类video.java和User.java----映射VideoMapper.xml----VideoMapp ...
- 全注解怎么使用分页插件_分页插件使用的方式
分页插件使用的方式 修改 pom 文件,添加分页 jar 包依赖 修改 mybatis.xml 文件 UserDao 接口,UserMapper.xml 添加对应方法与实现 sql 对应 UserSe ...
- element ui +mybatisPlus分页插件实现分页功能
elementui pagination插件 当然这里的依赖部分就需要去创库ctrl+v了 <!--分页部分 pagination插件 @current-change="handlep ...
- java ajax jquery分页插件_JQueryPagination分页插件,ajax从struts请求数据
2017-07-16 学完了struts2,做了个关于分页的小例子,用到了JQuery分页插件Pagination,先贴下插件下载地址 插件作者对于参数讲解的不够详细,琢磨了半天才明白怎么用,不多说, ...
- jquery php 分页插件,jQuery 分页插件代码
jQuery 分页插件代码 js代码 var newlist = new Vue({ el: '#app', data: { current_page: 1, //当前页 pages: 50, //总 ...
最新文章
- python添加行索引_python-熊猫在特定级别向多索引添加行
- Genymotion 解决虚拟镜像下载速度特别慢的问题
- B站最火数学视频3Blue1Brown是如何制作的
- 【Leetcode】背包问题模板
- zabbix3.4监控java_CentOS7,zabbix3.4通过,zabbix-Java-gateway监控Tomcat
- Filter过滤器~~~编码过滤和登陆验证
- VB 字符串续行符最多25行…………
- CentOS7设置环境变量
- [Android Memory] Android系统中查看某个应用当前流量的方法
- 002-软件质量模型
- windows 中hosts文件
- 基于SpringBoot的酒店管理系统
- mysql列名命名_重命名MySQL中的列名?
- php 公众号迁移,openid转换
- “企鹅号+时尚集团MCN”强强联手 打造19春夏时装周报道新模式
- weed-fs 压力测试
- Redis跳跃表源码解析
- alert angularjs
- 门诊地图导航怎么做,零成本的医院室内导航解决方案
- Python编程:使用os.urandom生成Flask的秘钥SECRET_KEY
热门文章
- cent os 7 mysql_centos – 百胜:Cent OS 7中没有包mysql-server
- 顺德职业技术学院计算机专业录取线,顺德职业技术学院录取分数线2021是多少分(附历年录取分数线)...
- jqueryui手风琴_jQueryUI手风琴插件
- mockito模拟依赖注入_Mockito间谍–部分模拟
- jquery 遍历无限极树_jQuery parent()和children()树遍历函数示例
- web前端面试:不做面试“海王”,一份资料就可成功上岸!
- 数据库改名系列(数据库名,逻辑名,物理文件名)
- 活动目录集成区域DNS服务
- 找呀志_通过开源框架引AsyncHttpClient处理get/post要求
- SQLSERVER中如何快速比较两张表的不一样