spring--hibernate 事务

<?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:context="http://www.springframework.org/schema/context"
      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.5.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

      <bean id="dataSource"  destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
             <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
             <property name="url" value="jdbc:sqlserver://hzz:1433;databaseName=Heros"/>
             <property name="username" value="sa"/>
             <property name="password" value="caxa"/>
     </bean>   
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
             <property name="dataSource" ref="dataSource"></property>
             <property name="mappingResources">
                <list>
                  <value>db/mapping/Employee.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
               <props>
                  <prop  key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.format_sql">true</prop>
               </props>
          </property>
     </bean> 
     <bean id="employeeDAO" class="db.dao.EmployeeDAOImpl">
         <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>  
     <bean id="employeeService" class="db.service.EmployeeServiceImpl">
         <property name="employeeDAO" ref="employeeDAO"></property>
     </bean>

      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"></property>
      </bean>
          <tx:advice id="txAdvice" transaction-manager="txManager">
              <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="save*"  propagation="REQUIRED"/>
              </tx:attributes>
          </tx:advice>
          <aop:config>
              <aop:pointcut id="txPointCut" expression="bean(*Service)"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
          </aop:config>
  
          

 
     
</beans>

 

1.Spring整合Hibernate
(1)引入开发包
a. spring基本包 : spring.jar,commons-logging.jar
b. spring aop包 : aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1_3.jar
c. 数据库驱动包 : mysql-connector-java-5.1.6-bin.jar
d. 连接池包 : commons-dbcp.jar,commons-pool.jar,commons-collections.jar
e. hibernate : hibernate3.jar,hibernate-entitymanager.jar,hibernate-commons-annotations.jar,hibernate-annotations.jar
f. dom4j开发包 : dom4j-1.6.1.jar
g. slf4j开发包 : slf4j-api-1.5.0.jar,slf4j-log4j12-1.5.0.jar
h. log4j开发包: log4j-1.2.15.jar
i. javassist开发包 : javassist.jar
j.(可选) 如果使用spring注解需要引入commons-annotation.jar

posted on 2016-01-25 22:52  编世界  阅读(155)  评论(0编辑  收藏  举报