Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板博客分类:JavaMySQLHibernateAOPSpringStruts 1、导入29个JAR包  JAR包名称作用Struts2.1.6(7个) struts2-core-2.1.6.jarstruts2开发的核心类库freemarker

1、导入29个JAR包

 

JAR包名称

作用

Struts2.1.67个)

 

struts2-core-2.1.6.jar

struts2开发的核心类库

freemarker-2.3.13.jar

struts2的UI标签的模板使用freemarker编写

commons-logging-1.0.4.jar

ASF出的日志包,支持Log4J和JDK的日志记录

ognl-2.6.11.jar

对象图导航语言,通过它来读写对象属性

xwork-2.1.2.jar

xwork类库,struts2在其上进行构建

commons-fileupload-1.2.1.jar

文件上传组件,2.1.6版本后必须加入此jar包

commons-io-1.3.2.jar

以后文件上传下载需要

 

 

Hibernate3.3.213个)

 

hibernate3.jar

hibernate3开发的核心类库

antlr-2.7.6.jar

解析HQL

commons-collections-3.1.jar

集合框架

dom4j-1.6.1.jar

解析xml

javassist-3.9.0.GA.jar

 

jta-1.1.jar

 

junit-4.8.1.jar

Junit test

ejb3-persistence.jar

@Entity

Hibernate-annotations.jar

 

Hibernate-commons-annotations.jar

 

log4j-1.2.15.jar

是log4j实现类

slf4j-api-1.5.8.jar

标准接口

slf4j-log4j12-1.5.8.jar

是slf4j转换log4j的中间接口

 

 

Spring 2.5.68个)

 

spring.jar

Spring核心文件

common-annotations.jar

IOC支持, 例如@resource

aspectjrt.jar

AOP支持:aspectj运行时候需要的

aspectjweaver.jar 

AOP支持:织入

cglib-nodep-2.1_3.jar

动态生成字节码

Commons-dbcp.jar

数据源      

commons-po ol.jar

数据源                          

struts2-spring-plugin-2.1.6.jar

Struts2和Spring结合所需包

 

 

commons-logging-1.0.4.jar

Struts2加入了就不需要导入

log4j-1.2.15.jar

Hibernate加入了就不需要导入

 

 

数据库包(1个)

 

mysql-connector-java-3.1.10-bin.jar

MySql的驱动程序

 

 

 2、导入框架的配置文件

 

SRC目录下的配置文件

 

log4j.properties

log4j的配置文件,放到SRC根目录下

hibernate.cfg.xml

Hibernate的配置文件

beans.xml

Spring的配置文件

struts.xml

Struts的配置文件

 

 

WEB-INF下配置文件

 

web.xml

Struts2和Spring的结合配置

 

PS:如果需要使用JSTL标签需要导入2个包

    jstl.jar

    standard.jar

 

 

3、建立对应的package和对应的接口与类框架

 

hibernate.cfg.xml

 

Xml代码 复制代码  收藏代码
  1. <?xml version='1.0' encoding='GBK'?>     
  2. <!DOCTYPE hibernate-configuration PUBLIC      
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">     
  5. <hibernate-configuration>     
  6.      
  7.     <session-factory>     
  8.              
  9.         <!-- MySQL数据库连接设置.端口号不用改。只需修改XXXX-->     
  10.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>     
  11.         <property name="connection.url">jdbc:mysql://localhost:3306/ssh</property>     
  12.         <property name="connection.username">root</property>     
  13.         <property name="connection.password">lee</property>     
  14.      
  15.         <!-- 一般不用HIBERNATE的连接池       
  16.         <property name="connection.pool_size">1</property>     
  17.         -->     
  18.      
  19.         <!--  指定用哪种数据库语言,查文档搜索dialect -->     
  20.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>     
  21.         <!--<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> -->     
  22.      
  23.         <!-- 当前SESSION运行环境的上下文getCurrentSession对应的配置-thread线程。JTA是分布式管理,例如多个数据库-->     
  24.         <property name="current_session_context_class">thread</property>     
  25.      
  26.         <!-- Disable the second-level cache  -->     
  27.         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>     
  28.      
  29.         <!-- 打印自动生成的SQL语句-->     
  30.         <property name="show_sql">true</property>     
  31.               
  32.         <!-- 格式化SQL语句      
  33.         <property name="format_sql">true</property>-->     
  34.      
  35.         <!-- 如果不存在表,自动在数据库里生成,常用的是create,update-->     
  36.         <property name="hbm2ddl.auto">update</property>     
  37.      
  38.         <!-- 定义需要映射的路径      
  39.         <mapping class="ssh.model.User"/>     
  40.         -->     
  41.               
  42.     </session-factory>     
  43. </hibernate-configuration>   
