事務:事務就是一系列的動作,這些動作要么都完成,要么都不完成。
核心事務管理接口的實現類是DataSourceTransactionManager
spring事務管理的注解使用:
1.導入jar包。
2.定義一個bean事務管理類
3.在配置文件中開啟事務管理的注解驅動
spring配置文件:applicationContext.xml
<?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" 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">
<context:component-scan base-package="com.zhiyou100.kfs"></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> <!-- 開啟注解:如果你的事務管理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.然后測試