Struts2+Spring4.2+Hibernate4.3整合

一、导包

antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
dom4j-1.6.1.jar
freemarker-2.3.22.jar
geronimo-jta_1.1_spec-1.1.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.0.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.3.0.Final.jar
jstl-1.2.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mysql-connector-java-5.1.7-bin.jar
ognl-3.0.6.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
spring-web-4.2.4.RELEASE.jar
standard.jar
struts2-core-2.3.24.jar
struts2-spring-plugin-2.3.24.jar
xwork-core-2.3.24.jar

二、spring与hibernate整合

  hibernate.cfg.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- Mysql数据库方言 -->
 9         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
10         <!-- 显示sql语句 -->
11         <property name="show_sql">true</property>
12     
13     <!-- 引入映射文件 -->
14         <mapping resource="com/tx/model/User.hbm.xml"/>
15     </session-factory>
16 </hibernate-configuration>

  ApplicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3         xmlns="http://www.springframework.org/schema/beans" 
 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-4.2.xsd 
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-4.2.xsd
11         http://www.springframework.org/schema/aop
12         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
13         http://www.springframework.org/schema/tx
14         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
15         ">
16         
17     <!-- 读取db.properties文件 -->
18     <context:property-placeholder location="classpath:db.properties"/>
19     <!-- 配置数据源 -->
20     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
21         <property name="driverClass" value="${jdbc.driverClass}"></property>
22         <property name="jdbcUrl" value="${jdbc.url}"></property>
23         <property name="user" value="${jdbc.user}"></property>
24         <property name="password" value="${jdbc.password}"></property>
25     </bean>
26     
27     <!-- 用spring orm包整合sessionFactory -->
28     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
29         <!-- 注入数据源 -->
30         <property name="dataSource" ref="dataSource"></property>
31         <!-- 加载hibernate核心配置文件 -->
32         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
33     </bean>
34     
35     <!-- 用spring rom的hibernate事务管理器 -->
36     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
37         <!-- 注入sessionFactory -->
38         <property name="sessionFactory" ref="sessionFactory"></property>
39     </bean>
40     
41     <!-- 配置 -->
42     <tx:advice id="txAdvice" transaction-manager="transactionManager">
43         <tx:attributes>
44             <tx:method name="save*" propagation="REQUIRED"/>
45             <tx:method name="update*" propagation="REQUIRED"/>
46             <tx:method name="delete*" propagation="REQUIRED"/>
47             <tx:method name="query*" read-only="true"/>
48         </tx:attributes>
49     </tx:advice>
50     
51     <aop:config>
52         <!-- 配置切点 -->
53         <aop:pointcut expression="execution(* com.tx.service..*.*(..))" id="mycut"/>
54         <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
55     </aop:config>
56     
57     <!-- ==============以上为spring与hibernate的整合配置====================== -->
58     <bean id="userAction" class="com.tx.action.UserAction" scope="prototype">
59         <property name="userService" ref="userService"></property>
60     </bean>
61     <!-- ==============以上为spring与struts的整合============================ -->
62     <bean id="userDao" class="com.tx.dao.impl.UserDaoImpl">
63         <property name="sessionFactory" ref="sessionFactory"></property>
64     </bean>
65     <bean id="userService" class="com.tx.service.impl.UserServiceImpl">
66         <property name="userDao" ref="userDao"></property>
67     </bean>
68 </beans>

三、spring与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     <!-- #  struts.objectFactory = spring    将action的创建交给spring容器    
 8             struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
 9             -->
10     <!-- <constant name="struts.objectFactory" value="spring"></constant> -->
11 
12     <package name="user" namespace="/" extends="struts-default" >
13         <action name="userAction_*" class="userAction" method="{1}">
14             <result name="success">/success.jsp</result>
15         </action>
16     </package>
17 </struts>
18     

 

posted @ 2018-04-04 22:44  cat_fish  阅读(169)  评论(0编辑  收藏  举报