生活随笔
收集整理的這篇文章主要介紹了
Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分頁(yè)模板
MySQLHibernateAOPSpringStruts?
1、導(dǎo)入29個(gè)JAR包
?
JAR包名稱 | 作用 |
Struts2.1.6(7個(gè)) | ? |
struts2-core-2.1.6.jar | struts2開(kāi)發(fā)的核心類庫(kù) |
freemarker-2.3.13.jar | struts2的UI標(biāo)簽的模板使用freemarker編寫(xiě) |
commons-logging-1.0.4.jar | ASF出的日志包,支持Log4J和JDK的日志記錄 |
ognl-2.6.11.jar | 對(duì)象圖導(dǎo)航語(yǔ)言,通過(guò)它來(lái)讀寫(xiě)對(duì)象屬性 |
xwork-2.1.2.jar | xwork類庫(kù),struts2在其上進(jìn)行構(gòu)建 |
commons-fileupload-1.2.1.jar | 文件上傳組件,2.1.6版本后必須加入此jar包 |
commons-io-1.3.2.jar | 以后文件上傳下載需要 |
? | ? |
Hibernate3.3.2(13個(gè)) | ? |
hibernate3.jar | hibernate3開(kāi)發(fā)的核心類庫(kù) |
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實(shí)現(xiàn)類 |
slf4j-api-1.5.8.jar | 標(biāo)準(zhǔn)接口 |
slf4j-log4j12-1.5.8.jar | 是slf4j轉(zhuǎn)換log4j的中間接口 |
? | ? |
Spring 2.5.6(8個(gè)) | ? |
spring.jar | Spring核心文件 |
common-annotations.jar | IOC支持, 例如@resource |
aspectjrt.jar | AOP支持:aspectj運(yùn)行時(shí)候需要的 |
aspectjweaver.jar? | AOP支持:織入 |
cglib-nodep-2.1_3.jar | 動(dòng)態(tài)生成字節(jié)碼 |
Commons-dbcp.jar | 數(shù)據(jù)源?????? |
commons-po ol.jar | 數(shù)據(jù)源?????????????????????????? |
struts2-spring-plugin-2.1.6.jar | Struts2和Spring結(jié)合所需包 |
? | ? |
commons-logging-1.0.4.jar | Struts2加入了就不需要導(dǎo)入 |
log4j-1.2.15.jar | Hibernate加入了就不需要導(dǎo)入 |
? | ? |
數(shù)據(jù)庫(kù)包(1個(gè)) | ? |
mysql-connector-java-3.1.10-bin.jar | MySql的驅(qū)動(dòng)程序 ? |
?
?2、導(dǎo)入框架的配置文件
?
SRC目錄下的配置文件 | ? |
log4j.properties | log4j的配置文件,放到SRC根目錄下 |
hibernate.cfg.xml | Hibernate的配置文件 |
beans.xml | Spring的配置文件 |
struts.xml | Struts的配置文件 |
? | ? |
WEB-INF下配置文件 | ? |
web.xml | Struts2和Spring的結(jié)合配置 |
?
PS:如果需要使用JSTL標(biāo)簽需要導(dǎo)入2個(gè)包
??? jstl.jar
??? standard.jar
?
?
3、建立對(duì)應(yīng)的package和對(duì)應(yīng)的接口與類框架
?
hibernate.cfg.xml
?
Xml代碼 ?
<?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數(shù)據(jù)庫(kù)連接設(shè)置.端口號(hào)不用改。只需修改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>?? ??????????-->?? ???? ??????????<!--??指定用哪種數(shù)據(jù)庫(kù)語(yǔ)言,查文檔搜索dialect?-->?? ??????????<property?name="dialect">org.hibernate.dialect.MySQLDialect</property>?? ??????????<!--<property?name="dialect">org.hibernate.dialect.SQLServerDialect</property>?-->?? ???? ??????????<!--?當(dāng)前SESSION運(yùn)行環(huán)境的上下文getCurrentSession對(duì)應(yīng)的配置-thread線程。JTA是分布式管理,例如多個(gè)數(shù)據(jù)庫(kù)-->?? ??????????<property?name="current_session_context_class">thread</property>?? ???? ??????????<!--?Disable?the?second-level?cache??-->?? ??????????<property?name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>?? ???? ??????????<!--?打印自動(dòng)生成的SQL語(yǔ)句-->?? ??????????<property?name="show_sql">true</property>?? ????????????? ??????????<!--?格式化SQL語(yǔ)句??? ??????????<property?name="format_sql">true</property>-->?? ???? ??????????<!--?如果不存在表,自動(dòng)在數(shù)據(jù)庫(kù)里生成,常用的是create,update-->?? ??????????<property?name="hbm2ddl.auto">update</property>?? ???? ??????????<!--?定義需要映射的路徑??? ??????????<mapping?class="ssh.model.User"/>?? ??????????-->?? ????????????? ??????</session-factory>?? ??</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數(shù)據(jù)庫(kù)連接設(shè)置.端口號(hào)不用改。只需修改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> --> <!-- 指定用哪種數(shù)據(jù)庫(kù)語(yǔ)言,查文檔搜索dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> --> <!-- 當(dāng)前SESSION運(yùn)行環(huán)境的上下文getCurrentSession對(duì)應(yīng)的配置-thread線程。JTA是分布式管理,例如多個(gè)數(shù)據(jù)庫(kù)--> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- 打印自動(dòng)生成的SQL語(yǔ)句--> <property name="show_sql">true</property> <!-- 格式化SQL語(yǔ)句 <property name="format_sql">true</property>--> <!-- 如果不存在表,自動(dòng)在數(shù)據(jù)庫(kù)里生成,常用的是create,update--> <property name="hbm2ddl.auto">update</property> <!-- 定義需要映射的路徑 <mapping class="ssh.model.User"/> --> </session-factory> </hibernate-configuration>
??
?
?
beans:xml
Xml代碼 ?
<?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?/>?? ????????? ??????<!--自動(dòng)掃描com.bjsxt包下所有寫(xiě)了@Component的類注入進(jìn)來(lái)-->?? ??????<context:component-scan?base-package="ssh"?/>?? ????????? ??????<!--AOP:掃描上面定義自動(dòng)掃描的路徑com.bjsxt目錄下的文件,就可以用@Aspect定義切入類-->?? ??????<aop:aspectj-autoproxy?/>?? ???? ???? ??????<!--==========================定義數(shù)據(jù)源==========================-->?? ??????<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"?/>?? ???? ???????????<!--?將參數(shù)名稱設(shè)為packagesToScan?可定義掃描目標(biāo)包下所有實(shí)體類,告訴sessionFactory哪些類被Entity和注解了?-->?? ???????????<property?name="packagesToScan">?? ??????????????<list>?? ??????????????????<value>ssh.model</value>?? ??????????????</list>?? ???????????</property>?? ???? ??????????<!--直接導(dǎo)入Hibernate的配置文件。這樣才能自動(dòng)建立數(shù)據(jù)庫(kù)表和顯示DDL語(yǔ)句。如果上面的配置只能先手動(dòng)建立數(shù)據(jù)表然后更新。不能自動(dòng)創(chuàng)建表-->?? ??????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"/>?? ??????</bean>?? ????????? ??????<!--====================定義Hibernate事務(wù)管理器===================-->?? ??????<bean?id="txManager"?class="org.springframework.orm.hibernate3.HibernateTransactionManager">?? ??????????<property?name="sessionFactory"?ref="sessionFactory"?/>?? ??????</bean>?? ????????? ??????<!--<aop:advisor?是定義事務(wù)的建議,pointcut-ref是上面pointcut的ID,advice-ref是單獨(dú)的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>?? ????????? ??????<!--定義建議(事務(wù)),供<aop:advisor的advice-ref引用,transaction-manager="?txManager?"引用的是上面定義的事務(wù)管理器<bean?id="txManager"。???propagation="REQUIRED"可以省略,默認(rèn)是這個(gè)-->?? ??????<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>???<?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 /> <!--自動(dòng)掃描com.bjsxt包下所有寫(xiě)了@Component的類注入進(jìn)來(lái)--> <context:component-scan base-package="ssh" /> <!--AOP:掃描上面定義自動(dòng)掃描的路徑com.bjsxt目錄下的文件,就可以用@Aspect定義切入類--> <aop:aspectj-autoproxy /> <!--==========================定義數(shù)據(jù)源==========================--> <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" /> <!-- 將參數(shù)名稱設(shè)為packagesToScan 可定義掃描目標(biāo)包下所有實(shí)體類,告訴sessionFactory哪些類被Entity和注解了 --> <property name="packagesToScan"> <list> <value>ssh.model</value> </list> </property> <!--直接導(dǎo)入Hibernate的配置文件。這樣才能自動(dòng)建立數(shù)據(jù)庫(kù)表和顯示DDL語(yǔ)句。如果上面的配置只能先手動(dòng)建立數(shù)據(jù)表然后更新。不能自動(dòng)創(chuàng)建表--> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> <!--====================定義Hibernate事務(wù)管理器===================--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--<aop:advisor 是定義事務(wù)的建議,pointcut-ref是上面pointcut的ID,advice-ref是單獨(dú)的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> <!--定義建議(事務(wù)),供<aop:advisor的advice-ref引用,transaction-manager=" txManager "引用的是上面定義的事務(wù)管理器<bean id="txManager"。 propagation="REQUIRED"可以省略,默認(rèn)是這個(gè)--> <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代碼 ?
<?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>????<?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代碼 ?
<?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>??? ????????? ????????? ??????<!--中文問(wèn)題??? ??????????1.??Struts2.1.8已經(jīng)修正,只需要改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??? ??????????此時(shí),session(應(yīng)該說(shuō)的是Hibernate的session)在事物結(jié)束(通常是service調(diào)用完)后自動(dòng)關(guān)閉。由于使用的是load獲取數(shù)據(jù),在jsp頁(yè)面申請(qǐng)取得數(shù)據(jù)時(shí)才真正的執(zhí)行sql,而此時(shí)session已經(jīng)關(guān)閉,故報(bào)錯(cuò)。??? ??????????Session關(guān)閉解決方法:??? ??????????在web.xml中增加filter—openSessionInView,用于延長(zhǎng)session在jsp調(diào)用完后再關(guān)閉??? ???? ??????-->??? ??????<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>???<?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> <!--中文問(wèn)題 1. Struts2.1.8已經(jīng)修正,只需要改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 此時(shí),session(應(yīng)該說(shuō)的是Hibernate的session)在事物結(jié)束(通常是service調(diào)用完)后自動(dòng)關(guān)閉。由于使用的是load獲取數(shù)據(jù),在jsp頁(yè)面申請(qǐng)取得數(shù)據(jù)時(shí)才真正的執(zhí)行sql,而此時(shí)session已經(jīng)關(guān)閉,故報(bào)錯(cuò)。 Session關(guān)閉解決方法: 在web.xml中增加filter—openSessionInView,用于延長(zhǎng)session在jsp調(diào)用完后再關(guān)閉 --> <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、建立實(shí)體類,并加入Hibernate的注解
1)在實(shí)體類上加相應(yīng)注解@Entity @Id等
2)在字段屬性的get方法上加--@Column(name = "表字段名")
?
User.java
?
?
5、定義工具類
?
1)定義查詢返回結(jié)果
?
QueryResult.java
Java代碼 ?
package?ssh.utils; ????import?java.util.List; ????/* ??*?定義查詢返回的結(jié)果,泛型定義在類上 ??*/??public?class?QueryResult<T>?{ ??????private?List<T>?resultlist;???????//記錄查詢的結(jié)果 ??????private?long?totalrecord;???????//記錄查詢得到的總條數(shù) ?????? ??????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; ??????} ??}??package ssh.utils;import java.util.List;/* * 定義查詢返回的結(jié)果,泛型定義在類上 */public class QueryResult<T> { private List<T> resultlist; //記錄查詢的結(jié)果 private long totalrecord; //記錄查詢得到的總條數(shù) 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)定義分頁(yè)工具類
?
PageView.java
Java代碼 ?
package?ssh.utils; ????import?java.util.List; ????/** ??*??在Action里的調(diào)用方法 ?????//這里必須要構(gòu)造新對(duì)象,不然剛打開(kāi)沒(méi)有currentPage參數(shù)傳遞過(guò)來(lái),如果不新建也行,第一次打開(kāi)必須傳遞currentPage參數(shù)過(guò)來(lái) ?????private?PageView<T>pageView=new?PageView<T>();?? ????? ?????public?PageView<T>?getPageView()?{ ?????????return?pageView; ?????} ??????public?void?setPageView(PageView<T>?pageView)?{ ?????????this.pageView?=?pageView; ?????} ?????int?maxresult=1;???????????????? ?????int?firstindex=(pageView.getCurrentPage()-1)*maxresult; ?????QueryResult<T>?Service.getScrollData(firstindex,maxresult,?null,?null,?null); ?????pageView.setQueryResult(maxresult,qr); ?????request.put("pageView",?pageView); ??*/????public?class?PageView<T>?{ ??????/**?分頁(yè)數(shù)據(jù)?**/??????private?List<T>?records; ????????/**?頁(yè)碼開(kāi)始索引?,例如顯示第1?2?3?4?5?6?7?8?9?,開(kāi)始索引為1?**/??????private?long?startIndex; ????????/**?頁(yè)碼結(jié)束索引?,例如顯示第1?2?3?4?5?6?7?8?9?,結(jié)束索引為9?**/??????private?long?endIndex; ????????/**?總頁(yè)數(shù)?,沒(méi)有0頁(yè),所以設(shè)置默認(rèn)值為1?**/??????private?long?totalPage?=?1; ????????/**?每頁(yè)顯示記錄數(shù)?**/??????private?int?maxResult?=?10; ????????/**?當(dāng)前頁(yè)?**/??????private?int?currentPage?=?1; ????????/**?總記錄數(shù)?**/??????private?long?totalRecord; ????????/**?工具條上顯示的頁(yè)碼數(shù)量?**/??????private?int?pageBarSize?=?8; ???????? ??????//?這只方法觸發(fā)記錄查詢結(jié)果和總條數(shù) ??????public?void?setQueryResult(int?maxResult,QueryResult<T>?qr)?{ ??????????this.maxResult?=?maxResult; ??????????this.records?=?qr.getResultlist(); ??????????this.totalRecord?=?qr.getTotalrecord(); ??????????this.totalPage?=?this.totalRecord?%?this.maxResult?==?0???this.totalRecord/?this.maxResult?:?this.totalRecord?/?this.maxResult?+?1; ?????????? ??????????/*****************************************************/??????????this.startIndex?=?currentPage?-?(pageBarSize?%?2?==?0???pageBarSize?/?2?-?1?:?pageBarSize?/?2); ??????????this.endIndex?=?currentPage?+?pageBarSize?/?2; ????????????if?(startIndex?<?1)?{ ??????????????startIndex?=?1; ??????????????if?(totalPage?>=?pageBarSize) ??????????????????endIndex?=?pageBarSize; ??????????????else??????????????????endIndex?=?totalPage; ??????????} ??????????if?(endIndex?>?totalPage)?{ ??????????????endIndex?=?totalPage; ??????????????if?((endIndex?-?pageBarSize)?>?0) ??????????????????startIndex?=?endIndex?-?pageBarSize?+1;?//最后一頁(yè)顯示多小條頁(yè) ??????????????else??????????????????startIndex?=?1; ??????????} ??????} ????????public?List<T>?getRecords()?{ ??????????return?records; ??????} ????????public?void?setRecords(List<T>?records)?{ ??????????this.records?=?records; ??????} ????????public?long?getStartIndex()?{ ??????????return?startIndex; ??????} ????????public?void?setStartIndex(long?startIndex)?{ ??????????this.startIndex?=?startIndex; ??????} ????????public?long?getEndIndex()?{ ??????????return?endIndex; ??????} ????????public?void?setEndIndex(long?endIndex)?{ ??????????this.endIndex?=?endIndex; ??????} ????????public?long?getTotalPage()?{ ??????????return?totalPage; ??????} ????????public?void?setTotalPage(long?totalPage)?{ ??????????this.totalPage?=?totalPage; ??????} ????????public?int?getMaxResult()?{ ??????????return?maxResult; ??????} ????????public?void?setMaxResult(int?maxResult)?{ ??????????this.maxResult?=?maxResult; ??????} ????????public?int?getCurrentPage()?{ ??????????return?currentPage; ??????} ????????public?void?setCurrentPage(int?currentPage)?{ ??????????this.currentPage?=?currentPage<1?1:currentPage;??//如果當(dāng)前頁(yè)為0,則顯示第一頁(yè) ??????} ????????public?long?getTotalRecord()?{ ??????????return?totalRecord; ??????} ????????public?void?setTotalRecord(long?totalRecord)?{ ??????????this.totalRecord?=?totalRecord; ??????} ????????public?int?getPageBarSize()?{ ??????????return?pageBarSize; ??????} ????????public?void?setPageBarSize(int?pageBarSize)?{ ??????????this.pageBarSize?=?pageBarSize; ??????} ??}??
總結(jié)
以上是生活随笔為你收集整理的Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。