标签:
这是一个简单的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>
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>
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>
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>
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>
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>
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>
1 public interface ImplUserDao { 2 public void add(User user); 3 }
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 }
1 public interface ImplUserService { 2 public void add(User user); 3 }
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 }
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 }
引用:http://www.cnblogs.com/shangxiaofei/p/3951469.html
致 漂亮的小吐绿 ^_^
Struts2+Hibernate3.0+Spring2.5.6整合
标签:
原文地址:http://www.cnblogs.com/shuanlei/p/4257563.html