使用Hibernate和Struts2实现分页功能可以分为以下几个步骤:
- 添加依赖
在pom.xml文件中添加Hibernate和Struts2的依赖,示例代码如下:
<dependencies>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.32.Final</version>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.26</version>
</dependency>
</dependencies>
- 配置Hibernate
在项目的配置文件中添加Hibernate的配置信息,包括数据源、映射文件、SessionFactory等,示例代码如下:
<hibernate-configuration>
<session-factory>
<!-- 数据源配置 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 映射文件 -->
<mapping class="com.example.User"/>
<!-- SessionFactory配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
</session-factory>
</hibernate-configuration>
- 编写数据访问层代码
使用Hibernate实现数据访问层,包括实体类、DAO类、以及相关的HQL查询,示例代码如下:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
public class UserDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<User> findPage(int page, int size) {
Session session = sessionFactory.openSession();
Query query = session.createQuery("from User order by id");
query.setFirstResult((page - 1) * size);
query.setMaxResults(size);
List<User> users = query.list();
session.close();
return users;
}
public Long getCount() {
Session session = sessionFactory.openSession();
Query query = session.createQuery("select count(*) from User");
Long count = (Long) query.uniqueResult();
session.close();
return count;
}
}
- 编写业务逻辑层代码
使用Struts2实现业务逻辑层,包括Action类和相应的视图,示例代码如下:
public class UserAction extends ActionSupport {
private UserDao userDao;
private List<User> users;
private int total;
private int page;
private int size = 10;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<User> getUsers() {
return users;
}
public int getTotal() {
return total;
}
public void setPage(int page) {
this.page = page;
}
public void setSize(int size) {
this.size = size;
}
public String execute() {
users = userDao.findPage(page, size);
total = (int) (userDao.getCount() + size - 1) / size;
return SUCCESS;
}
}
- 编写视图
使用JSP编写视图,包括显示用户列表和分页条,示例代码如下:
<%@ taglib prefix="s" uri="/struts-tags" %>
<ul>
<s:iterator value="users">
<li><s:property value="name"/> (age: <s:property value="age"/>)</li>
</s:iterator>
</ul>
<div class="pagination">
<s:url var="url" value="/user.action"/>
<s:set name="params" value="'&size='+size"/>
<s:if test="page==1">
<span class="disabled">«</span>
</s:if>
<s:else>
<a href="<s:url includeParams="true"><s:param name="page" value="1"/><s:param name="size" value="%{#size}"/></s:url>">«</a>
</s:else>
<s:iterator value="1..total">
<s:if test="page==#this">
<span class="current"><s:property value="#this"/></span>
</s:if>
<s:else>
<a href="<s:url includeParams="true"><s:param name="page" value="%{#this}"/><s:param name="size" value="%{#size}"/></s:url>"><s:property value="#this"/></a>
</s:else>
</s:iterator>
<s:if test="page==total">
<span class="disabled">»</span>
</s:if>
<s:else>
<a href="<s:url includeParams="true"><s:param name="page" value="%{total}"/><s:param name="size" value="%{#size}"/></s:url>">»</a>
</s:else>
</div>
至此,使用Hibernate和Struts2实现分页功能的示例就完成了。以下是两个示例:
示例1:查询用户列表并分页显示
public class UserAction extends ActionSupport {
private UserDao userDao;
private List<User> users;
private int total;
private int page;
private int size = 10;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<User> getUsers() {
return users;
}
public int getTotal() {
return total;
}
public void setPage(int page) {
this.page = page;
}
public void setSize(int size) {
this.size = size;
}
public String execute() {
users = userDao.findPage(page, size);
total = (int) (userDao.getCount() + size - 1) / size;
return SUCCESS;
}
}
示例2:在用户列表中增加搜索功能并分页显示搜索结果
public class UserAction extends ActionSupport {
private UserDao userDao;
private List<User> users;
private int total;
private int page;
private int size = 10;
private String search;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public List<User> getUsers() {
return users;
}
public int getTotal() {
return total;
}
public void setPage(int page) {
this.page = page;
}
public void setSize(int size) {
this.size = size;
}
public void setSearch(String search) {
this.search = search;
}
public String execute() {
users = userDao.findPage(page, size, search);
total = (int) (userDao.getCount(search) + size - 1) / size;
return SUCCESS;
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用hibernate和struts2实现分页功能的示例 - Python技术站