分页查询 PageBean工具类
package com.itcast.utils; import java.util.List; public class PageBean<T> { private int totalCount;//总记录数 private int totalPage;//tottalPage://总页码 // private List<T> list = new ArrayList<>();//每页的数据 private List<T> list ;//每页的数据 private int currentPage;//当前的页码 private int rows;//每页显示的记录数 @Override public String toString() { return "PageBean{" + "totalCount=" + totalCount + ", totalPage=" + totalPage + ", list=" + list + ", currentPage=" + currentPage + ", rows=" + rows + '}'; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.setTotalPage(totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1); this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { if (currentPage>totalPage){ this.currentPage = totalPage; } this.totalPage = totalPage; } public int getOffset(){ return (currentPage - 1) * rows; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { if (currentPage == null || currentPage < 1) { currentPage = 1; } this.currentPage = currentPage; } public int getRows() { return rows; } public void setRows(Integer rows) { if (rows == null) { rows = 5; } this.rows = rows; } public PageBean() { } public PageBean(int totalCount, int totalPage, List<T> list, int currentPage, int rows) { this.totalCount = totalCount; this.totalPage = totalPage; this.list = list; this.currentPage = currentPage; this.rows = rows; } }UserMapper
/** * 分页 * @param User * @return */ int findCount(Integer id); /** * 分页查询每页记录 * @param start * @param rows * @param User * @return */ List<User> findByPage(@Param("start") Integer start , @Param("rows") Integer rows,@Param("id") Integer id);UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.itcast.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.itcast.entity.User"> <!--主键id--> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> <result column="userrole" property="userrole" jdbcType="VARCHAR"/> </resultMap> <select id="findByPage" resultType="com.itcast.entity.User"> select * from user where 1=1 <if test="id!=null"> id=#{id} </if> limit #{start},#{rows} </select> <select id="findCount" resultType="Integer"> select count(*) from user where 1=1 <if test="id!=null"> id=#{id} </if> </select> </mapper>service
package com.itcast.service; import com.itcast.entity.User; import com.itcast.utils.PageBean; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserService { int findCount(Integer id); /** *@Param("")这里没使用注解是因为@Param参数提供给xml使用 */ PageBean<User> findByPage(Integer start , Integer rows,Integer id); }UserServiceImpl
package com.itcast.service.impl; import com.itcast.entity.User; import com.itcast.mapper.UserMapper; import com.itcast.service.UserService; import com.itcast.utils.PageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int findCount(Integer id) { return userMapper.findCount(id); } @Override public PageBean<User> findByPage(Integer currentPage, Integer rows,Integer id) { if(currentPage ==null) currentPage = 1; if(rows == null) rows = 10; //创建对象 PageBean<User> pb = new PageBean<>(); pb.setCurrentPage(currentPage); pb.setRows(rows); //调用dao查记录数 int totalCount = userMapper.findCount(id); pb.setTotalCount(totalCount); if (totalCount > 0) { List<User> list = userMapper.findByPage((currentPage -1) * rows, rows,id); pb.setList(list); } else { pb.setList((new ArrayList())); } //计算总的记录数 //调用dao查list集合 return pb; }}Usercontroller
package com.itcast.controller; import com.itcast.entity.User; import com.itcast.service.UserService; import com.itcast.utils.PageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/fenye") @ResponseBody public PageBean<User> find(Integer currentPage, Integer rows,Integer id) { PageBean<User> byPage = userService.findByPage(currentPage, rows,id); System.out.println(byPage); return byPage; } }前端
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>仓库退货</title> <script src="js/jquery-3.4.1.min.js"></script> <style> #table { width: 80%; margin: 0 auto; } a { text-decoration: none } </style> <script> $(function () { $("#btn2").click(function () { $("#tab").empty(); $.ajax({ url: "fenye", type: "post", dataType: "json", success: function (datas) { for (var i = 0; i < datas.list.length; i++) { var data = datas.list[i]; var $tr = $("<tr></tr>"); var $td1 = $('<td width="25"><input type="checkbox" name="cb" value="data.id"></td>'); var $td2 = $('<td width="25" align="center"></td>'); var $td3 = $("<td width='80' align='center'></td>"); var $td4 = $("<td width='100' align='center'></td>"); var $td5 = $("<td width='40' align='center'></td>"); var $td6 = $("<td width='40' align='center'>删除</td>"); // var a = i; $td1.html('<input type="checkbox" align="center">'); // $td2.text(a + 1); $td2.text(data.id); $td3.text(data.username); $td4.text(data.password); $td5.text(data.userrole); $tr.append($td1).append($td2).append($td3).append($td4) .append($td5).append($td6); $tr.addClass('btn') $("#tab").append($tr); } } }) }) }); function nextPage() { if (currentPage >= totalPage) { alert("已经是最后一页了"); return } currentPage++ search() } function prePage() { if (currentPage <= 1) return currentPage-- search() } function reSearch() { currentPage = 1; rows = 5; totalPage = 1; search(); } //起始页 var currentPage = 1; //每页显示的行数 var rows = 5; //总页码1开始 var totalPage = 1; function search() { $("#tab").empty(); var pram = { currentPage, rows }; $.ajax({ url: "fenye", data: pram, type: "post", dataType: "json", success: function (datas) { totalPage = datas.totalPage; $('.page-wrap').text('总共:' + datas.totalCount + '条,当前:' + datas.currentPage + '页,当前' + datas.currentPage + '/' + datas.totalPage + '页') for (var i = 0; i < datas.list.length; i++) { var data = datas.list[i]; var $tr = $("<tr></tr>"); var $td1 = $('<td width="26" align="center"> <input type="checkbox" name="cb" value="data.id"></td>'); var $td2 = $('<td width="25" align="center"></td>'); var $td3 = $("<td width='80' id='id' align='center'></td>"); var $td4 = $("<td width='100' align='center'></td>"); var $td5 = $("<td width='40' align='center'></td>"); var $td6 = $("<td width='40' align='center'>删除</td>"); // var a = i; $td1.html('<input type="checkbox" align="center" id="" value="' + data.id + '">') // $td2.text(a + 1); $td2.text(data.id); $td3.text(data.username); $td4.text(data.password); $td5.text(data.userrole); $tr.append($td1).append($td2).append($td3).append($td4) .append($td5).append($td6); $("#tab").append($tr); } $("#pageSelect").text(''); if (totalPage == 0) { $("#pageSelect").append("<option value='1'>无内容</option>") } for (let i = 1; i <= totalPage; i++) { $("#pageSelect").append("<option value='" + i + "'>" + i + "</option>") } $("#pageSelect").val(currentPage) } }) } search();//这个方法是点开网页数据全部加载出来了 function toPage() { currentPage = $("#pageSelect").val() search() } //显示第一页提示 function prePage() { if (currentPage == 1) { alert("已经是第一页了") return; } currentPage -= 1; search(); } </script> </head> <body> <div align="center"> <button onclick="searchAll()">查看信息</button> </div> <br> <table id="table"> <thead> <tr class="text-c"> <th width="80"><input type="checkbox" id="firstCb"></th> <th width="100">id</th> <th width="40">username</th> <th width="90">password</th> <th width="90">userrole</th> <th width="500">操作</th> </tr> </thead> <tbody id="tab"> </tbody> </table> <div align="center"> <button id="btn3" onclick="prePage();">上一页</button> <button id="btn4" onclick="nextPage();">下一页</button> <span class="page-wrap"></span> <select id="pageSelect"> <option value="0">请选择</option> </select> <button onclick="toPage()">go</button> </div> </body> </html>访问http://localhost:8080/test.jsp 结果