struts2+hibernate+spring

1添加jar包(hibernate+其他jar包)


2修改applicationContext配置文件,通过spring容器管理hibernate

<!--配置数据源 dataSource-->

       <bean id="dataSource"   class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

           <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>  

           <property name="username" value="scott"></property>  

           <property name="password" value="tiger"></property> 

           <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>

       </bean>

        <!-- 配置sessionFactory-->

       <bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  

            <property name="dataSource">   

                 <ref bean="dataSource" />  

            </property>

              <!-- 读取hibernate配置文件-->

            <property name="configLocation"value="classpath:hibernate.cfg.xml" />

            <property name="hibernateProperties">   

                  <props>   

                      <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>    

                      <prop key="hibernate.hbm2ddl.auto">update</prop>

                  </props>  

            </property>

            <!-- 映射文件 (也可以写在hibernate.cfg.xml配置文件中)-->

            <property name="mappingResources">

                 <list>

                       <value>vo/emp.hbm.xml</value>

                 </list>

            </property>

       </bean>

3创建hibernate.cfg.xml

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

      "-//Hibernate/HibernateConfiguration DTD 3.0//EN"

      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

      <hibernate-configuration>

           <session-factory>

                 <property name="current_session_context_class">thread</property>

                 <property  name="connection.characterEncoding">UTF-8</property>

           </session-factory>

      </hibernate-configuration>

4写dao层

 

 

配置连接池

注意:如果使用的是Spring3.2.*与Hibernate4.3.*会有版本不兼容的问题

 

<bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"> 

        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"> 

        </property> 

        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 

        <property name="user" value="scott"/> 

        <property name="password" value="tiger"/> 

        <property name="minPoolSize" value="10" /> 

        <property name="maxPoolSize" value="100" /> 

        <property name="maxIdleTime" value="1800" /> 

        <property name="acquireIncrement" value="3" /> 

        <property name="maxStatements" value="1000" /> 

        <property name="initialPoolSize" value="10" /> 

        <property name="idleConnectionTestPeriod" value="60" /> 

        <property name="acquireRetryAttempts" value="30" /> 

        <property name="breakAfterAcquireFailure" value="true" /> 

        <property name="testConnectionOnCheckout" value="false" /> 

</bean>

配置事物

修改applicationContext.xml 添加

<!--配置事务管理 -->

     <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">  

          <property name="sessionFactory"ref="sessionFactory" /> 

      </bean>

     <!--配置事务增强-->

    <tx:advice id="txAdvice"transaction-manager="transactionManager">

        <tx:attributes>

             <tx:method name="save*"propagation="REQUIRED"/>

               <tx:method name="delete*"propagation="REQUIRED"/>

               <tx:method name="update*"propagation="REQUIRED"/>

               <tx:method name="get*"read-only="true" propagation="NOT_SUPPORTED"/>

               <tx:method name="*"read-only="true"/>

        </tx:attributes>

    </tx:advice>

    <!--aop配置-->

    <aop:config expose-proxy="true">

        <aop:pointcutid="pt" expression="execution(*service.*.*(..))"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>

</aop:config>

 

注意el中的路径

修改hibernate.cfg.xml


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值