spring 程式設計式事務控制

2021-10-10 18:32:20 字數 2418 閱讀 6549

/** * 賬戶的業務成層實現類

* 事務控制應該都是在業務層

*/public class accountserviceimpl implements iaccountservice

private transactiontemplate transactiontemplate;

public void settransactiontemplate(transactiontemplate transactiontemplate)

@override

public account findaccountbyid(integer accountid)})

;}@override

public void transfer(string sourcename, string targetname, float money)});}}

bean.xml

""xmlns:xsi=

""xsi:schemalocation=""

>

<

!--配置賬戶的業務層-->

"accountservice" class=

"com.itheima.service.impl.accountserviceimpl"

>

"accountdao" ref=

"accountdao"

>

"transactiontemplate" ref=

"transactiontemplate"

>

<

!--配置賬戶的持久層-->

"accountdao" class=

"com.itheima.dao.impl.accountdaoimpl"

>

"datasource" ref=

"datasource"

>

<

!--配置資料來源-->

"datasource" class=

"org.springframework.jdbc.datasource.drivermanagerdatasource"

>

"driverclassname" value=

"com.mysql.jdbc.driver"

>

"url" value=

"jdbc:mysql://localhost:3306/eesy"

>

"username" value=

"root"

>

"password" value=

"root"

>

<

!--配置事務管理器-->

"transactionmanager" class=

"org.springframework.jdbc.datasource.datasourcetransactionmanager"

>

"datasource" ref=

"datasource"

>

<

!--配置事務模板物件-->

"transactiontemplate" class=

"org.springframework.transaction.support.transactiontemplate"

>

"transactionmanager" ref=

"transactionmanager"

>

程式設計式事務控制和宣告式事務控制spring都支援。

區別在於使用宣告式事務控制時,通過spring框架的xml和aop兩種配置方式,spring框架會將事務控制的過程完成。

使用程式設計式事務控制相當於將spring框架事務控制過程自定義化了一遍,需要通過匿名內部類的方式實現事務控制的過程。

spring程式設計式事務

程式設計式事務 需要編寫 控制事務在 開始,提交,回滾。public class stucardserviceimpl implements stucardservice scdao.updatestucardbycardno sourcecard system.out.println 支付完成!商...

spring中JDBC程式設計式事務

在spring採用純jdbc進行資料庫操作 org.gjt.mm.mysql.driver jdbc mysql localhost 3306 test root root 測試 package com.spring.jdbc import org.springframework.beans.fac...

spring 程式設計式事務管理

事務管理很重要,他保證了每一步的操作都是可靠的,即便出現了異常的情況也不會破壞資料庫的完整性。1 在 spring 中,事務是通過 transactiondefinition 介面來定義的。該介面包含與事務屬性有關的方法。具體內容如 下 public inte ce transactiondefin...