<?xml version='1.0' encoding='GBK'?>  <!DOCTYPE hibernate-configuration PUBLIC           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  <hibernate-configuration>        <session-factory>                    <!-- MySQL数据库连接设置.端口号不用改。只需修改XXXX-->          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="connection.url">jdbc:mysql://localhost:3306/ssh</property>          <property name="connection.username">root</property>          <property name="connection.password">lee</property>            <!-- 一般不用HIBERNATE的连接池            <property name="connection.pool_size">1</property>          -->            <!--  指定用哪种数据库语言,查文档搜索dialect -->          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>          <!--<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> -->            <!-- 当前SESSION运行环境的上下文getCurrentSession对应的配置-thread线程。JTA是分布式管理,例如多个数据库-->          <property name="current_session_context_class">thread</property>            <!-- Disable the second-level cache  -->          <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>            <!-- 打印自动生成的SQL语句-->          <property name="show_sql">true</property>                     <!-- 格式化SQL语句           <property name="format_sql">true</property>-->            <!-- 如果不存在表,自动在数据库里生成,常用的是create,update-->          <property name="hbm2ddl.auto">update</property>            <!-- 定义需要映射的路径           <mapping class="ssh.model.User"/>          -->                 </session-factory>  </hibernate-configuration> 

  

 

 

beans:xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="GBK"?>     
  2. <beans xmlns="http://www.springframework.org/schema/beans"     
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  4.     xmlns:context="http://www.springframework.org/schema/context"     
  5.     xmlns:aop="http://www.springframework.org/schema/aop"     
  6.     xmlns:tx="http://www.springframework.org/schema/tx"     
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd      
  9.            http://www.springframework.org/schema/context      
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd      
  11.            http://www.springframework.org/schema/aop      
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd      
  13.            http://www.springframework.org/schema/tx      
  14.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">        
  15.     <!--使用Annotation-->     
  16.     <context:annotation-config />     
  17.           
  18.     <!--自动扫描com.bjsxt包下所有写了@Component的类注入进来-->     
  19.     <context:component-scan base-package="ssh" />     
  20.           
  21.     <!--AOP:扫描上面定义自动扫描的路径com.bjsxt目录下的文件,就可以用@Aspect定义切入类-->     
  22.     <aop:aspectj-autoproxy />     
  23.      
  24.      
  25.     <!--==========================定义数据源==========================-->     
  26.     <bean id="dataSource"    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
  27.     <property name="driverClassName" value="com.mysql.jdbc.Driver" />     
  28.     <property name="url" value="jdbc:mysql://localhost:3306/ssh" />     
  29.     <property name="username" value="root" />     
  30.     <property name="password" value="lee" />     
  31.     </bean>     
  32.      
  33.      
  34.     <!--=======================定义sessionFactory====================-->     
  35.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">     
  36.         <property name="dataSource" ref="dataSource" />     
  37.      
  38.          <!-- 将参数名称设为packagesToScan 可定义扫描目标包下所有实体类,告诉sessionFactory哪些类被Entity和注解了 -->     
  39.          <property name="packagesToScan">     
  40.             <list>     
  41.                 <value>ssh.model</value>     
  42.             </list>     
  43.          </property>     
  44.      
  45.         <!--直接导入Hibernate的配置文件。这样才能自动建立数据库表和显示DDL语句。如果上面的配置只能先手动建立数据表然后更新。不能自动创建表-->     
  46.         <property name="configLocation" value="classpath:hibernate.cfg.xml"/>     
  47.     </bean>     
  48.           
  49.     <!--====================定义Hibernate事务管理器===================-->     
  50.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     
  51.         <property name="sessionFactory" ref="sessionFactory" />     
  52.     </bean>     
  53.           
  54.     <!--<aop:advisor 是定义事务的建议,pointcut-ref是上面pointcut的ID,advice-ref是单独的advice,是下面定义的 <tx:advice id="txAdvice" -->     
  55.     <aop:config>     
  56.         <aop:pointcut id="myServiceMethod" expression="execution(public * ssh.service..*.*(..))"  />     
  57.         <aop:advisor pointcut-ref="myServiceMethod" advice-ref="txAdvice"/>     
  58.     </aop:config>     
  59.           
  60.     <!--定义建议(事务),供<aop:advisor的advice-ref引用,transaction-manager=" txManager "引用的是上面定义的事务管理器<bean id="txManager"。   propagation="REQUIRED"可以省略,默认是这个-->     
  61.     <tx:advice id="txAdvice" transaction-manager="txManager">     
  62.         <tx:attributes>     
  63.             <tx:method name="add*" propagation="REQUIRED"/>     
  64.             <!--    
  65.             <tx:method name="exists" read-only="true" />    
  66.             -->     
  67.         </tx:attributes>     
  68.     </tx:advice>     
  69.      
  70.      
  71.     <!--=========================定义HibernateTemplate===================-->     
  72.     <!--定义后在DAO里注入HibernateTemplate(private HibernateTemplate hibernateTemplate;在其Set方法上加@Resource注入),hibernateTemplate里面直接由save、update、delete、load等方法,可以直接hibernateTemplate.save(testUser);-->     
  73.      
  74.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">     
  75.         <property name="sessionFactory" ref="sessionFactory"></property>     
  76.     </bean>     
  77.      
  78. </beans>   
