Struts2+Hibernate3.0+Spring2.5.6整合

这是一个简单的SSH XMl 配置的 整合 DEMO.~~

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 5
 6     <!-- 配置Spring自动管理Session. 要配置到struts过滤器之前!-->
 7     <filter>
 8         <filter-name>hibernateSessionFilter</filter-name>
 9         <filter-class>
10             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
11     </filter>
12     <filter-mapping>
13         <filter-name>hibernateSessionFilter</filter-name>
14         <url-pattern>/*</url-pattern>
15     </filter-mapping>
16
17     <!-- classpath:applicationContext.xml  编译路径SRC,即指向.class文件目录的src
18          相当 于/WEB-INF /classes/ applicationContext.xml
19     -->
20     <context-param>
21         <param-name>contextConfigLocation</param-name>
22         <param-value>classpath:applicationContext.xml</param-value>
23     </context-param>
24
25     <!-- ContextLoaderListener预设读取applicationContext.xml  -->
26     <listener>
27         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
28     </listener>
29     <!-- 配置  struts2 -->
30     <filter>
31         <filter-name>struts2</filter-name>
32         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
33     </filter>
34     <filter-mapping>
35         <filter-name>struts2</filter-name>
36         <url-pattern>/*</url-pattern>
37     </filter-mapping>
38
39 </web-app>

WEB.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:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6 http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8 http://www.springframework.org/schema/tx
 9 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12   http://www.springframework.org/schema/context
13    http://www.springframework.org/schema/context/spring-context-2.5.xsd
14 ">
15     <!-- 配置  datasource -->
16     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
17         <property name="driverClassName" value="com.mysql.jdbc.Driver">
18         </property>
19         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
20         <property name="username" value="root"></property>
21         <property name="password" value="1234"></property>
22         <property name="initialSize" value="5"></property>
23         <property name="minIdle" value="2"></property>
24     </bean>
25
26     <!-- 配置  SessionFactory -->
27     <bean id="sessionFactory"
28         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
29         <property name="dataSource" ref="dataSource"></property>
30
31
32         <!-- 映射路径 -->
33         <property name="mappingDirectoryLocations">
34                   <list>
35                       <value>classpath:/com/shuanlei/po</value>
36                   </list>
37              </property>
38             <!--
39         <property name="mappingResources">
40                 <list> <value>com/shuanlei/po/User.hbm.xml</value> </list>
41             <list>
42                 <value>classpath:/com/shuanlei/po</value>
43             </list>
44         </property>
45             -->
46         <!-- 加载Hibernate -->
47         <property name="hibernateProperties">
48             <props>
49                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
50                 <prop key="hibernate.hbm2ddl.auto">update</prop>
51                 <prop key="hibernate.show_sql">true</prop>
52                 <prop key="hibernate.format_sql">true</prop>
53
54             </props>
55         </property>
56     </bean>
57
58     <!--
59         配置 hibernatetemplate 为hibernateTemplate注入 sessionFactory .
60     -->
61     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
62         <property name="sessionFactory" ref="sessionFactory"></property>
63     </bean>
64     <!-- 配置  事务 -->
65     <bean id="txManager"
66         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
67         <property name="sessionFactory" ref="sessionFactory"></property>
68     </bean>
69     <tx:annotation-driven transaction-manager="txManager" />
70
71     <!-- 配置  AOp -->
72     <aop:config>
73         <aop:pointcut expression="execution(public * com.shuanlei.service.impl.*.*(..))"
74             id="bussinessService" />
75         <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
76     </aop:config>
77     <tx:advice id="txAdvice" transaction-manager="txManager">
78         <tx:attributes>
79             <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
80             <tx:method name="add*" propagation="REQUIRED" />
81             <tx:method name="del*" propagation="REQUIRED" />
82             <tx:method name="update*" propagation="REQUIRED" />
83
84         </tx:attributes>
85     </tx:advice>
86     <!-- 导入 其它的 xml包包 -->
87     <import resource="classpath*:/applicationContext-action.xml" />
88     <import resource="classpath*:/applicationContext-dao.xml" />
89     <import resource="classpath*:/applicationContext-service.xml" />
90
91 </beans>

applicationContext.XML

 1 <?xml version="1.0" encoding="UTF-8"?>
 2
 3   <beans
 4       xmlns="http://www.springframework.org/schema/beans"
 5       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6       xmlns:p="http://www.springframework.org/schema/p"
 7       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 8
 9
10       <bean id="UserAction" class="com.shuanlei.action.UserAction" scope="prototype" autowire="byType" >
11       </bean>
12
13  </beans>

applicationContext-action.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  <beans
 3       xmlns="http://www.springframework.org/schema/beans"
 4       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5       xmlns:p="http://www.springframework.org/schema/p"
 6       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7
 8       <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
 9             <property name="sessionFactory" ref="sessionFactory"></property>
10      </bean>
11
12       <bean id="implUserDao" class="com.shuanlei.dao.impl.UserDaoImpl" scope="prototype" autowire="byType" >
13            <property name="hibernateTemplate" ref="hibernateTemplate"></property>
14       </bean>
15
16  </beans>

applicationContext-dao.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3       xmlns="http://www.springframework.org/schema/beans"
 4       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5       xmlns:p="http://www.springframework.org/schema/p"
 6       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7
 8
 9       <bean id="implUserService" class="com.shuanlei.service.impl.UserServiceImpl" scope="prototype" autowire="byType" >
10
11       </bean>
12
13  </beans>

applicationContext-service.xml

 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
 6 <struts>
 7     <constant name="struts.multipart.saveDir" value="/tmp" />
 8     <!-- spring  交 给 struts2管理 -->
 9     <constant name="struts.objectFactory" value="spring"></constant>
10     <!--
11         需要添加额外的jar , 才能去 ioc 容器当中获取 bean struts-spring-plugin...jar
12     -->
13     <package name="default" namespace="/" extends="struts-default">
14         <action name="UserAction" class="com.shuanlei.action.UserAction"></action>
15
16     </package>
17
18 </struts>

struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5   <hibernate-mapping
 6       package="com.shuanlei.po">
 7
 8       <class name="User" table="t_user">
 9          <id  name="id"  column="id" >
10             <generator class="native"></generator>
11         </id>
12
13          <property name="uname" column="uname"></property>
14
15          <property name="pwd" column="pwd"></property>
16
17      </class>
18
19  </hibernate-mapping>

User.hbm.xml

1 public interface ImplUserDao {
2     public void add(User user);
3 }

com.shuanlei.dao.ImplUserDao

 1 public class UserDaoImpl implements ImplUserDao{
 2
 3     private HibernateTemplate hibernateTemplate;
 4
 5     public void add(User user) {
 6
 7         hibernateTemplate.save(user);
 8     }
 9
10     public HibernateTemplate getHibernateTemplate() {
11         return hibernateTemplate;
12     }
13
14     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
15         this.hibernateTemplate = hibernateTemplate;
16     }
17
18 }

com.shuanlei.dao.impl.UserDaoImpl

1 public interface ImplUserService {
2     public void add(User user);
3 }

com.shuanlei.service.ImplUserService

 1 public class UserServiceImpl implements ImplUserService {
 2     private ImplUserDao implUserDao;
 3
 4     public void add(User user) {
 5
 6         implUserDao.add(user);
 7     }
 8
 9     public ImplUserDao getImplUserDao() {
10         return implUserDao;
11     }
12
13     public void setImplUserDao(ImplUserDao implUserDao) {
14         this.implUserDao = implUserDao;
15     }
16
17 }

com.shuanlei.service.impl.UserServiceImpl

 1 public class UserAction {
 2     private ImplUserService implUserService;
 3     private User user;
 4
 5     public String add(){
 6
 7         implUserService.add(user);
 8         return null;
 9     }
10
11     public User getUser() {
12         return user;
13     }
14
15     public void setUser(User user) {
16         this.user = user;
17     }
18
19     public ImplUserService getImplUserService() {
20         return implUserService;
21     }
22
23     public void setImplUserService(ImplUserService implUserService) {
24         this.implUserService = implUserService;
25     }
26
27 }

com.shuanlei.action.UserAction

引用:http://www.cnblogs.com/shangxiaofei/p/3951469.html

致 漂亮的小吐绿  ^_^

时间: 2024-12-22 08:40:54

Struts2+Hibernate3.0+Spring2.5.6整合的相关文章

struts2+hibernate3.2+spring2.0整合篇(三)

在上篇文章中,我们已经将各层之间相关的类创建完毕,这篇我们主要讲解如何通过spring管理类之间的依赖关系. 步骤八:通过spring管理类之间的依赖关系. applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="

struts2+hibernate3.2+spring2.0整合篇(一)

本系列文章将通过一个添加用户的实例,讲解struts2+hibernate3.2+spring2.0的整合过程. 步骤一:用MyEclipse8.5搭建SSH开发环境 (1)搭建Hibernate3.2开发环境 新建Web Project,右键选中项目-->MyEclipse-->Project Capabilities-->Add Hibernate Capabilities--> (2)搭建Spring2.0开发环境 右键选中项目-->MyEclipse-->Pro

【Struts2+Hibernate3+Spring3】利用SSH整合,完成打印用户表,用户登录、注册、修改密码系统

本文视图尽可能不杂糅其它技术,尽可能少写代码,完成SSH整合.以致于各位在现有网上资料越来越天花龙凤之下,清晰地了解到传说中的三大框架SSH是怎么整合的. 一.SSH的下载 首先SSH的下载就已经是一个难点.SSH三个地方同时推出各自的新技术,已经要求利用Maven完成其版本的同步.毕竟Spring的版本,决定其能整合什么版本的Struts与Hibernate.Spring3.0.5就不能整合Hibernate4.x了.因此本文选取的SSH的版本,分别为struts 2.3.20 GA.Hibe

Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享

原创整理不易,转载请注明出处:Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享 代码下载地址:http://www.zuidaima.com/share/1780237805931520.htm 一.准备 1. Hibernate: hibernate-distribution-3.3.2.GA, hibernate-annotations-3.4.0.GA 2. Spring: spring-fr

Struts2.1.6+Spring2.0+Hibernate3.1整合图解教程(ssh223)

在网上搜索了很多ssh223的配置文章,但有的开发环境不同,版本也不同,下面是我做的ssh223的开发配置教程,以供参考(因为是很久很久以前写的,因此环境有些古老,有用得到朋友根据实际情况来就OK) 开发环境:MyEclipse5.5+Tomcat6.0+MySql5.1 配置环境:Struts2.1.6+Spring2.0+Hibernate3.1 第一步:创建Java-Web Project工程,选择J2EE 5.0版本 下一步 下一步 第二步:配置spring2.0 1.添加jar包 如图

struts2.1.8 spring2.5.6 hibernate3.3G 依赖jar包

----struts2.1.8---- struts2-core-2.1.8.1.jar struts2核心包 struts2-json-plugin-2.1.8.1.jar struts2的json插件--var s = {name:"zhangs",age:"18"} struts2-spring-plugin-2.1.8.1.jar 与spring集成插件 xwork-core-2.1.6.jar struts2的构建基础jar struts2-convent

Struts2+Hibernate3+Spring3 整合

Struts2+Hibernate3+Spring3 整合 一.SSH的下载 首先SSH的下载就已经是一个难点.SSH三个地方同时推出各自的新技术,已经要求利用Maven完成其版本的同步.毕竟spring的版本,决定其能整合什么版本的Struts与hibernate.Spring3.0.5就不能整合Hibernate4.x了.因此本文选取的SSH的版本,分别为struts 2.3.20 GA.Hibernate3.6.10Final.Spring3.0.5,最后有一个log4j,因为Spring

spring3+struts2+hibernate3整合出现的问题,No mapping found for dependency [type=java.lang.String, name=&#39;struts.objectFactory.spring.enableAopSupport&#39;]

七月 11, 2016 3:49:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:EBook' did not find a matching property.七月 11, 2016 3:4

ExtJs4.2+Mysql+Struts2+Hibernate3实现分页查询

ExtJs4.2+Mysql+Struts2+Hibernate3实现分页查询 1.demo简介 这是一个由ExtJs4.2,Mysql5.5 ,Struts2,Hibernate3.3构成的简单web项目,本人由于最近在研究ExtJs所以特意做了这个Demo,方便有需要的同学查看,也给自己留下学习笔记吧.需要特别说明我这里并没有整合Struts,Hibernate,这两个工具是独立运行的.转载请注明:http://blog.csdn.net/qiuzhping/article/details/