这两种方式都是介绍的后端分页,也就是每次点击上一页、下一页都会发送请求到后端进行分页查询,这样的话对于大数据量的话效率不会低,前台分页的话就是把所有数据查到给前端,用前端插件进行分页,只请求一次,就不用写pageHelper那些了,直接给前台返回List<实体>就可以了,然后把前台插件中sidePagination这个属性,改成client就可以了;
如下图是bootstrap前台分页:
以下是bootstrap的后台分页:
导入jar包:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.1</version>
</dependency>
前台代码,需要导入bootstrap相应的文件,下载地址:
https://download.csdn.net/download/kxj19980524/10832565
下面是初始化bootstrap表格的代码:
<%--
Created by IntelliJ IDEA.
User: liulx
Date: 2018/9/3
Time: 9:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html >
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" target="_blank" rel="external nofollow" >
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/table/bootstrap-table.min.css" target="_blank" rel="external nofollow" >
<title>展示用户信息!</title>
</head>
<body>
<div class="container">
<br>
<table id="table"></table>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="${pageContext.request.contextPath}/static/common/jquery-3.2.1.js" ></script>
<script src="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js" ></script>
<script src="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/table/bootstrap-table.min.js"></script>
<script src="${pageContext.request.contextPath}/static/bootstrap-3.3.7-dist/table/locale/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript">
var option = { // 对应table标签的id
url: "<%=request.getContextPath()%>/witkeyUser/findAllUser", // 获取表格数据的url
cache: false, // 设置为 false 禁用 AJAX 数据缓存, 默认为true
striped: true, //表格显示条纹,默认为false
pagination: true, // 在表格底部显示分页组件,默认false
pageList: [5,10,15, 20], // 设置页面可以显示的数据条数
pageSize: 5, // 页面数据条数
pageNumber: 1, // 首页页码
sidePagination: 'server', // 设置为服务器端分页server client前台分页
sortName: 'id', // 要排序的字段
sortOrder: 'a sc', // 排序规则
columns: [
{
checkbox: true, // 显示一个勾选框
align: 'center' // 居中显示
}, {
field: 'id', // 返回json数据中的name
title: '编号' // 表格表头显示文字
}, {
field: 'username',
title: '用户名'
}, {
field: 'password',
title: '密码'
}, {
title: "电话",
field:'iphone'
}, {
title: "QQ",
field:'qq'
}, {
title: "创建用户",
field:'createName'
}, {
title: "创建时间",
field:'createDate'
}, {
title: "修改用户",
field:'updateName'
}, {
title: "修改时间",
field:'updateDate'
}, {
title: "备注",
field:'remarks'
}
],
onLoadSuccess: function(){ //加载成功时执行
console.info("加载成功");
},
onLoadError: function(){ //加载失败时执行
console.info("加载数据失败");
}
}
$(function () {
$("#table").bootstrapTable(option);
})
</script>
</body>
</html>
controller接收的时候我自己封装了一个实体类:
package com.bgs.pojo;
public class BootTableParam {
private String sort;//排序的字段名 id
private String order;//排序的方式:desc|asc
private Integer offset;//偏移量,从第几条数据开始显示:0
private Integer limit;//一页显示的条数:5
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
}
//后台分页
@RequestMapping("/findAllUser")
@ResponseBody
public BootStrapResult findAllUser(BootTableParam bootTableParam) {
BootStrapResult allUser = witkeyUserService.findAllUser(bootTableParam);
return allUser;
}
service分页代码:(因为bootstrap传入后台参数跟easyui不同,所以这用的pageHelper中 .offsetPage 这个方法来分页查询,然后把参数传进去,它就会自动变成分页来查询了,sql语句直接查询所有就可以了):
@Override
public BootStrapResult findAllUser(BootTableParam bootTableParam) {
PageHelper.offsetPage(bootTableParam.getOffset(),bootTableParam.getLimit()); //从第几个开始查 一页查几个
List<WitkeyUser> allUser = witkeyUserMapper.findAllUser(bootTableParam.getSort(),bootTableParam.getOrder());
PageInfo<WitkeyUser> info = new PageInfo<>(allUser);
BootStrapResult result = new BootStrapResult();
result.setRows(info.getList());
result.setTotal(info.getTotal());
return result;
}
返回类BootStrapResult :
package com.bgs.pojo;
import java.util.List;
public class BootStrapResult<T> {
private Long total;//一共有多少条数据
private List<T> rows; //查询结果
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
sql语句:直接查所有就可以,有排序的话加上排序等:
easyui进行后台分页:
1. 添加maven依赖
我用了版本控制:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<pagehelper.version>5.1.1</pagehelper.version>
2. 在mybatis-config.xml里面配置
- PageHelper是一个mybatis的分页插件
- com.github.pagehelper为PageHelper类所在包名
- pagehelper是mybatis 提供的分页插件,目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
3. 在applicationContext.xml里面配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--需要的是这行注释下面的这一句-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
4. mapper.xml里写正常的sql语句,不需要添加任何分页的东西
5. service层参考代码
- PageInfo本身就是插件包下的类
- EUDataGridResult是我封装的一个工具类
public EUDataGridResult showUserByDeptId(Integer id, Integer page, Integer rows) {
//将分页参数传入pageHelper对象
PageHelper.startPage(page,rows);
List<User> userList = userMapper.showUserByDeptId(id);
//将数据传入pageInfo中将数据进行分页
PageInfo<User> pi = new PageInfo<>(userList);
EUDataGridResult result = new EUDataGridResult();
result.setRows(userList);
result.setTotal(pi.getTotal());
return result;
}
EUDataGridResult工具类里的代码如下:
public class EUDataGridResult {
//条数
private long total;
//具体的信息:集合
private List<?> rows;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
6. 表现层通过controller来调用即可
@RequestMapping("/showUserByDeptId/{id}")
public @ResponseBody EUDataGridResult showUserByDeptId(Integer page, Integer rows,
@PathVariable Integer id,HttpSession session){
EUDataGridResult result = userService.showUserByDeptId(id,page,rows);
return result;
}