Spring Data JPA+kkpager实现分页功能实例
程序员文章站
2023-12-16 23:15:40
一、spring data jpa分页
分页效果如下:
前台表格用的是: bootstrap
分页插件用的是: kkpager
kkpager是一个js分页展示...
一、spring data jpa分页
分页效果如下:
前台表格用的是: bootstrap
分页插件用的是: kkpager
kkpager是一个js分页展示控件,传入简单参数就能使用的分页效果控件,github地址:
项目结构:
familymember实体类:
package com.fendo.entity; import java.io.serializable; import java.util.date; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; import org.hibernate.annotations.genericgenerator; @entity @table(name="family_member") public class familymember implements serializable{ private integer id; private string familyname; private string familycharge; private string mobile; private string email; private string address; private date createdata; @id @generatedvalue(strategy=generationtype.identity) public integer getid() { return id; } public void setid(integer id) { this.id = id; } @column(name="family_name") public string getfamilyname() { return familyname; } public void setfamilyname(string familyname) { familyname = familyname; } @column(name="family_charge") public string getfamilycharge() { return familycharge; } public void setfamilycharge(string familycharge) { familycharge = familycharge; } @column(name="mobile") public string getmobile() { return mobile; } public void setmobile(string mobile) { mobile = mobile; } @column(name="email") public string getemail() { return email; } public void setemail(string email) { email = email; } @column(name="address") public string getaddress() { return address; } public void setaddress(string address) { address = address; } @column(name="create_data") public date getcreatedata() { return createdata; } public void setcreatedata(date createdata) { createdata = createdata; } public familymember() { super(); // todo auto-generated constructor stub } public familymember(integer id, string familyname, string familycharge, string mobile, string email, string address, date createdata) { super(); this.id = id; familyname = familyname; familycharge = familycharge; mobile = mobile; email = email; address = address; createdata = createdata; } }
familydao接口类:
package com.fendo.dao; import java.util.list; import java.util.map; import org.springframework.data.jpa.repository.jpaspecificationexecutor; import org.springframework.data.repository.pagingandsortingrepository; import com.fendo.entity.familymember; public interface familydao extends pagingandsortingrepository<familymember, string>, jpaspecificationexecutor<familymember>{ }
familyservice服务接口类:
package com.fendo.service; import java.util.list; import java.util.map; import com.fendo.entity.familymember; public interface familyservice { public list<familymember> getall() throws exception; public familymember save(familymember familymember) throws exception; public map<string, object> getuserbysearch(map<string, string> familyargs, final string sorttype) throws exception; }
familyservice服务接口实现类:
package com.fendo.service.imp; import java.util.arraylist; import java.util.list; import java.util.map; import javax.persistence.criteria.criteriabuilder; import javax.persistence.criteria.criteriaquery; import org.apache.commons.lang.stringutils; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.domain.page; import org.springframework.data.domain.pageable; import org.springframework.data.jpa.domain.specification; import javax.persistence.criteria.predicate; import javax.persistence.criteria.root; import javax.transaction.transactional; import org.springframework.stereotype.service; import com.fendo.dao.familydao; import com.fendo.entity.familymember; import com.fendo.service.familyservice; import com.fendo.util.pageutils; @service @transactional public class familyserviceimp implements familyservice{ @autowired public familydao familydao; @override public list<familymember> getall() throws exception { return (list<familymember>) this.familydao.findall(); } @override public familymember save(familymember familymember) throws exception { return familydao.save(familymember); } /** * 查询用户信息列表(支持分页和多条件查询)。 * */ @override public map<string, object> getuserbysearch(map<string, string> familyargs, final string sorttype) throws exception { // 获得分页对象pageable,并且在pageable中页码是从0开始,设定按照sorttype升序排列 pageable pageable = pageutils.buildpagerequest(integer.valueof(familyargs.get("pagenum")), integer.valueof(familyargs.get("pagesize")), sorttype); page<familymember> objpage = familydao.findall(new specification<familymember>() { public predicate topredicate(root<familymember> root, criteriaquery<?> query, criteriabuilder cb) { list<predicate> lstpredicates = new arraylist<predicate>(); if (stringutils.isnotblank(familyargs.get("familyname"))) { lstpredicates.add(cb.like(root.get("familyname").as(string.class), "%" + familyargs.get("familyname") + "%")); } if (stringutils.isnotblank(familyargs.get("mobile"))) { lstpredicates.add(cb.like(root.get("mobile").as(string.class), "%" + familyargs.get("mobile") + "%")); } predicate[] arraypredicates = new predicate[lstpredicates.size()]; return cb.and(lstpredicates.toarray(arraypredicates)); } }, pageable); return pageutils.getpagemap(objpage); } }
前台接受参数工具类:
package com.fendo.util; import java.util.hashmap; import java.util.map; import javax.servlet.http.httpservletrequest; /** * 工具类 * @author fendo * */ public class familyutil { /** * 封装从前台传递过来的查询参数。 * */ public static map<string, string> getselargstomap(httpservletrequest request) throws exception { map<string, string> serargs = new hashmap<string, string>(); string familyname = request.getparameter("familyname"); string mobile = request.getparameter("mobile"); string pagenum = request.getparameter("pagenum") == null ? "1" : request.getparameter("pagenum"); string pagesize = request.getparameter("pagesize") == null ? "10" : request.getparameter("pagesize"); serargs.put("familyname", familyname); serargs.put("mobile", mobile); serargs.put("pagenum", pagenum); serargs.put("pagesize", pagesize); return serargs; } }
分页工具类:
package com.fendo.util; import java.util.hashmap; import java.util.map; import org.apache.commons.lang3.stringutils; import org.springframework.data.domain.page; import org.springframework.data.domain.pagerequest; import org.springframework.data.domain.sort; import org.springframework.data.domain.sort.direction; public class pageutils { /** * 封装分页数据到map中。 */ public static map<string, object> getpagemap(page<?> objpage) { map<string, object> resultmap = new hashmap<string, object>(); resultmap.put("resultlist", objpage.getcontent()); // 数据集合 resultmap.put("totalnum", objpage.gettotalelements()); // 总记录数 resultmap.put("totalpage", objpage.gettotalpages()); // 总页数 resultmap.put("pagenum", objpage.getnumber()); // 当前页码 resultmap.put("pagesize", objpage.getsize()); // 每页显示数量 return resultmap; } /** * 创建分页请求。 * * @param pagenum 当前页 * @param pagesize 每页条数 * @param sorttype 排序字段 * @param direction 排序方向 */ public static pagerequest buildpagerequest(int pagenum, int pagesize, string sorttype, string direction) { sort sort = null; if (!stringutils.isnotblank(sorttype)) { return new pagerequest(pagenum - 1, pagesize); } else if (stringutils.isnotblank(direction)) { if (direction.asc.equals(direction)) { sort = new sort(direction.asc, sorttype); } else { sort = new sort(direction.desc, sorttype); } return new pagerequest(pagenum - 1, pagesize, sort); } else { sort = new sort(direction.asc, sorttype); return new pagerequest(pagenum - 1, pagesize, sort); } } /** * 创建分页请求(该方法可以放到util类中). */ public static pagerequest buildpagerequest(int pagenum, int pagesize, string sorttype) { return buildpagerequest(pagenum, pagesize, sorttype, null); } /** * 创建分页请求 * * @param pagenum * @param pagesize * @param sort * @return */ public static pagerequest buildpagerequest(int pagenum, int pagesize, sort sort) { return new pagerequest(pagenum - 1, pagesize, sort); } /** * 创建分页请求(该方法可以放到util类中). */ public static pagerequest buildpagerequest(int pagenum, int pagesize) { return buildpagerequest(pagenum, pagesize, null, null); } }
controller类:
package com.fendo.controller; import java.util.hashmap; import java.util.list; import java.util.map; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; import com.fendo.entity.familymember; import com.fendo.service.imp.familyserviceimp; import com.fendo.util.familyutil; @controller() @requestmapping(value="datatable") public class datatablecontroller { @autowired public familyserviceimp familymember; @requestmapping(value="/home_list") public string home(model model,httpservletrequest request,httpservletresponse response){ map<string, object> resultmap = new hashmap<>(); list<familymember> list; try { list = familymember.getall(); // 查询表单或分页保持请求时 请求参数的接收 map<string, string> serargs = new hashmap<string, string>(); serargs = familyutil.getselargstomap(request);//这个类在下面给出 resultmap = familymember.getuserbysearch(serargs, "createdata"); //按创建时间排序 model.addattribute("resultmap",resultmap); } catch (exception e) { e.printstacktrace(); } return "datatable"; } }
首页datatable.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="ctx" value="${pagecontext.request.contextpath}"/> <!doctype html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>spring data jpa分页示例</title> <!-- bootstrap --> <link href="${ctx}/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <!-- jquery (necessary for bootstrap's javascript plugins) --> <script src="${ctx}/js/jquery-1.10.2.min.js"></script> <!-- include all compiled plugins (below), or include individual files as needed --> <script src="${ctx}/js/bootstrap.min.js"></script> <script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script> <link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css" rel="external nofollow" /> </head> <body> <div class="container"> <div style="text-align:center"> <h2>spring data jpa分页示例</h2> </div> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">数据表格</h3> </div> <div class="panel-body" > <form class="form-inline" id="searchform" method="post" action="${ctx}/datatable/home_list"> <div class="row"> <div class="form-group col-sm-3"> <input type="checkbox" id="inlinecheckbox1" value="option1"> <label>选择所有</label> </div> <div class="form-group col-sm-3"> <label>家庭名:</label> <input type="input" name="familyname" id="familyname" class="form-control" id="exampleinputname3" placeholder="用户名"> </div> <div class="form-group col-sm-3"> <label>手机号:</label> <input type="input" name="mobile" id="mobile" class="form-control" id="exampleinputmobile3" placeholder="手机号"></input> </div> <div class="col-sm-3"> <button type="submit" class="btn btn-primary">搜索</button> <button type="button" class="btn btn-primary">删除</button> </div> </div> </form> <table class="table table-hover" style="margin-top:2px;"> <thead> <tr> <th></th> <th>顺序</th> <th>家庭名称</th> <th>家庭key</th> <th>负责人</th> <th>手机号</th> <th>邮箱</th> <th>家庭地址</th> <th>创建时间</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody> <c:foreach var="lise" items="${resultmap.resultlist}" varstatus="status"> <tr> <td><input type="checkbox" id="inlinecheckbox1" value="option1"></td> <th scope="row">${status.index+1}</th> <td>${lise.familyname}</td> <td>${lise.id}</td> <td>${lise.familycharge}</td> <td>${lise.mobile }</td> <td>${lise.email}</td> <td>${lise.address}</td> <td>${lise.createdata}</td> <td>启用</td> <td><a href="#" rel="external nofollow" >编辑</a></td> </tr> </c:foreach> </tbody> </table> </div> </div> <div style="width:800px;margin:0 auto;"> <div id="kkpager"></div> </div> </div> <script> var param = ""; $(function() { var totalpage = "${resultmap.totalpage}"; var totalrecords = "${resultmap.totalnum}"; var pagesize = "${resultmap.pagesize}"; var pagenum = parseint("${resultmap.pagenum}") + 1; //初始化分页控件 //有些参数是可选的,比如lang,若不传有默认值 kkpager.init({ pno: pagenum, //总页码 total: "${resultmap.totalpage}", //总数据条数 totalrecords: totalrecords, //链接前部 hrefformer: '${ctx}/datatable/home_list', //链接尾部 hreflatter: '', getlink: function(n) { return getinitparam() + "&pagenum=" + n + "&pagesize=" + pagesize; }, lang: { prepagetext: '上一页', nextpagetext: '下一页', totalpagebeforetext: '共', totalpageaftertext: '页', totalrecordsaftertext: '条数据', gopagebeforetext: '转到', gopagebuttonoktext: '确定', gopageaftertext: '页', buttontipbeforetext: '第', buttontipaftertext: '页' } }); //生成 kkpager.generpagehtml(); $('#mykkpagerselect').val(pagesize); }); function getinitparam() { var familyname = $('#familyname').val(); var mobile = $('#mobile').val(); var attr = "?familyname=" + encodeuri(encodeuri(familyname)) + "&mobile=" + mobile; return "${ctx}/datatable/home_list" + attr; } /** * 搜索。 */ function search() { $('#searchform').submit(); } </script> </body> </html>
完整示例: springdata-jpa_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Spring Data JPA实现动态条件与范围查询实例代码
-
Spring MVC+MyBatis+MySQL实现分页功能实例
-
Spring Data JPA实现动态条件与范围查询实例代码
-
Spring Data JPA实现分页Pageable的实例代码
-
在Spring Boot中使用Spring-data-jpa实现分页查询
-
序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
-
ASP.Net MVC+Data Table实现分页+排序功能的方法
-
php实现分页功能的详细实例方法
-
spring-data-jpa实现增删改查以及分页操作方法
-
Java List分页功能实现代码实例