使用註解實現Spring的聲明式交易管理,更加簡單。
步驟:
1) 必須引入Aop相關的jar檔案
2) bean.xml中指定註解方式實現聲明式交易管理以及應用的交易管理員類
3)在需要添加事務控制的地方,寫上: @Transactional
@Transactional註解:
1)應用事務的註解
2)定義到方法上: 當前方法應用spring的聲明式事務
3)定義到類上: 當前類的所有的方法都應用Spring聲明式交易管理;
4)定義到父類上: 當執行父類的方法時候應用事務。
案例:
1.Dept實體類
<span style="font-family:Courier New;font-size:14px;">public class Dept {private int deptId;private String deptName;public int getDeptId() {return deptId;}//get set}</span> 2.DeptDao 通過註解的方式注入-->因此要想找到JdbcTemple類需要在bean.xml設定檔中進行配置
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.anno;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;@Componentpublic class DeptDao {//IOC容器注入@Resourceprivate JdbcTemplate jdbcTemplate;public void save(Dept dept){String sql="insert into t_dept(deptName) values(?)";jdbcTemplate.update(sql,dept.getDeptName());}}</span> 3.DeptService --->事務是在service層的.要想使用事務只需要在方法上添加@Transactional即可(添加到類上也可以 這樣的話就對此類中的所有方法進行事務配置)
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.anno;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;@Componentpublic class DeptService {//注入@Resourceprivate DeptDao deptDao;@Transactionalpublic void save(Dept dept){//如果這裡加了事務 出錯了 會自動復原 都會儲存失敗//如果沒有添加事務 出錯了 第一個會儲存成功deptDao.save(dept);}}</span> 4.bean.xml---->要想使用註解事務 只需要在設定檔中指定註解實現事務
<span style="font-family:Courier New;font-size:14px;"><?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 資料庫連接池的配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///day15"></property> <property name="user" value="root"></property> <property name="password" value="169500"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="5"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean> <bean id="jdbcTemple" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟註解掃描 --> <context:component-scan base-package="cn.itcast.anno"></context:component-scan> <!-- -***************************spring聲明式交易管理xml方式************************************************* --> <!-- 1.配置交易管理員 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 註解方式實現事務: 指定註解方式實現事務--> <tx:annotation-driven transaction-manager="txManager"/> </beans></span>
5.測試類別App
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.anno;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {@Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/anno/bean.xml");DeptService deptService=(DeptService) ac.getBean("deptService");Dept dept=new Dept();dept.setDeptName("annoTX");deptService.save(dept);}}</span> 運行結果測試:
事務總結:
如果使用spring提供的交易管理方式..假設在資料庫層出現了錯誤.就會 復原事務..資料庫不會有資料
如果不採用事務..如果出現了錯誤..就不會復原..資料庫中會有資料.舉例:
如果A轉賬給B A轉賬100給B..不使用事務.B出錯了..事務不會復原。A少了100 B沒有增加
如果使用事務..則這個操作是在同一個事務下 .只要出錯了事務就會復原!可以保證使用者的安全