java分页 (spring+springmvc+hibernate)
来源:互联网 发布:云数据库 编辑:程序博客网 时间:2024/06/05 03:15
分页方式:
1.直接从数据库分页: 2.先全部查询,用 List.subList( firstResult,maxResult ) 分页显示
用SQL语句: firstResult : 当前页的第一条记录下标
mysql: select userId,name,sex maxResult: 当前页的最后一条记录下标 (左闭右开)
from user
where userId="001"
limit 2,10;
oracle: select * from
(select s.* ,rownum rn from (select * from 表名) s where rownum<=10) where rn>=1;
3.用 HibernateTemplate.findByCriteria(criteria, firstResult, maxResults),可避免写SQL
firstResult : 当前页的第一条记录下标。
maxResults :当前页的大小。
由于使用的是spring+springmvc+hibernate,所以以后两种方法为例
分页类:(可用于直接从数据库分页查询,也可用于先全部查询,再分页)
package com.hhy.util;
import java.io.Serializable;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class PageUtil implements Serializable{
private int firstResult; //分页的起始索引
private int maxResult; //结束索引
private int pageSize; //每页的记录条数
private int currentPage; //当前页
private int totalPage; //总页数
private int totalCount; //总的记录数
private List pageList; //每页的数据 (用于list分页查询)
public PageUtil() {
super();
}
/**
* @param currentPage 当前页
* @param pageSize 每页的大小
* @param totalCount 总记录数
*/
public void pageInit(int currentPage,int pageSize,int totalCount){
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalCount%pageSize!=0?totalCount/pageSize+1:totalCount/pageSize;
this.currentPage = currentPage>=totalPage?totalPage:currentPage;
this.currentPage=this.currentPage<=1?1:this.currentPage;
this.firstResult = (currentPage-1)*pageSize;
this.maxResult=pageSize*currentPage;
if(this.maxResult>=totalCount){
this.maxResult=totalCount;
}
}
public int getFirstResult() {
return firstResult;
}
public void setFirstResult(int firstResult) {
this.firstResult = firstResult;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getMaxResult() {
return maxResult;
}
public void setMaxResult(int maxResult) {
this.maxResult = maxResult;
}
public List getPageList() {
return pageList;
}
public void setPageList(List pageList) {
this.pageList = pageList;
}
@Override
public String toString() {
return "PageUtil [firstResult=" + firstResult + ", maxResult="
+ maxResult + ", pageSize=" + pageSize + ", currentPage="
+ currentPage + ", totalPage=" + totalPage + ", totalCount="
+ totalCount + ", pageList=" + pageList + "]";
}
}
用HibernateTemplate.findByCriteria():
页面:
<!-- 多条件查询 -->
<div class="bar">用户管理</div>
<div class="conbar" >
<form id="commform" class="form-inline" >
<input type="text" id="commodityId" name="commodityId" class="form-control" placeholder="用户编号"
style="width:130px;margin-left:20px" >
<input type="text" id="commodityName" name="commodityName" class="form-control" placeholder="用户姓名"
style="width:130px;margin-left:40px" >
<input type="button" value="查询" class="querybut" id="ufind">
<input type="button" value="添加" class="querybut" id="uadd">
<input type="hidden" value="${page.currentPage}" name="currentPage">
</form>
</div>
<!-- 显示查询的数据 -->
<div class="table-responsive" style="margin-left:20px;margin-top:0px;margin-right:30px">
<table class="table">
<thead>
<tr>
<th>用户编号</th>
<th>姓名</th>
<th>性别</th>
<th>职务</th>
<th>操作</th>
<th>业绩</th>
</tr>
</thead>
<tbody>
<c:forEach items="${uList}" var="u">
<tr>
<td>${u.userId}</td>
<td>${u.name}</td>
<td>${u.sex}</td>
<td>${u.position.positionName}</td>
<td>
<a href="javascript:update('${u.userId}','${u.name}','${u.sex}','${u.position.positionId}','${u.privilage.privilageId}')" style="text-decoration: none">修改</a>
<a href="javascript:dele('${u.userId}','${u.name}','${u.sex}','${u.position.positionId}','${u.privilage.privilageId}','${u.password}')" style="text-decoration: none">删除</a>
</td>
<td>
<a href="#">
<img src="images/yj.png">
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 分页 -->
<div class="container" style="margin-top:10px;">
<ul class="pager">
<c:if test="${page.totalPage<=1}">
<li><a >上一页</a></li>
<li><a >下一页</a></li>
</c:if>
<c:if test="${page.totalPage>1}">
<c:if test="${page.currentPage<=1}">
<li><a>上一页</a></li>
<li><a href="<%= path %>/xtgl/userControl?currentPage=${page.currentPage+1}&commodityId=${cId}&commodityName=${cName}">下一页</a></li>
</c:if>
<c:if test="${page.currentPage>1&&page.currentPage<page.totalPage}">
<li><a href="<%= path %>/xtgl/userControl?currentPage=${page.currentPage-1}&commodityId=${cId}&commodityName=${cName}">上一页</a></li>
<li><a a href="<%= path %>/xtgl/userControl?currentPage=${page.currentPage+1}&commodityId=${cId}&commodityName=${cName}">下一页</a></li>
</c:if>
<c:if test="${page.currentPage>=page.totalPage}">
<li><a href="<%= path %>/xtgl/userControl?currentPage=${page.currentPage-1}&commodityId=${cId}&commodityName=${cName}">上一页</a></li>
<li><a>下一页</a></li>
</c:if>
</c:if>
</ul>
</div>
<script>
//查询
$("#ufind").click(function(){
$("#commform").submit();
});
</script>
后台:
Controller
@RequestMapping(value="/xtgl/userControl")
public ModelAndView userCon(HttpServletRequest request,@ModelAttribute("commodityId") String id,@ModelAttribute("commodityName") String name){
int currentPage;
int totalCount=userService.getTotalCount(id, name);
System.out.println("id: "+id+" "+"name: "+name);
if(request.getParameter("currentPage")==null){
currentPage=1;
}else{
currentPage=Integer.parseInt(request.getParameter("currentPage"));
}
page.pageInit(currentPage, 10, totalCount);
List<User> uList=userService.getAllUser(id,name,page);
ModelAndView mav=new ModelAndView();
mav.setViewName("xtgl/userControl");
mav.addObject("uList", uList);
mav.addObject("page",page);
mav.addObject("cId", id);
mav.addObject("cName", name);
return mav;
}
DaoImpl
public List<User> getAllUser(String id,String name,PageUtil page) {
DetachedCriteria criteria=DetachedCriteria.forClass(User.class);
criteria.addOrder(Order.desc("userId"));
if(id!=null&&!id.isEmpty()){
criteria.add(Restrictions.eq("userId",id));
}
if(name!=null&&!name.isEmpty()){
criteria.add(Restrictions.eq("name",name));
}
List<User> users=this.hibernateTemplate.findByCriteria(criteria,page.getFirstResult(),page.getPageSize());
if(users!=null&&users.size()>0){
return users;
}else{
return null;
}
}
serviceImpl
public List<User> getAllUser(String id,String name,PageUtil page) {
return userDao.getAllUser(id,name,page);
}
用此方法查询会产生一下类似下面的SQL语句:
select userId,name,sex
from user
where userId="001"
limit 2,10;
用 list 逻辑处理类似,只是需要把数据全部查询出来,在对数据分页显示,像下面这样:
List<User> users=userService.getAll();
users.sub( param1,param2);
结语:总之,都是从数据库中先把数据全部查出来 (有条件就加上条件)在分页显示,,哈哈。。
- java分页 (spring+springmvc+hibernate)
- spring+springmvc+hibernate,实现分页功能
- spring+springmvc+hibernate,实现分页功能
- SpringMVC+Hibernate+Spring框架之一(Hibernate 分页实现,视图采用Freemarker)
- Hibernate+SpringMVC+Spring+分页实现留言管理项目
- springMVC框架整合--(Spring+SpringMVC+hibernate)
- Springmvc 配置(spring+hibernate+springmvc)
- (二)Springmvc 配置(spring+hibernate+springmvc)
- JAVA WEB搭建 SpringMVC+Spring+hibernate 框架
- JAVA WEB搭建 SpringMVC+Spring+hibernate 框架
- Spring+hibernate+springmvc 整合(二)
- Spring+hibernate+springmvc 整合(三)
- Spring+hibernate+springmvc 整合(四)
- Spring+hibernate+springmvc 整合(五)
- Spring+hibernate+springmvc 整合(六)
- Spring+hibernate+springmvc 整合(七)
- SpringMVC+Hibernate+Spring整合实例(一)
- SpringMVC+Hibernate+Spring整合实例(二)
- GYM100608 J
- LeetCode-135. Candy (JAVA)根据等级分糖果
- 模仿iOS7 task switcher的卡片动画
- java中next与nextLine用法区别
- ETL工具比较(Informatica ,SSIS,Kettle )
- java分页 (spring+springmvc+hibernate)
- 遍历map的几种方式
- ztree组件使用--判断选中节点的根节点
- iOS 基于GCDAsyncSocket快速开发Socket通信
- 关于scala中的.endswith问题
- resin4 配置的优化
- shell中条件判断if中的-z到-d的意思
- mysql主从复制环境安装
- 【双硬盘双系统】LinuxC下对EFI格式windows磁盘进行分区-动态生成GPT表