Struts2+Spring4+Hibernate4整合超詳細教程

Struts2、Spring4、Hibernate4整合 超詳細教程

Struts2、Spring4、Hibernate4整合實例-下載


 


項目目的:
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4)搭建項目架構原型。
項目架構原型:Struts2 + Spring4.0+ Hibernate4.2.4。
項目特色:同時使用了Struts2、Spring4、Hibernate4、log4j等庫或框架,搭建一個最基本的項目原型。

加入 Spring

  • 加入Spring 所需 jar 包

    這裏寫圖片描述

  • 配置 web.xml 文件

 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6 
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:applicationContext*.xml</param-value>
10     </context-param>
11 
12     <listener>
13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15 
16 
17 </web-app>

 

 

  • 加入 Spring 的配置文件[ applicationContext.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"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11 
12 </beans>
applcationContext.xml

 


加入Hibernate

  • 加入Hibernate所需jar包

hibernate所需jar包

  • 加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本屬性
 1 <?xml version="1.0" encoding="UTF-8"?>
 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     <session-factory>
 7         <!-- 配置 hibernate 的基本屬性 -->
 8 
 9         <!-- 方言 -->
10         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
11 
12         <!-- 是否顯示及格式化 SQL -->
13         <property name="hibernate.show_sql">true</property>
14         <property name="hibernate.format_sql">true</property>
15 
16         <!-- 生成數據表的策略 -->
17         <property name="hibernate.hbm2ddl.auto">update</property>
18 
19         <!-- 二級緩存相關 -->
20         <!--  ....... -->
21     </session-factory>
22 
23 </hibernate-configuration>
hibernate.cfg.xml

 

  • 和 Spring 進行整合

加入 c3p0 和 MySQL 的驅動

 c3p0的驅動

MySQL 的驅動

新建db.properties

1 jdbc.user=root
2 jdbc.password=1230
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
5 
6 jdbc.initPoolSize=5
7 jdbc.maxPoolSize=10
db.properties

在 Spring 的配置文件中配置: 數據源, SessionFactory, 聲明式事務

 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"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11 
12 
13     <context:annotation-config />
14     <context:component-scan base-package="com" />
15 
16     <!-- 導入資源文件 -->
17     <context:property-placeholder location="classpath:db.properties"/>
18 
19     <!-- 配置 C3P0 數據源 -->
20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
21         <property name="user" value="${jdbc.user}"></property>
22         <property name="password" value="${jdbc.password}"></property>
23         <property name="driverClass" value="${jdbc.driverClass}"></property>
24         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
25 
26         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
27         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
28     </bean>
29 
30     <!-- 配置 SessionFactory -->
31     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
32         <property name="dataSource" ref="dataSource"></property>
33         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
34         <property name="mappingLocations" value="classpath:com/entities/*.hbm.xml"></property>
35     </bean>
36 
37     <!-- 配置 Spring 的聲明式事務 -->
38     <!-- 1. 配置 hibernate 的事務管理器 -->
39     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
40         <property name="sessionFactory" ref="sessionFactory"></property>
41     </bean>
42 
43     <!-- 2. 配置事務屬性 -->
44     <tx:advice id="txAdvice" transaction-manager="transactionManager">
45         <tx:attributes>
46             <tx:method name="get*" read-only="true"/>
47             <tx:method name="lastNameIsValid" read-only="true"/>
48             <tx:method name="*"/>
49         </tx:attributes>
50     </tx:advice>
51 
52     <!-- 3. 配置事務切入點, 再把事務屬性和事務切入點關聯起來 -->
53     <aop:config>
54         <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
55         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
56     </aop:config>
57 
58 </beans>
applicationContext.xml

 

  • 小測試

新建實體類Test.java

 1 package com.entities;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 import javax.persistence.Table;
 8 
 9 @Entity
10 @Table(name = "test")
11 public class Test {
12 
13     @Id
14     @GeneratedValue(strategy = GenerationType.IDENTITY)
15     private long id;//主鍵
16     private String name;
17 
18 
19     public long getId() {
20         return id;
21     }
22     public void setId(long id) {
23         this.id = id;
24     }
25     public String getName() {
26         return name;
27     }
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32 }
Test.java

 

hibernate.cfg.xml 添加

1    <session-factory>
2         <!-- 以上 ...-->
3         <mapping class="com.entities.Test"></mapping>
4     </session-factory>
View Code

如果成功,數據庫內自動生成Test表


加入 Struts2

  • 加入 jar 包: 若有重複的 jar 包, 則需要刪除版本較低的.

    這裏寫圖片描述

  • 在 web.xml 文件中配置 Struts2 的 Filter
 1  <!-- 配置 Struts2 的 Filter -->
 2     <filter>
 3         <filter-name>struts2</filter-name>
 4         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 5     </filter>
 6 
 7     <filter-mapping>
 8         <filter-name>struts2</filter-name>
 9         <url-pattern>/*</url-pattern>
10     </filter-mapping>
web.xml
  • 加入 Struts2 的配置文件,新建struts.xml
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <package name="default" namespace="/" extends="struts-default">
 9 
10             <action name="test">
11                 <result>index.jsp</result>
12             </action>   
13 
14     </package>
15 
16 </struts>
struts.xml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章