ばねトランザクション管理(コアトランザクション管理インターフェイス:のTransactionManager)

トランザクション:トランザクションが完了または完了していない一連のアクションです。

コアトランザクション管理インタフェースの実装クラスはDataSourceTransactionManagerです

アノテーションを使用して春のトランザクション管理:

1.インポートジャーパッケージ。

 

 

2.豆トランザクション管理クラスを定義します。

設定ファイル3.オープントランザクション管理、アノテーション・ドリブン

春の設定ファイル:applicationContext.xmlを

<?xml version = "1.0" エンコード= "UTF-8"?>

<豆のxmlns = "http://www.springframework.org/schema/beans"

       xmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance"

       xmlns:コンテキスト= "http://www.springframework.org/schema/context"

       xmlns:TX = "http://www.springframework.org/schema/tx"

       XSI:のschemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

 

       <コンテキスト:コンポーネント・スキャンベースパッケージ=「com.zhiyou100.kfs」> </コンテキスト:コンポーネント・スキャン>

       <! - (接続オブジェクトの数内部に格納されている)ソース構成データ:データベースとの対話。データソース:C3P0、ドルイド(アリ) - >

       <ビーンID = "データソース" クラス= "com.mchange.v2.c3p0.ComboPooledDataSource">

              <プロパティ名=「ユーザー」値=「ルート」/>

              <プロパティ名= "パスワード" 値= "123" />

              <プロパティ名= "driverClass" 値= "はcom.mysql.jdbc.Driver"> </ property>の

              <プロパティ名= "jdbcUrlと" 値= "はjdbc:mysqlの://127.0.0.1:3306 /テスト"> </ property>の

       </豆>

      

       <!-- 配置springJdbc的模板类 -->

       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byType">

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

       </bean>

       <!-- 定义一个事务管理类 -->

       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

       </bean>

       <!-- 开启注解:如果你的事务管理bean的id是transactionManager,属性transaction-manager可以不写 -->

       <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

4.在事务方法上加事务注解@Transactional

package com.zhiyou100.kfs.service;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

 

import com.zhiyou100.kfs.dao.BookShopDao;

 

/**

 *   书店Service的接口的实现类

 * @author KFS

 *

 */

@Service

public class BookShopServiceImp implements BookShopService{

       @Autowired

       private BookShopDao bookShopDao;

      

       public void setBookShopDao(BookShopDao bookShopDao) {

              this.bookShopDao = bookShopDao;

       }

       /**

        *   更新用户余额

        */

       @Transactional     //事务注解

       @Override

       public void purchase(String username, String isbn) {

              //查书的价格

              double price=bookShopDao.finBookPriceByIsbn(isbn);

              //更新书的库存

              bookShopDao.updateBookShop(isbn);

              //更新用户余额

              bookShopDao.updateAccount(username, price);

       }

 

}

5.测试

package com.zhiyou100.kfs.dao;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.zhiyou100.kfs.service.xml.BookShopService;

 

public class Test {

 

       public static void main(String[] args) {

              ApplicationContext app=new ClassPathXmlApplicationContext("appxml.xml");

              BookShopService bss=(BookShopService)app.getBean("bookShopServiceImp");

              bss.purchase("tom", "001");

       }

 

}

 

spring事务管理的xml使用:

1.导入jar包

 

 

2.定义一个bean事务管理类

3.在事务方法中不用注解

4.在spring配置文件中配置

<?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.xsd

              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

 

       <context:component-scan base-package="com.zhiyou100.kfs.service.xml,com.zhiyou100.kfs.dao"></context:component-scan>

       <!-- 配置数据源(里面存放了若干个连接对象):数据库交互的。    数据源:c3p0,druid(阿里) -->

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

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

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

              <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

              <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>

       </bean>

      

       <!-- 配置springJdbc的模板类 -->

       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byType">

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

       </bean>

       <!-- 定义一个事务管理类 -->

       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

       </bean>

       <!-- 建议:设置事务方法属性 -->

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

              <tx:attributes>

                     <!-- read-only:只读。用在查询上 -->

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

                     <tx:method name="purchase" propagation="REQUIRED"/>

                     <tx:method name="insert*"/>

                     <tx:method name="*"/>

              </tx:attributes>

       </tx:advice>

       <!-- xml切面的配置 -->

       <aop:config>

              <!-- 切点 -->

              <aop:pointcut expression="execution(* com.zhiyou100.kfs.service.xml.*.*(..))" id="pointcut"/>

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

       </aop:config>

</beans>

5.然后测试

おすすめ

転載: www.cnblogs.com/kfsrex/p/11494684.html