1.创建实体类
package com.mxl.models;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String password;
private String name;
private String sex;
private Date birthday;
private String email;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
2.创建实体类映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mxl.models.User" table="user">
<id name="id" column="id"><generator class="native"></generator></id>
<property name="username"></property>
<property name="password"></property>
<property name="name"></property>
<property name="sex"></property>
<property name="birthday"></property>
<property name="email"></property>
<property name="phone"></property>
</class>
</hibernate-mapping>
3.创建action
package com.mxl.actions;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.mxl.dao.UserDao;
import com.mxl.models.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserManager extends ActionSupport {
private User user;
private List<User>users;
private UserDao userDao=new UserDao();
private int pageNo=1;
private int pageSize=8;
private int pageCount;
public String user_getAll(){
pageCount=userDao.getPageCount(pageSize);
if(pageNo<1){
pageNo=1;
}else if(pageNo>pageCount){
pageNo=pageCount;
}
users=userDao.selectAllList(pageNo,pageSize);
return "userList";
}
public void setUsers(List<User> users) {
this.users = users;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}
4.创建UserDao
package com.mxl.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mxl.connect.HibernateSessionFactory;
import com.mxl.models.User;
public class UserDao {
private Session session=null;
private Transaction tx=null;
public List<User> selectAllList(int pageNo,int pageSize){//分页查询
List<User>list=null;
try{
session=HibernateSessionFactory.getSession();
String hql="from User";
Query query=session.createQuery(hql);
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
list=query.list();
}catch(Exception ex){
ex.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
public int getPageCount(int pageSize){//获得总页数
String hql="select count(id) from User";
int count=0;
int pageCount=0;
try{
session=HibernateSessionFactory.getSession();
Query query=session.createQuery(hql);
long temp=(Long)query.uniqueResult();
count=(int)temp;
}catch(Exception e){
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
if(count%pageSize==0){
pageCount=count/pageSize;
}else{
pageCount=count/pageSize+1;
}
return pageCount;
}
}
5.创建显示页面(关键代码)
<p style="font-weight: bold; "><a href="Manager!user_getAll?pageNo=1">[首页]</a>
<a href="Manager!user_getAll?pageNo=<s:property value="pageNo-1"/>">[上一页]</a>
<a href="Manager!user_getAll?pageNo=<s:property value="pageNo+1"/>">[下一页]</a>
<a href="Manager!user_getAll?pageNo=<s:property value="pageCount"/>">[尾页]</a></p>
6.运行效果