亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

Spring如何基于xml實現聲明式事務控制

瀏覽:53日期:2023-08-10 10:26:21

一、pom.xml

<?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>org.example</groupId> <artifactId>A02spring</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--https://mvnrepository.com/artifact/org.springframework/spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!--https://mvnrepository.com/artifact/org.springframework/spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!--https://mvnrepository.com/artifact/org.springframework/spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <!--https://mvnrepository.com/artifact/org.springframework/spring-context--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.8.RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration> <source>1.8</source> <target>1.8</target></configuration> </plugin> </plugins> </build></project>

二、spring的xml配置文件

<?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' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd'> <bean class='com.wuxi.services.impl.AccountServiceImpl'> <property name='accountDao' ref='accountDao'></property> </bean> <bean class='com.wuxi.daos.impl.AccountDaoImpl'> <property name='dataSource' ref='dataSource'></property> </bean> <bean class='org.springframework.jdbc.datasource.DriverManagerDataSource'> <property name='driverClassName' value='com.mysql.cj.jdbc.Driver'></property> <property name='url' value='jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false'></property> <property name='username' value='root'></property> <property name='password' value='123456'></property> </bean><!--spring中基于xml的聲明式事務控制配置步驟 1、配置事務管理器 2、配置事務的通知 3、配置aop中的通用切入點表達式 4、建立事務通知和切入點表達式的對應關系 5、配置事務的屬性--> <!--事務管理器--> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='dataSource'></property> </bean> <!--事務的通知--> <tx:advice transaction-manager='transactionManager'> <!-- 事務的屬性 isolation:用于指定事務的隔離級別。默認值是DEFAULE,表示使用數據庫的默認隔離級別。 propagation:用于指定事務的傳播行為。默認值是REQUIRED,表示一定會有事務,增刪改的選擇。查詢方法可以選擇SUPPORTYS。 read-only:用于指定事務是否只讀。只有查詢方法才能設置為true。默認值是false,表示讀寫。 timeout:用于指定事務的超時時間,默認值是-1,表示永不超時,如果指定了數值,以秒為單位。 rollback-for:用于指定一個異常,當產生該異常時,事務回滾,產生其他異常時,事務不回滾。沒有默認值。表示任何異常都回滾。 no-rollback-for:用于指定一個異常,當產生該異常時,事務不回滾,產生其他異常時事務回滾。沒有默認值。表示任何異常都回滾。 --> <tx:attributes> <tx:method name='*' propagation='REQUIRED' read-only='false'/> <tx:method name='find*' propagation='SUPPORTS' read-only='true'/> </tx:attributes> </tx:advice> <aop:config> <!--切入點表達式--> <aop:pointcut expression='execution(* com.wuxi.services.*.*(..))'/> <!--切入點表達式和事務通知的對應關系--> <aop:advisor advice-ref='txAdvice' pointcut-ref='ptc'></aop:advisor> </aop:config></beans>

三、實體類

package com.wuxi.beans;import lombok.Data;import java.io.Serializable;@Datapublic class Account implements Serializable { private Integer id; private String name; private Float money;}

四、dao

1、接口

package com.wuxi.daos;import com.wuxi.beans.Account;public interface AccountDao { Account findAccountById(Integer accountId); Account findAccountByName(String accountName); void updateAccount(Account account);}

2、實現類

package com.wuxi.daos.impl;import com.wuxi.beans.Account;import com.wuxi.daos.AccountDao;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public Account findAccountById(Integer accountId) { List<Account> accounts = getJdbcTemplate().query('select * from account where id = ?', new BeanPropertyRowMapper<Account>(Account.class), accountId); return accounts.isEmpty() ? null : accounts.get(0); } @Override public Account findAccountByName(String accountName) { List<Account> accounts = getJdbcTemplate().query('select * from account where name = ?', new BeanPropertyRowMapper<Account>(Account.class), accountName); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException('結果集不唯一'); } return accounts.get(0); } @Override public void updateAccount(Account account) { getJdbcTemplate().update('update account set name=?,money=? where id=?', account.getName(), account.getMoney(), account.getId()); }}

五、service

1、接口

package com.wuxi.services;import com.wuxi.beans.Account;public interface AccountService { Account findAccounById(Integer accountId); void transfer(String sourceName, String targetName, Float money);}

2、實現類

package com.wuxi.services.impl;import com.wuxi.beans.Account;import com.wuxi.daos.AccountDao;import com.wuxi.services.AccountService;public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public Account findAccounById(Integer accountId) { return accountDao.findAccountById(accountId); } @Override public void transfer(String sourceName, String targetName, Float money) { Account source = accountDao.findAccountByName(sourceName); Account target = accountDao.findAccountByName(targetName); source.setMoney(source.getMoney() - money); target.setMoney(target.getMoney() + money); accountDao.updateAccount(source); int i = 1 / 0; accountDao.updateAccount(target); }}

六、測試

package com.wuxi.tests;import com.wuxi.services.AccountService;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = 'classpath:application.xml')public class MySpringTest { @Autowired private AccountService as; @Test public void testTransfer() { as.transfer('aaa', 'bbb', 100f); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 99久久精品费精品国产一区二区 | 91久久精品 | 欧美久久久久久久久 | 国产99视频在线观看 | 久久久国产成人精品 | 成人欧美日韩视频一区 | 亚洲va久久久噜噜噜久久狠狠 | 国产成人精品免费视频大全可播放的 | 男人av的天堂 | 日韩成人在线观看 | 国产做a爰片久久毛片a | 俄罗斯aaaa一级毛片 | 在线观看免费a∨网站 | 精品久久久久久中文字幕一区 | 日韩日b视频 | 国产亚洲精品久久麻豆 | 全部免费毛片在线 | 成人免费一区二区三区在线观看 | 一区二区三区欧美日韩国产 | 国产日韩欧美在线一二三四 | 亚洲一级毛片免费看 | 美女全黄视频 | 久久精品全国免费观看国产 | 国产午夜精品理论片久久影视 | 久久夜色精品国产噜噜亚洲a | 成人午夜性a一级毛片美女 成人午夜亚洲影视在线观看 | 亚洲高清国产一区二区三区 | 26uuu天天夜夜综合 | 欧美日韩国产成人精品 | 亚洲久久久 | 国产在线91精品入口首页 | 久久久最新精品 | 一级国产 | 黄在线观看网站 | 看一级特黄a大片国产 | 中国一级毛片免费观看 | 乱人伦中文字幕视频 | 91久久网 | 日本韩国一区二区三区 | 91精品综合 | 国产日韩欧美综合在线 |