<?xml version="1.0" encoding="GBK"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:tx="http://www.springframework.org/schema/tx"      xsi:schemaLocation="http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd              http://www.springframework.org/schema/context              http://www.springframework.org/schema/context/spring-context-2.5.xsd              http://www.springframework.org/schema/aop              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd              http://www.springframework.org/schema/tx              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">         <!--使用Annotation-->      <context:annotation-config />             <!--自动扫描com.bjsxt包下所有写了@Component的类注入进来-->      <context:component-scan base-package="ssh" />             <!--AOP:扫描上面定义自动扫描的路径com.bjsxt目录下的文件,就可以用@Aspect定义切入类-->      <aop:aspectj-autoproxy />          <!--==========================定义数据源==========================-->      <bean id="dataSource"    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">      <property name="driverClassName" value="com.mysql.jdbc.Driver" />      <property name="url" value="jdbc:mysql://localhost:3306/ssh" />      <property name="username" value="root" />      <property name="password" value="lee" />      </bean>          <!--=======================定义sessionFactory====================-->      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">          <property name="dataSource" ref="dataSource" />             <!-- 将参数名称设为packagesToScan 可定义扫描目标包下所有实体类,告诉sessionFactory哪些类被Entity和注解了 -->           <property name="packagesToScan">              <list>                  <value>ssh.model</value>              </list>           </property>            <!--直接导入Hibernate的配置文件。这样才能自动建立数据库表和显示DDL语句。如果上面的配置只能先手动建立数据表然后更新。不能自动创建表-->          <property name="configLocation" value="classpath:hibernate.cfg.xml"/>      </bean>             <!--====================定义Hibernate事务管理器===================-->      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">          <property name="sessionFactory" ref="sessionFactory" />      </bean>             <!--<aop:advisor 是定义事务的建议,pointcut-ref是上面pointcut的ID,advice-ref是单独的advice,是下面定义的 <tx:advice id="txAdvice" -->      <aop:config>          <aop:pointcut id="myServiceMethod" expression="execution(public * ssh.service..*.*(..))"  />          <aop:advisor pointcut-ref="myServiceMethod" advice-ref="txAdvice"/>      </aop:config>             <!--定义建议(事务),供<aop:advisor的advice-ref引用,transaction-manager=" txManager "引用的是上面定义的事务管理器<bean id="txManager"。   propagation="REQUIRED"可以省略,默认是这个-->      <tx:advice id="txAdvice" transaction-manager="txManager">          <tx:attributes>              <tx:method name="add*" propagation="REQUIRED"/>              <!--              <tx:method name="exists" read-only="true" />              -->          </tx:attributes>      </tx:advice>          <!--=========================定义HibernateTemplate===================-->      <!--定义后在DAO里注入HibernateTemplate(private HibernateTemplate hibernateTemplate;在其Set方法上加@Resource注入),hibernateTemplate里面直接由save、update、delete、load等方法,可以直接hibernateTemplate.save(testUser);-->        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">          <property name="sessionFactory" ref="sessionFactory"></property>      </bean>    </beans> 

  

 

