首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > Java开发 > 正文

SSH整合大杂烩(struts+spring+hibernate)

作者:cocomyyz 来源: 日期:2013-08-04 23:44:58 人气:4 加入收藏 评论:0 标签:java

整合导入顺序:struts--->spring--->hibernate


加载原理:把struts的启动任务交给web.xml,然后spring的加载交给struts,

在粗体前后分别加入:

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

<message-resources parameter="com.handson.springlogin.struts.ApplicationResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property property="contextConfigLocation" value="classpath:applicationContext*.xml" />
</plug-in>


最后hibernate交给spring管理(导入时选择第二个):


applicationContext-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- DBCP data source -->
<!--
<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="defaultAutoCommit" value="false"></property>
  <property name="maxActive" value="50"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/exam"></property>
  <property name="username" value="root"></property>
  <property name="password" value=""></property>
  <property name="password" value="true"></property>
  <property name="password" value="true"></property>
  <property name="password" value="180"></property>
</bean>
  -->

<!-- C3P0 data source -->
<bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  <property name="autoCommitOnClose" value="false"></property>
  <property name="maxIdleTime" value="1800"></property> <!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃 -->
  <property name="maxPoolSize" value="50"></property>
  <property name="initialPoolSize" value="5"></property>
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/exam"></property>
  <property name="user" value="root"></property>
  <property name="password" value=""></property>
</bean>

<!-- Hibernate sessionFactory -->
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
   <value>com/brady/exam/model/Grade.hbm.xml</value>
   <value>com/brady/exam/model/Paper.hbm.xml</value>
   <value>com/brady/exam/model/Test.hbm.xml</value>
   <value>com/brady/exam/model/Subject.hbm.xml</value>
   <value>com/brady/exam/model/User.hbm.xml</value>
   <value>com/brady/exam/model/Question.hbm.xml</value>
   <value>com/brady/exam/model/Basic.hbm.xml</value>
   <value>com/brady/exam/model/AnswerTest.hbm.xml</value>
   <value>com/brady/exam/model/Admin.hbm.xml</value>
   <value>com/brady/exam/model/AnswerQuestion.hbm.xml</value>
   </list>
  </property>
</bean>

<!-- Tranasction Manager -->

<bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="insert*" propagation="REQUIRED"/>
   <tx:method name="edit*" propagation="REQUIRED"/>
   <tx:method name="save*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="merge*" propagation="REQUIRED"/>
   <tx:method name="remove*" propagation="REQUIRED"/>
   <tx:method name="delete*" propagation="REQUIRED"/>
   <tx:method name="find*" read-only="true"/>
   <tx:method name="get*" read-only="true"/>
  </tx:attributes>
</tx:advice>

<aop:config>
  <aop:pointcut id="daoPointcut"
   expression="execution(* com.brady.exam.dao.*.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="daoPointcut"/>
</aop:config>

<!-- End Tranasction Manager -->

<bean id="userDAO" class="com.brady.exam.dao.impl.hibernate.UserDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
</bean>

<bean id="basicDAO" class="com.brady.exam.dao.impl.hibernate.BasicDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
</bean>

</beans>


本文网址:http://www.mingyangnet.com/html/java/165.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
更多>>网友评论
发表评论
编辑推荐
  • 没有资料