栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring基础知识04

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring基础知识04

提示:这本文入门使用


文章目录

前言SpringAOP,事务

声明式事务和编程式事务 传播特性,隔离级别,超时时间,回滚设置


前言

提示:以下是本篇文章正文内容,下面案例可供参考

SpringAOP,事务

接上文,若是想只在xml中配置,如下。


before method=“start” pointcut"execution( Integer com.mashibing. service.MyCalculator.(…) " after method=“1ogFinally” pointcut="execution( Integer com.mashibing. service.MyCalculator.(.))‘X after-returning method"stop" pointcut="execution( Integer com.mashibing. service.MyCalculator.(…))" result=“e”
after-throwing method-"logException"pointcut-"execution( Integer com.mashibing. service.MyCalculator.
(…),exception=“e”
around method “around” pointcut="execution( Integer com.mashibing.service. MyCalculator.*(.) X/aop:arou’

与上文所言的效果一致,在实际公司项目中两者兼备,并无偏重。
也可以将切面抽象出来,作为每个切面内的私有引用。

@Test
public void test(){
JdbcTemplate jdbcTemplate = context. getBean( name:" jdbcTemplate",.class)
JdbcTemplate.String sql =“select * from emp where sal >?”;
List< Emp> result = jdbcTemplate. query(sql, new BeanPropertyRowMapper<>(Emp.class)
for (Emp emp : result){
System. out.println(emp);

声明式事务和编程式事务

在事务控制方面,主要有两个分类:
编程式事务∶在代码中直接加入处理事务的逻辑,可能需要在代码中显式调用beginTransaction()、 commit).rollback(等事务管理相关的方法
声明式事务:在方法的外部添加注解或者直接在配置文件中定义,将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。spring的AOP恰好可以完成此功能:事务管理代码的固定模式作为一种横切关注点,通过AOP方法模块化,进而实现声明式事务。
//导入事务管理模块





导入命名空间
xmlns:tx= http://www.springframswork.orgs/schema/tx"
http://www.sprinsframework..9ng.schema/tx
http://www.sprinsframework..9rg.schema/tx/spring-tx.xsd

@Transactional
public void buyBook(){
bookDao. getPrice( id: 1);
bookDao. updateBalance( userName:" zhangsan", price: 100);
int i= 1/0;
bookDao.updateStock( id: 1);
}

传播特性,隔离级别,超时时间,回滚设置

propagation:传播特性:
isolation:隔离级别:4种隔离级别,会引发不同的数据错乱问题
timeout:超时时间
readonly:只读事务,如果配置了只读事务,那么在事务运行期间,不允许对数据进行修改,否则抛出异常
//设置哪些异常不会回滚数据
noRollBackfor: noRollbackFor = {ArithmeticException.class}
设置哪些异常回滚
rollBackfor :
rollbackForClassName

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号