hibernate 实现分页

本文介绍了一个基于Struts2框架实现的简单分页功能。通过AdminUserAction类处理分页逻辑,配合queryDeleteUser.jsp页面展示用户数据,并利用Hibernate进行数据加载。实现了每页显示10条记录,并提供了上一页和下一页的导航。
摘要由CSDN通过智能技术生成

queryDeleteUser.jsp 默认每次读10条数据,当然可以设置

 <s:if test="first>0">
            <td width="49"><div align="center"><a href="admin/queryUsers!queryUsers?first=<s:property value="first-10"/>&maxLength=10">上一页</a></div></td>
            </s:if>
            <s:if test="(first+maxLength)<totalPage">
            <td width="49"><div align="center"><a href="admin/queryUsers!queryUsers?first=<s:property value="first+10"/>&maxLength=10">下一页</a></div></td></s:if>
struts.xml

<action name="queryUsers" class="com.apache.shopping.action.AdminUserAction">
<result>/queryDeleteUser.jsp</result>
</action>
AdminUserAction.java

package com.apache.shopping.action;

import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.apache.shopping.dto.UserDto;
import com.apache.shopping.model.Admin;
import com.apache.shopping.model.User;
import com.apache.shopping.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class AdminUserAction extends ActionSupport implements SessionAware{
	public static final long serialVersionUID=-2352343211L;
	private Map session;
	private UserService userService;
	private UserDto userDto;
	private int first;
	private int maxLength;
	private int totalPage;
	private int id;
	
	private ArrayList<User> users;
	
	public ArrayList<User> getUsers() {
		return users;
	}
	public void setUsers(ArrayList<User> users) {
		this.users = users;
	}
	public int getFirst() {
		return first;
	}
	public void setFirst(int first) {
		this.first = first;
	}
	public int getMaxLength() {
		return maxLength;
	}
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}
	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public UserDto getUserDto() {
		return userDto;
	}
	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}
public String queryUsers(){
	totalPage=userService.getAllUserSize();
	users=userService.loadAllUsers(first, maxLength);
	return SUCCESS;
}
public int getTotalPage() {
	return totalPage;
}
public void setTotalPage(int totalPage) {
	this.totalPage = totalPage;
}
public String deleteUser(){
	userService.deleteUserById(id);
	return  queryUsers();
}
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
}
UserDaoImpl.java

	@SuppressWarnings("unchecked")
	public ArrayList<User> loadAllUsers(final int first,final int maxLength) {//关键代码
		return (ArrayList<User>)hibernateTemplate.executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery("from User");
				query.setFirstResult(first);
				query.setMaxResults(maxLength);
				return query.list();
			}
		});
	}

很简单就做到了分页


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值