說明:本實例通過Struts2+Spring+Hibernate三個框架的整合實現了對數據庫信息最基本的CRUD操作,在前端頁面用jQuery進行信息的展示實現基本功能。
開發環境MyEclipse8.5+Tomcat6.0+Mysql5.1+jdk1.6

 

其他工具版本:Struts2.1.8.1,Spring2.5,Hibernate3.3,Jquery1.5(jquery.validate.js等);有些工具版本會稍微升級,不會對整體項目有大的影響。
前端頁面:Xhtml+css

另:頁面,數據庫和xml文件皆用UTF-8編碼。

環境搭建

1、打開myeclipse,新建web project輸入項目名稱,項目名稱為DyEnigma

2、先把spring整合進來,因為以后的hibernate要用到它的配置文件。我把spring的配置文件命名為spring.xml并把它放在了WEB-INF文件夾中,spring.xml文件內部的配置模板代碼如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7            http://www.springframework.org/schema/context
 8            http://www.springframework.org/schema/context/spring-context-2.5.xsd
 9            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
10            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
11 </beans>

然后導入spring依賴包,這里簡單的把名稱羅列出來,aspectjrt.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar、common-annotations.jar、commons-logging.jar、spring.jar、log4j-1.2.15.jar,另外使用c3p0建立連接池,還要加入c3p0-0.9.1.2.jar包;這里spring采用掃描加注解的方式管理bean,在配置文件中加入代碼
<context:component-scan base-package="cn.dy" />
以后建立的action,實體類,dao和service全部都會在cn.dy下面,以讓spring根據各自的注解自動管理。

3、在spring配置文件里面配置數據源以及整合進hibernate,代碼如下

 1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 2         destroy-method="close">
 3         <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
 4         <property name="jdbcUrl"
 5             value="jdbc:mysql://localhost:3306/learn?useUnicode=true&amp;characterEncoding=UTF-8" />
 6         <property name="user" value="root" />
 7         <property name="password" value="123456" />
 8         <!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
 9         <property name="initialPoolSize" value="1" />
10         <!--連接池中保留的最小連接數。-->
11         <property name="minPoolSize" value="1" />
12         <!--連接池中保留的最大連接數。Default: 15 -->
13         <property name="maxPoolSize" value="300" />
14         <!--最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->
15         <property name="maxIdleTime" value="60" />
16         <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
17         <property name="acquireIncrement" value="5" />
18         <!--每60秒檢查所有連接池中的空閑連接。Default: 0 -->
19         <property name="idleConnectionTestPeriod" value="60" />
20     </bean>
21     <bean id="sessionFactory"
22         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
23         <property name="dataSource" ref="dataSource" />
24         <property name="mappingResources">
25             <list>
26                 <value>cn/dy/bean/User.hbm.xml</value>
27             </list>
28         </property>
29         <property name="hibernateProperties">
30             <value>
31                 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
32                 hibernate.hbm2ddl.auto=update
33                 hibernate.show_sql=false
34                 hibernate.format_sql=false
35           </value>
36         </property>
37     </bean>

注意第五行的細節問題,另外,第26行的value是以后我們將要建立的hibernate配置文件,可以先不要添加,然后導入hibernate依賴包和mysql數據庫連接包
antlr-2.7.6.jar、commons-collections-3.1.jar、dom4j-1.6.1.jar、ehcache-1.2.3.jar、ejb3-persistence.jar、hibernate3.jar、hibernate-annotations.jar、hibernate-cglib-repack-2.1_3.jar、hibernate-commons-annotations.jar、hibernate-entitymanager.jar、javassist-3.4.GA.jar、jta-1.1.jar、slf4j-api-1.5.2.jar、slf4j-log4j12.jar、mysql-connector-java-3.1.13-bin.jar。

4、接下來就是繼續配置spring.xml把事務交給spring管理,很簡單,在配置文件末尾加入兩段代碼:

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--使用基于注解方式配置事務 -->
<tx:annotation-driven transaction-manager="txManager" />

5、添加struts2支持,導入Commons-fileupload-1.2.1.jar,commons-logging-1.0.4.jar,freewarker-2.3.15.jar,ognl-2.7.3.jar,struts2-core-2.1.8.1.jar,struts-spring-plugin-2.1.8.1.jar,xwork-core-2.1.6.jar,commons-io-1.3.2.jar。

6、配置web.xml文件中spring和struts2關系,代碼如下,(第11行,從struts2.1.3開始使用)

 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     <context-param>
 7         <param-name>contextConfigLocation</param-name>
 8         <param-value>/WEB-INF/spring.xml</param-value>
 9     </context-param>
10     <listener>
11         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12     </listener>
13     <filter>
14         <filter-name>struts2</filter-name>
15         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
16     </filter>
17     <filter-mapping>
18         <filter-name>struts2</filter-name>
19         <url-pattern>/*</url-pattern>
20     </filter-mapping>
21     <welcome-file-list>
22         <welcome-file>index.jsp</welcome-file>
23     </welcome-file-list>
24 </web-app>
25 

7、建立struts.xml文件放到src下,代碼如下,第六行是默認的視圖主題,避免JSP頁面里面自動加入格式代碼,第七行是指定由spring來進行action對象的創建。

1 <?xml version="1.0" encoding="UTF-8"?>
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 <struts>
6     <constant name="struts.ui.theme" value="simple" />
7     <constant name="struts.objectFactory" value="spring" />
8 </struts>

另外struts.xml的其他配置見另一篇文章:【struts配置文件介紹


8、文件架構的創建:cn.dy.action、cn.dy.bean、cn.dy.dao、cn.dao.impl、cn.dy.service、cn.dy.service.impl;另外還有測試文件的所在包cn.dy.test、密碼進行加密所用的類所在的包cn.dy.own。


9、打開mysql數據庫,新建一個數據庫命名為:learn。


到目前為止,環境結構已經搭建好了。下面就是環境的測試和數據庫操作。


      此文部分內容來源網絡。如有侵犯您的版權問題,請來消息至電子郵件DyEngima&163.com(&換成@),經核實后會在文章內部標明來源。
轉載請注明來源http://www.aygfsteel.com/DyEnigma/
簽名:有能力、有擔當、有情義的人才能稱之為男人,而不是由性別決定。