Mybatis分页Pagehelper以及前端插件结合使用完整版

前端效果图:
在这里插入图片描述

后台部分

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>
  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值