struts.xml

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="GBK" ?>     
  2. <!DOCTYPE struts PUBLIC      
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">     
  5.      
  6. <struts>     
  7.      <constant name="struts.devMode" value="true" />                        
  8.      <package name="default" namespace="/" extends="struts-default">  
  9.         <action name="index" class="mstx.action.UserAction">     
  10.             <result name="success">/index.jsp</result>                        
  11.         </action>     
  12.         <action name="user" class="mstx.action.UserAction" method="save">     
  13.             <result name="success">/success.jsp</result>                         
  14.         </action>     
  15.               
  16.     </package>     
  17.      
  18. </struts>    
<?xml version="1.0" encoding="GBK" ?>  <!DOCTYPE struts PUBLIC       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"       "http://struts.apache.org/dtds/struts-2.0.dtd">    <struts>       <constant name="struts.devMode" value="true" />                          <package name="default" namespace="/" extends="struts-default">        <action name="index" class="mstx.action.UserAction">              <result name="success">/index.jsp</result>                             </action>          <action name="user" class="mstx.action.UserAction" method="save">              <result name="success">/success.jsp</result>                              </action>                 </package>    </struts>  

 

 

web.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>      
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"     
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">      
  6.     <welcome-file-list>      
  7.         <welcome-file>index.jsp</welcome-file>      
  8.     </welcome-file-list>      
  9.      
  10.     <listener>      
  11.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
  12.         <!-- default: /WEB-INF/applicationContext.xml -->      
  13.     </listener>      
  14.      
  15.     <context-param>      
  16.         <param-name>contextConfigLocation</param-name>      
  17.         <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->      
  18.         <param-value>classpath:beans.xml</param-value>      
  19.     </context-param>      
  20.           
  21.           
  22.     <!--中文问题      
  23.         1.  Struts2.1.8已经修正,只需要改Struts.xml的i18n.encoding = gbk      
  24.         2.  使用spring的characterencoding      
  25.     -->      
  26.     <filter>      
  27.         <filter-name>encodingFilter</filter-name>      
  28.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      
  29.         <init-param>      
  30.             <param-name>encoding</param-name>      
  31.             <param-value>GBK</param-value>      
  32.         </init-param>      
  33.     </filter>      
  34.           
  35.     <filter-mapping>      
  36.         <filter-name>encodingFilter</filter-name>      
  37.         <url-pattern>/*</url-pattern>      
  38.     </filter-mapping>      
  39.      
  40.      
  41.     <!--openSessionInView      
  42.         此时,session(应该说的是Hibernate的session)在事物结束(通常是service调用完)后自动关闭。由于使用的是load获取数据,在jsp页面申请取得数据时才真正的执行sql,而此时session已经关闭,故报错。      
  43.         Session关闭解决方法:      
  44.         在web.xml中增加filter—openSessionInView,用于延长session在jsp调用完后再关闭      
  45.      
  46.     -->      
  47.     <filter>      
  48.         <filter-name>openSessionInView</filter-name>      
  49.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>      
  50.     </filter>      
  51.           
  52.     <filter-mapping>      
  53.         <filter-name>openSessionInView</filter-name>      
  54.         <url-pattern>/*</url-pattern>      
  55.     </filter-mapping>      
  56.           
  57.     <filter>      
  58.         <filter-name>struts2</filter-name>      
  59.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>      
  60.     </filter>      
  61.      
  62.     <filter-mapping>      
  63.         <filter-name>struts2</filter-name>      
  64.         <url-pattern>/*</url-pattern>      
  65.     </filter-mapping>      
  66.           
  67. </web-app>   
<?xml version="1.0" encoding="UTF-8"?>   <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">       <welcome-file-list>           <welcome-file>index.jsp</welcome-file>       </welcome-file-list>         <listener>           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>           <!-- default: /WEB-INF/applicationContext.xml -->       </listener>         <context-param>           <param-name>contextConfigLocation</param-name>           <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->           <param-value>classpath:beans.xml</param-value>       </context-param>                     <!--中文问题           1.  Struts2.1.8已经修正,只需要改Struts.xml的i18n.encoding = gbk           2.  使用spring的characterencoding       -->       <filter>           <filter-name>encodingFilter</filter-name>           <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>           <init-param>               <param-name>encoding</param-name>               <param-value>GBK</param-value>           </init-param>       </filter>              <filter-mapping>           <filter-name>encodingFilter</filter-name>           <url-pattern>/*</url-pattern>       </filter-mapping>           <!--openSessionInView           此时,session(应该说的是Hibernate的session)在事物结束(通常是service调用完)后自动关闭。由于使用的是load获取数据,在jsp页面申请取得数据时才真正的执行sql,而此时session已经关闭,故报错。           Session关闭解决方法:           在web.xml中增加filter—openSessionInView,用于延长session在jsp调用完后再关闭         -->       <filter>           <filter-name>openSessionInView</filter-name>           <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>       </filter>              <filter-mapping>           <filter-name>openSessionInView</filter-name>           <url-pattern>/*</url-pattern>       </filter-mapping>              <filter>           <filter-name>struts2</filter-name>           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>       </filter>         <filter-mapping>           <filter-name>struts2</filter-name>           <url-pattern>/*</url-pattern>       </filter-mapping>          </web-app> 

  

 

 

4、建立实体类,并加入Hibernate的注解

  1)在实体类上加相应注解@Entity @Id等

  2)在字段属性的get方法上加--@Column(name = "表字段名")

 

User.java

 

 

5、定义工具类

 

1)定义查询返回结果

 

QueryResult.java

Java代码 复制代码  收藏代码
  1. package ssh.utils;   
  2.   
  3. import java.util.List;   
  4.   
  5. /*  
  6.  * 定义查询返回的结果,泛型定义在类上  
  7.  */  
  8. public class QueryResult<T> {   
  9.     private List<T> resultlist;       //记录查询的结果   
  10.     private long totalrecord;       //记录查询得到的总条数   
  11.        
  12.     public List<T> getResultlist() {   
  13.         return resultlist;   
  14.     }   
  15.     public void setResultlist(List<T> resultlist) {   
  16.         this.resultlist = resultlist;   
  17.     }   
  18.     public long getTotalrecord() {   
  19.         return totalrecord;   
  20.     }   
  21.     public void setTotalrecord(long totalrecord) {   
  22.         this.totalrecord = totalrecord;   
  23.     }   
  24. }  
