Spring的事務控制 基於註解的方式

2021-10-10 17:56:54 字數 2433 閱讀 2564

1.建立資料表

2.建立account實體類

public

class

account

public

void

setname

(string name)

public string getmoney()

public

void

setmoney

(string money)

}

3.建立accountdao介面及其實現類(介面**省略)

@repository

("accountdao"

)public

class

accountdaoimpl

implements

accountdao

@override

public

void

in(string inman,

double money)

}

4.建立accountservice介面及其實現類(介面**省略)

@service

("accountservice"

)public

class

accountserviceimpl

implements

accountservice

}

5.配置spring檔案

<?xml version="1.0" encoding="utf-8"?>

xmlns

=""xmlns:xsi

=""xmlns:context

=""xmlns:tx

=""xmlns:aop

=""xsi:schemalocation

=" /spring-beans.xsd

/spring-tx.xsd

">

<

context:component-scan

base-package

="com.hao"

/>

<

tx:annotation-driven

transaction-manager

="transactionmanager"

/>

"transactionmanager"

class

="org.springframework.jdbc.datasource.datasourcetransactionmanager"

>

name

="datasource"

ref="datasource"

/>

bean

>

"datasource"

class

="com.mchange.v2.c3p0.combopooleddatasource"

>

name

="driverclass"

value

="com.mysql.cj.jdbc.driver"

/>

name

="jdbcurl"

value

="jdbc:mysql://localhost:3306/test?servertimezone=utc"

/>

name

="user"

value

="root"

/>

name

="password"

value

="hao20001010"

/>

bean

>

"jdbctemplate"

class

="org.springframework.jdbc.core.jdbctemplate"

>

name

="datasource"

ref="datasource"

/>

bean

>

beans

>

6.測試:

public

class

accountcontroller

}

結果:

spring基於註解 的宣告式事務控制(配置方式)

配置事務管理器 transactionmanager class org.springframework.jdbc.datasource.datasourcetransactionmanager datasource ref datasource property bean service acco...

spring基於註解方式實現事務

環境搭建 資料來源,資料庫驅動 spring jdbc模組org.springframework groupid spring jdbc artifactid 4.3.12 release version dependency c3p0 groupid c3p0 artifactid 0.9.1 2...

Spring 基於xml和註解的事務

org.springframework spring tx 5.0.2.release 需要新增事務管理的tx約束,同時也需要aop的約束 事務的屬性 propagation 用於指定事務的傳播行為。預設值是required,表示一定會有事務,增刪改的選擇。查詢方法可以選擇supports。read...