Spring框架学习笔记(十) Spring中的事务管理(基于XML配置)

(一) 创建 xml文件

 引入 Aop   beans  tx 命名空间(注意不用引入 Context )

 (二) 配置数据库

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root"></property>
          <property name="password" value="1099026712"> </property>
          <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
  
     </bean>

(三) 配置 jdbctemplate的bean属性

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <property name="dataSource" ref="dataSource"></property>
     </bean>

(四)配置每个类的bean属性,注意类之间的引用关系

<!-- 配置bean -->
     <bean id="daoImpl" class="jdbc.tx.xml.DaoImpl">
      <property name="jdbcTemplate" ref="jdbcTemplate"></property>
     </bean>
     
     <bean id="bookshopImpl" class="jdbc.tx.xml.impl.BookshopImpl">
     <property name="book" ref="daoImpl"></property>
     </bean>
     
     <bean id="bookshopListImpl" class="jdbc.tx.xml.impl.BookshopListImpl">
     <property name="bk" ref="bookshopImpl"></property>
     </bean>

 (五) 配置 事务管理器,并开启事务

         在tx 中添加属性信息

<!-- 配置事务管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
     </bean>
     
     <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
        <tx:method name="purchase" propagation="REQUIRED"/>
        </tx:attributes>
     </tx:advice>

(六) 配置Aop 事务切入点信息

<!-- 配置事务切入点 -->
     <aop:config >
      <aop:pointcut expression="execution(* jdbc.tx.xml.impl.*.*(..))" 
      id="pointcut"/>
      <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
     </aop:config>

(七) 运行执行

发布了25 篇原创文章 · 获赞 14 · 访问量 7183

猜你喜欢

转载自blog.csdn.net/sdau_20171819/article/details/104254456