hibernate 實現分頁

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();
			}
		});
	}

很簡單就做到了分頁


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章