package ssh.utils;import java.util.List;/* * 定义查询返回的结果,泛型定义在类上 */public class QueryResult<T> {	private List<T> resultlist;		//记录查询的结果	private long totalrecord;		//记录查询得到的总条数		public List<T> getResultlist() {		return resultlist;	}	public void setResultlist(List<T> resultlist) {		this.resultlist = resultlist;	}	public long getTotalrecord() {		return totalrecord;	}	public void setTotalrecord(long totalrecord) {		this.totalrecord = totalrecord;	}}

  

 

2)定义分页工具类

 

PageView.java

Java代码 复制代码  收藏代码
  1. package ssh.utils;   
  2.   
  3. import java.util.List;   
  4.   
  5. /**  
  6.  *  在Action里的调用方法  
  7.     //这里必须要构造新对象,不然刚打开没有currentPage参数传递过来,如果不新建也行,第一次打开必须传递currentPage参数过来  
  8.     private PageView<T>pageView=new PageView<T>();    
  9.       
  10.     public PageView<T> getPageView() {  
  11.         return pageView;  
  12.     }  
  13.  
  14.     public void setPageView(PageView<T> pageView) {  
  15.         this.pageView = pageView;  
  16.     }  
  17.     int maxresult=1;                  
  18.     int firstindex=(pageView.getCurrentPage()-1)*maxresult;  
  19.     QueryResult<T> Service.getScrollData(firstindex,maxresult, null, null, null);  
  20.     pageView.setQueryResult(maxresult,qr);  
  21.     request.put("pageView", pageView);  
  22.  */  
  23.   
  24. public class PageView<T> {   
  25.     /** 分页数据 **/  
  26.     private List<T> records;   
  27.   
  28.     /** 页码开始索引 ,例如显示第1 2 3 4 5 6 7 8 9 ,开始索引为1 **/  
  29.     private long startIndex;   
  30.   
  31.     /** 页码结束索引 ,例如显示第1 2 3 4 5 6 7 8 9 ,结束索引为9 **/  
  32.     private long endIndex;   
  33.   
  34.     /** 总页数 ,没有0页,所以设置默认值为1 **/  
  35.     private long totalPage = 1;   
  36.   
  37.     /** 每页显示记录数 **/  
  38.     private int maxResult = 10;   
  39.   
  40.     /** 当前页 **/  
  41.     private int currentPage = 1;   
  42.   
  43.     /** 总记录数 **/  
  44.     private long totalRecord;   
  45.   
  46.     /** 工具条上显示的页码数量 **/  
  47.     private int pageBarSize = 8;   
  48.   
  49.        
  50.     // 这只方法触发记录查询结果和总条数   
  51.     public void setQueryResult(int maxResult,QueryResult<T> qr) {   
  52.         this.maxResult = maxResult;   
  53.         this.records = qr.getResultlist();   
  54.         this.totalRecord = qr.getTotalrecord();   
  55.         this.totalPage = this.totalRecord % this.maxResult == 0 ? this.totalRecord/ this.maxResult : this.totalRecord / this.maxResult + 1;   
  56.            
  57.         /*****************************************************/  
  58.         this.startIndex = currentPage - (pageBarSize % 2 == 0 ? pageBarSize / 2 - 1 : pageBarSize / 2);   
  59.         this.endIndex = currentPage + pageBarSize / 2;   
  60.   
  61.         if (startIndex < 1) {   
  62.             startIndex = 1;   
  63.             if (totalPage >= pageBarSize)   
  64.                 endIndex = pageBarSize;   
  65.             else  
  66.                 endIndex = totalPage;   
  67.         }   
  68.         if (endIndex > totalPage) {   
  69.             endIndex = totalPage;   
  70.             if ((endIndex - pageBarSize) > 0)   
  71.                 startIndex = endIndex - pageBarSize +1; //最后一页显示多小条页   
  72.             else  
  73.                 startIndex = 1;   
  74.         }   
  75.     }   
  76.   
  77.     public List<T> getRecords() {   
  78.         return records;   
  79.     }   
  80.   
  81.     public void setRecords(List<T> records) {   
  82.         this.records = records;   
  83.     }   
  84.   
  85.     public long getStartIndex() {   
  86.         return startIndex;   
  87.     }   
  88.   
  89.     public void setStartIndex(long startIndex) {   
  90.         this.startIndex = startIndex;   
  91.     }   
  92.   
  93.     public long getEndIndex() {   
  94.         return endIndex;   
  95.     }   
  96.   
  97.     public void setEndIndex(long endIndex) {   
  98.         this.endIndex = endIndex;   
  99.     }   
  100.   
  101.     public long getTotalPage() {   
  102.         return totalPage;   
  103.     }   
  104.   
  105.     public void setTotalPage(long totalPage) {   
  106.         this.totalPage = totalPage;   
  107.     }   
  108.   
  109.     public int getMaxResult() {   
  110.         return maxResult;   
  111.     }   
  112.   
  113.     public void setMaxResult(int maxResult) {   
  114.         this.maxResult = maxResult;   
  115.     }   
  116.   
  117.     public int getCurrentPage() {   
  118.         return currentPage;   
  119.     }   
  120.   
  121.     public void setCurrentPage(int currentPage) {   
  122.         this.currentPage = currentPage<1?1:currentPage;  //如果当前页为0,则显示第一页   
  123.     }   
  124.   
  125.     public long getTotalRecord() {   
  126.         return totalRecord;   
  127.     }   
  128.   
  129.     public void setTotalRecord(long totalRecord) {   
  130.         this.totalRecord = totalRecord;   
  131.     }   
  132.   
  133.     public int getPageBarSize() {   
  134.         return pageBarSize;   
  135.     }   
  136.   
  137.     public void setPageBarSize(int pageBarSize) {   
  138.         this.pageBarSize = pageBarSize;   
  139.     }   
  140. }  
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
11 0
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
12 0
|
30天前
|
Shell
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
11 0
|
30天前
|
Shell
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
13 0
|
25天前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
21 1
|
1天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
2天前
|
SQL 关系型数据库 MySQL
排序与分页——“MySQL数据库”
排序与分页——“MySQL数据库”
|
11天前
|
SQL 关系型数据库 MySQL
mysql基本查询、运算符、排序和分页
mysql基本查询、运算符、排序和分页
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
8 0
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
7 0