Spring基础知识04

提示:这本文入门使用



前言


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

SpringAOP,事务

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

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

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

@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方法模块化,进而实现声明式事务。
//导入事务管理模块

<bean id="transactionManager" class="org. springframework. jdbc. datasource. DataSource.class">
<property name="dataSource" ref "dataSource"</property>
</bean>

导入命名空间
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
在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值