日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

spring基于注解的声明式事务控制

發(fā)布時(shí)間:2024/4/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring基于注解的声明式事务控制 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day04_learn_06tx_anno</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.45</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies></project> package com.learn.dao;import com.learn.domain.Account;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 根據(jù)Id查詢賬戶* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 根據(jù)名稱查詢賬戶* @param accountName* @return*/Account findAccountByName(String accountName);/*** 更新賬戶* @param account*/void updateAccount(Account account); } package com.learn.dao.impl;import com.learn.dao.IAccountDao; import com.learn.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;import java.util.List;/*** 賬戶的持久層實(shí)現(xiàn)類*/ @Repository("accountDao") public class AccountDaoImpl implements IAccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public Account findAccountById(Integer accountId) {List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);return accounts.isEmpty()?null:accounts.get(0);}public Account findAccountByName(String accountName) {List<Account> accounts = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);if(accounts.isEmpty()){return null;}if(accounts.size()>1){throw new RuntimeException("結(jié)果集不唯一");}return accounts.get(0);}public void updateAccount(Account account) {jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());} } package com.learn.domain;import java.io.Serializable;/*** 賬戶的實(shí)體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';} } package com.learn.service;import com.learn.domain.Account;/*** 賬戶的業(yè)務(wù)層接口*/ public interface IAccountService {/*** 根據(jù)id查詢賬戶信息* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 轉(zhuǎn)賬* @param sourceName 轉(zhuǎn)成賬戶名稱* @param targetName 轉(zhuǎn)入賬戶名稱* @param money 轉(zhuǎn)賬金額*/void transfer(String sourceName, String targetName, Float money); } package com.learn.service.impl;import com.learn.dao.IAccountDao; import com.learn.domain.Account; import com.learn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類** 事務(wù)控制應(yīng)該都是在業(yè)務(wù)層*/ @Service("accountService") @Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只讀型事務(wù)的配置 public class AccountServiceImpl implements IAccountService{@Autowiredprivate IAccountDao accountDao;public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}//需要的是讀寫(xiě)型事務(wù)配置@Transactional(propagation= Propagation.REQUIRED,readOnly=false)public void transfer(String sourceName, String targetName, Float money) {System.out.println("transfer....");//2.1根據(jù)名稱查詢轉(zhuǎn)出賬戶Account source = accountDao.findAccountByName(sourceName);//2.2根據(jù)名稱查詢轉(zhuǎn)入賬戶Account target = accountDao.findAccountByName(targetName);//2.3轉(zhuǎn)出賬戶減錢source.setMoney(source.getMoney()-money);//2.4轉(zhuǎn)入賬戶加錢target.setMoney(target.getMoney()+money);//2.5更新轉(zhuǎn)出賬戶accountDao.updateAccount(source);// int i=1/0;//2.6更新轉(zhuǎn)入賬戶accountDao.updateAccount(target);} } <?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置spring創(chuàng)建容器時(shí)要掃描的包--><context:component-scan base-package="com.learn"></context:component-scan><!-- 配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置數(shù)據(jù)源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!-- spring中基于注解 的聲明式事務(wù)控制配置步驟1、配置事務(wù)管理器2、開(kāi)啟spring對(duì)注解事務(wù)的支持3、在需要事務(wù)支持的地方使用@Transactional注解--><!-- 配置事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 開(kāi)啟spring對(duì)注解事務(wù)的支持--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans> package com.learn.test;import com.learn.service.IAccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 使用Junit單元測(cè)試:測(cè)試我們的配置*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:bean.xml") public class AccountServiceTest {@Autowiredprivate IAccountService as;@Testpublic void testTransfer(){as.transfer("aaa","bbb",100f);}}

?

總結(jié)

以上是生活随笔為你收集整理的spring基于注解的声明式事务控制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。