거래 : 거래 중 하나를 완료 여부를 완료 일련의 작업입니다.
핵심 트랜잭션 관리 인터페이스의 구현 클래스의 DataSourceTransactionManager입니다
주석을 사용하여 Spring 트랜잭션 관리 :
1. 가져 오기 항아리 패키지.
2. 콩 트랜잭션 관리 클래스를 정의
구성 파일 3. 트랜잭션 관리 주석 중심의
봄 구성 파일 : applicationContext.xml
<? XML 버전 = "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 : 텍사스 = "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 = "는 dataSource"클래스 = "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.然后测试