前端效果图:
后台部分
maven依赖:
基于mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
service代码:
Role为角色实体类
PageInfo<Role> selectRoleList(Integer pageNum, Integer pageSize,Role role);
``
serviceimpl实现类:
注意:下面的方法不写在controller里面
@Override
public PageInfo<Role> selectRoleList(Integer pageNum, Integer pageSize, Role role) {
PageHelper.startPage(pageNum, pageSize);
List<Role> list = roleMapper.selectRoleListByName(role);
PageInfo<Role> pageInfo = new PageInfo<>(list);
return pageInfo;
}
controller:
@GetMapping("index")
public String adminIndex(Role record,Model model,
@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
@RequestParam(required=true,value="pageSize",defaultValue="15") Integer pageSize
){
PageInfo<Role> pageInfo = roleService.selectRoleList(pageNum, pageSize, record);
model.addAttribute("rolename", record.getRolename());
model.addAttribute("pageInfo", pageInfo);
return "admin/role/setup";
}
前端部分:
在后端 参数部分defaultValue="15"默认每页显示15条,若没有15条数据不显示分页;根据情况自己修改测试。
需要的js,css:
jquery.pagination.css
jquery.pagination.min.js
插件下载:
https://download.csdn.net/download/qq_22860341/10694937
或者去网上自行寻找资源下载
示例:
<link rel="stylesheet" th:href="@{/api/css/jquery.pagination.css}">
<script th:src="@{/api/js/jquery.pagination.min.js}"></script>
html代码:
<table class="table table-striped" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><input type="checkbox" name="" id="checkall" value="" /></td>
<td>序号</td>
<td>角色</td>
<td>创建时间</td>
</tr>
<tr th:each="a,aStat:${pageInfo.list}">
<td><input type="checkbox" name="childcheck" th:id="${a.id}" th:value="${a.id}" /></td>
<td th:text="${aStat.count}">1</td>
<td th:text="${a.rolename}">超级管理员</td>
<td th:text="${#strings.substring(a.createtime,0,19)}">2018.05.07 15:30</td>
</tr>
</table>
<div class="box">
<div id="pagination" class="page center">
</div>
最后这个分页代码一定加:
<div class="box">
<div id="pagination" class="page center">
</div>
js代码:
<script>
var pageNum = [[${pageInfo.pageNum}]];
var pages = [[${pageInfo.pages}]];
var pageSize = [[${pageInfo.pageSize}]];
//
$("#pagination").pagination({
currentPage: pageNum,
totalPage: pages,
isShow: true,
count: pageSize,
homePageText: "首页",
endPageText: "尾页",
prevPageText: "上一页",
nextPageText: "下一页",
});
//点击页数
$('.ui-pagination-page-item').on('click', function () {
var pageNum = $(this).attr('data-current');
var zhanghao = $("#searchrole").val(); //这是搜索时的参数,根据实际情况加或者不要
window.location.href = encodeURI('/sys/role/index?pageNum=' + pageNum+'&rolename='+zhanghao);
});
</script>