Spring AOP事務(wù)管理的示例詳解
轉(zhuǎn)賬案例-環(huán)境搭建
步驟1:準(zhǔn)備數(shù)據(jù)庫(kù)表
之前我們?cè)谡螹ybatis的時(shí)候已經(jīng)創(chuàng)建了這個(gè)表,可以直接使用
create database spring_db character set utf8; use spring_db; create table tbl_account( id int primary key auto_increment, name varchar(35), money double ); insert into tbl_account values(1,'Tom',1000); insert into tbl_account values(2,'Jerry',1000);
步驟2:創(chuàng)建項(xiàng)目導(dǎo)入jar包
項(xiàng)目的pom.xml添加相關(guān)依賴
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.10.RELEASE</version> </dependency> </dependencies>
步驟3:根據(jù)表創(chuàng)建模型類
public class Account implements Serializable { private Integer id; private String name; private Double money; //setter...getter...toString...方法略 }
步驟4:創(chuàng)建Dao接口
public interface AccountDao { @Update("update tbl_account set money = money + #{money} where name = #{name}") void inMoney(@Param("name") String name, @Param("money") Double money); @Update("update tbl_account set money = money - #{money} where name = #{name}") void outMoney(@Param("name") String name, @Param("money") Double money); }
步驟5:創(chuàng)建Service接口和實(shí)現(xiàn)類
public interface AccountService { /** * 轉(zhuǎn)賬操作 * @param out 傳出方 * @param in 轉(zhuǎn)入方 * @param money 金額 */ public void transfer(String out,String in ,Double money) ; } @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; public void transfer(String out,String in ,Double money) { accountDao.outMoney(out,money); accountDao.inMoney(in,money); } }
步驟6:添加jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false jdbc.username=root jdbc.password=root
步驟7:創(chuàng)建JdbcConfig配置類
public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }
步驟8:創(chuàng)建MybatisConfig配置類
public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; } }
步驟9:創(chuàng)建SpringConfig配置類
@Configuration @ComponentScan("com.itheima") @PropertySource("classpath:jdbc.properties") @Import({JdbcConfig.class,MybatisConfig.class}) public class SpringConfig { }
步驟10:編寫測(cè)試類
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class AccountServiceTest { @Autowired private AccountService accountService; @Test public void testTransfer() throws IOException { accountService.transfer("Tom","Jerry",100D); } }
事務(wù)管理
上述環(huán)境,運(yùn)行單元測(cè)試類,會(huì)執(zhí)行轉(zhuǎn)賬操作,Tom
的賬戶會(huì)減少100,Jerry
的賬戶會(huì)加100。
這是正常情況下的運(yùn)行結(jié)果,但是如果在轉(zhuǎn)賬的過(guò)程中出現(xiàn)了異常,如:
@Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; public void transfer(String out,String in ,Double money) { accountDao.outMoney(out,money); int i = 1/0; accountDao.inMoney(in,money); } }
以上就是Spring AOP事務(wù)管理的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring AOP事務(wù)管理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot 中html的頁(yè)面間跳轉(zhuǎn)問題小結(jié)
這篇文章主要介紹了SpringBoot 中html的頁(yè)面間跳轉(zhuǎn)問題小結(jié),本文給大家分享兩種方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10SpringBoot實(shí)現(xiàn)郵件發(fā)送的示例代碼
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-04-04idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析
這篇文章主要介紹了idea不能自動(dòng)補(bǔ)全yml配置文件的原因,通過(guò)添加yml文件為配置文件能夠很快的解決,具體解決步驟跟隨小編一起通過(guò)本文學(xué)習(xí)下吧2021-06-06200行java代碼實(shí)現(xiàn)2048小游戲
這篇文章主要為大家詳細(xì)介紹了200行java代碼實(shí)現(xiàn)2048小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04SpringCloud將Nacos作為配置中心實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Springcloud中的Nacos Config服務(wù)配置,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微博后臺(tái)登錄發(fā)送微博的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Java實(shí)現(xiàn)雙端鏈表LinkedList
本文主要介紹了Java實(shí)現(xiàn)雙端鏈表LinkedList,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入
這篇文章主要介紹了MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入的相關(guān)知識(shí),需要的朋友一起學(xué)習(xí)吧2016-01-01