`
qxmcool
  • 浏览: 93625 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

struts1.x+hibernate+dao分页

阅读更多
<%@ page language="java" pageEncoding="utf-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />

<title>mangpage.jsp</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<table>
<tr>
<td>
学生id
</td>
<td>
学生姓名
</td>
<td>
学生密码
</td>
</tr>
<logic:iterate id="stu" name="pc" property="smallList">
<tr>
<td>
<bean:write name="stu" property="id" />
</td>
<td>
<bean:write name="stu" property="username" />
</td>
<td>
<bean:write name="stu" property="password" />
</td>
</tr>
</logic:iterate>
</table>
<html:link action="/manyPage.do?pageindex=1">首页</html:link>
<logic:equal name="pc" property="firstPage" value="false">
<html:link action="/manyPage.do" paramId="pageindex" paramName="pc"
paramProperty="previousPageCount">上一页</html:link>
</logic:equal>
<logic:equal name="pc" property="lastPage" value="false">
<html:link action="/manyPage.do" paramId="pageindex" paramName="pc"
paramProperty="nextPageCount">下一页</html:link>
</logic:equal>
<html:link action="/manyPage.do1" paramId="pageindex" paramName="pc" paramProperty="allPage">尾页</html:link>
</body>
</html:html>

-----------------------------------------------------------

对应实体类

package com.qxm.pojo;

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private int id;

private String username;

private String password;

public int getId() {
return id;
}

public void setId(int 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;
}

}

------------------------------------------------------------

对应实体类的配置文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="T_suudent" name="com.qxm.pojo.Student">
    <id access="field" name="id">
      <generator class="native"/>
    </id>
    <property name="username" access="field"/>
     <property name="password" access="field"/>
  </class>
</hibernate-mapping>

-----------------------------------------------------------
hibernate的配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/manypage?useUnicode=true&amp;characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="show_sql">true</property>
<mapping resource="com/qxm/pojo/Student.hbm.xml" />

</session-factory>

</hibernate-configuration>
-----------------------------------------------------------

dao层

package com.qxm.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.qxm.pojo.Student;
import com.qxm.utils.HibernateSessionFactory;

@SuppressWarnings("unchecked")
public class ManyPageDao {

public ArrayList<Student> StudentLookUp(){

ArrayList<Student> alist = null;
Session session = HibernateSessionFactory.getSession();
try {
List list = session.createQuery("from com.qxm.pojo.Student").list();
alist = new ArrayList<Student>();
for(Iterator it = list.iterator();it.hasNext();){
Student stu = new Student();
stu = (Student)it.next();
alist.add(stu);
}
} catch (HibernateException e) {
e.printStackTrace();
}finally{
session.close();
}
return alist;
}
}

-----------------------------------------------------------
action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.qxm.struts.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.qxm.dao.ManyPageDao;
import com.qxm.manypage.PageController;
import com.qxm.pojo.Student;

public class ManyPageAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

String currentPage = request.getParameter("pageindex");
if(currentPage == null){
currentPage = "1";
}
int cp = Integer.parseInt(currentPage);
PageController pc = (PageController)request.getAttribute("pc");

if(pc == null){
pc = new PageController();
ManyPageDao dao = new ManyPageDao();
ArrayList<Student> bigList = dao.StudentLookUp();
pc.setBigList(bigList);
request.setAttribute("pc", pc);
}
pc.setCurrentPage(cp);

return mapping.findForward("display_success");
}
}
-----------------------------------------------------------
分页逻辑

package com.qxm.manypage;

import java.util.ArrayList;

import com.qxm.pojo.Student;

@SuppressWarnings("unchecked")
public class PageController {

private ArrayList bigList; // 存储所有数据的集合
private int currentPage = 1; // 当前页数
private int allPage; // 总页数
private int countNumber = 5; // 每页数据的条数
private int allCountNumber; // 数据的总条数
private ArrayList smallList;// 每页数据的小集合
private int previousPageCount;// 上一页的页数
private int nextPageCount;// 下一页的页数
private boolean firstPage;// 是否是第一页
private boolean lastPage;// 是否是最后一页

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
smallList = new ArrayList<Student>();
//上一页
previousPageCount = currentPage-1;
//下一页
nextPageCount = currentPage+1;

if(currentPage == 1){
firstPage = true;
}
else{
firstPage = false;
}
if(currentPage == allPage){
lastPage = true;
}
else{
lastPage = false;
}

for(int i = (currentPage-1)*countNumber;i<currentPage*countNumber&&i<allCountNumber;i++){
smallList.add(bigList.get(i));
}
}
public void setBigList(ArrayList bigList) {
this.bigList = bigList;
allCountNumber = bigList.size();
if(allCountNumber%countNumber==0){
allPage = allCountNumber/countNumber;
}
else{
allPage = allCountNumber/countNumber+1;
}
}

public int getCurrentPage() {
return currentPage;
}

public ArrayList getBigList() {
return bigList;
}


public int getAllPage() {
return allPage;
}

public void setAllPage(int allPage) {
this.allPage = allPage;
}

public int getCountNumber() {
return countNumber;
}

public void setCountNumber(int countNumber) {
this.countNumber = countNumber;
}

public int getAllCountNumber() {
return allCountNumber;
}

public void setAllCountNumber(int allCountNumber) {
this.allCountNumber = allCountNumber;
}

public ArrayList getSmallList() {
return smallList;
}

public void setSmallList(ArrayList smallList) {
this.smallList = smallList;
}

public int getPreviousPageCount() {
return previousPageCount;
}

public void setPreviousPageCount(int previousPageCount) {
this.previousPageCount = previousPageCount;
}

public int getNextPageCount() {
return nextPageCount;
}

public void setNextPageCount(int nextPageCount) {
this.nextPageCount = nextPageCount;
}

public boolean isFirstPage() {
return firstPage;
}

public void setFirstPage(boolean firstPage) {
this.firstPage = firstPage;
}

public boolean isLastPage() {
return lastPage;
}

public void setLastPage(boolean lastPage) {
this.lastPage = lastPage;
}
}

-----------------------------------------------------------

建表类

package com.qxm.utils;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

/**
* @param args
*/
public static void main(String[] args) {

Configuration cfg = new Configuration().configure();

SchemaExport se = new SchemaExport(cfg);

se.create(true, true);

}

}

-----------------------------------------------------------

hibernatesessionfactory
package com.qxm.utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution.  Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses 
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.  
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();   
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

static {
    try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
    }
    private HibernateSessionFactory() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}
-----------------------------------------------------------
log4j
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=d:/oa.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
log4j.logger.com.bjsxt = debug
-----------------------------------------------------------
分享到:
评论

相关推荐

    Struts1.3+Spring4.x+Hibernate4.x框架整合实现对书籍的增删改查(含分页)

    总结来说,"Struts1.3+Spring4.x+Hibernate4.x框架整合实现对书籍的增删改查(含分页)"项目展示了如何使用SSH框架来构建一个功能完善的书籍管理系统。Struts处理用户请求,Spring管理应用组件并提供事务支持,...

    STRUTS2+HIBERNATE详细的分页实现代码详细的分页实现代码

    根据提供的标题、描述、标签及部分内容,我们可以了解到这篇文章主要探讨的是如何在Struts2与Hibernate框架结合下实现分页功能。接下来将详细解析Struts2与Hibernate如何协作完成这一任务。 ### Struts2与Hibernate...

    struts2 + spring 3 + hibernate3.3整合实现图书馆管理管理

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合应用广泛用于构建复杂的Web应用程序,如本例中的图书馆管理系统。这个系统实现了用户登录和注册功能,并且提供了对书籍表的操作,包括增、删、改...

    ext3+struts2+hibernate+spring的CRUD+分页

    "ext3+struts2+hibernate+spring的CRUD+分页"是一个典型的Java Web开发组合,用于实现全面的数据管理功能。这个组合充分利用了各个框架的优势,提供了一个强大的后端数据处理和前端展示的解决方案。 首先,EXT3是一...

    struts2+spring+hibernate分页显示

    1. **前端请求**:用户在页面上选择分页参数,例如当前页和每页条数,通过Ajax或者表单提交给Struts2 Action。 2. **Struts2处理**:Action接收到请求后,根据参数调用Spring管理的服务层方法,传递分页参数。 3. ...

    struts1+hibernate分页

    本文将详细介绍如何在Struts1和Hibernate结合的项目中实现分页功能。 一、Struts1框架简介 Struts1是Apache软件基金会的一个开源项目,它提供了MVC架构来设计Web应用程序。在Struts1中,Action类是业务逻辑的核心,...

    struts2+spring+hibernate分页显示完整代码

    本篇文章将详细讲解如何在基于Struts2、Spring和Hibernate的项目中实现分页功能。 首先,我们从DAO层开始。在`MemberDao`接口中,我们定义了两个关键的方法,一个是用于分页查询,另一个是获取所有记录的数量。这两...

    45-使用Struts + DAO + Hibernate完成分页.rar

    在这个项目"45-使用Struts + DAO + Hibernate完成分页.rar"中,我们将探讨如何整合这三个组件来实现数据库数据的分页显示。 **Struts框架**是MVC(Model-View-Controller)设计模式的一种实现,主要用于处理用户...

    Struts1+Spring+hibernate《最新三国人物分页》

    1. **源代码**:包括Struts1的Action类、Spring的配置文件、Hibernate的实体类以及相关的DAO(数据访问对象)和Service层代码。 2. **配置文件**:如struts-config.xml(Struts1配置)、spring-context.xml(Spring...

    struts2+spring2+hibernate3注册查询搜索分页实例

    总的来说,这个"Struts2+Spring2+Hibernate3注册查询搜索分页实例"是一个很好的学习资源,涵盖了Java Web开发中的基础和核心部分。通过学习这个实例,开发者不仅可以掌握三大框架的基本用法,还能了解到如何将它们...

    学校宿舍管理系统(Struts2+JSP+DAO)

    【学校宿舍管理系统(Struts2+JSP+DAO)】是一个典型的Web应用程序,它结合了Struts2框架、JavaServer Pages(JSP)以及Data Access Object(DAO)模式,旨在高效地管理和维护学校的宿舍资源。这个系统的核心是通过...

    struts2+spring+hibernate+生成报表

    Struts2、Spring和Hibernate是Java企业级开发中常用的三个框架,它们的组合可以构建出高效、稳定且灵活的企业级应用。在这个项目中,我们关注的是如何利用这三大框架生成Excel报表,这对于数据分析、数据导出以及...

    struts2.0+hibernate+spring分页

    在本项目中,“struts2.0+hibernate+spring分页”是将这三种技术结合,实现数据的分页展示功能。分页是一种常见的优化策略,它可以提高用户体验,避免一次性加载过多数据导致页面响应慢或内存压力大。 首先,Struts...

    Struts2,Spring与Hibernate整合应用,学生成绩管理系统

    通过整合Struts2、Spring与Hibernate,学生成绩管理系统不仅实现了基本的登录、学生信息和成绩管理功能,还通过分页技术增强了用户体验。更重要的是,这一整合展示了如何利用现代Java EE框架构建复杂企业级应用的...

    struts+hibernate做的分页显示

    在这个项目中,"struts+hibernate做的分页显示"主要是利用这两者来实现数据的分页展示,提升用户体验,降低服务器压力。 首先,Struts是一个基于MVC设计模式的Java Web框架,它简化了开发过程,提供了处理HTTP请求...

    struts2.0 + hibernate + oracle 分页问题

    在实际开发中,结合Eclipse这样的集成开发环境,我们可以创建项目、配置Struts2和Hibernate的依赖,编写Action、DAO和实体类,并进行数据库连接和配置。在分页实现中,通常会有一个专门的服务或DAO方法,根据用户...

    struts2+spring+hibernate分页查询

    在Struts2、Spring和Hibernate的整合项目中,通常在Service层调用DAO层的方法进行数据库查询,DAO层可以利用Hibernate的Criteria或HQL进行分页操作。Service层处理完分页后,将结果封装成包含数据和分页信息的对象,...

    struts2+hibernate+spring分页

    Service层则调用DAO(数据访问对象),利用Hibernate的查询机制,执行分页查询。 在实际开发中,我们还需要考虑性能优化,例如缓存策略、索引优化、减少数据库查询次数等。对于大数据量的分页,可能还需要采用预...

    struts+hibernate+spring集成实现分页

    2. **创建DAO**:实现分页查询的方法,使用Hibernate的Session或SessionFactory进行数据库操作。 3. **配置Service**:在Spring中声明Service,注入DAO,实现业务逻辑,包括计算总页数、获取当前页数据等。 4. **...

Global site tag (gtag.js) - Google Analytics