用什么软件建网站最方便,seo短视频网页入口引流在线观看网站,网站建设教程网哪个好,少儿编程加盟店一、概述 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的一种方式#xff0c;Spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明#xff0c;是指在配置文件中声明#xff0c;用在Spring配置文件中声明式的处理事务来…一、概述 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的一种方式Spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明是指在配置文件中声明用在Spring配置文件中声明式的处理事务来代替业务逻辑中使用代码处理事务这样做的好处是事务管理不侵入开发的组件具体来说就是业务逻辑对象不会意识到自己正在事务管理之中事实上也应该如此因为事务管理是属于系统层面的服务而不是业务逻辑的一部分如果想要改变事务管理策略的话也只需要在配置文件中修改配置即可在不需要事务管理的时候修改一下配置信息即可移除事务管理服务无需改变代码重新编译这样维护起来也很方便Spring中是使用aop来完成声明式事务管理的因而声明式事务是以方法为单位的也即声明式事务是作用在业务方法上的。 二、声明式事务xml方式环境搭建
2.1、项目概览 2.2、配置思路 第一步配置事务管理器 第二步配置事务要处理的方法注意事项一旦配置了事务方法名称规则后service中的方法一定要按照这里配置的名称否则事务配置不生效 第三步配置aop 2.3、pom.xml 同 系列四 pom.xml 2.4、applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd!-- 组件扫描 --context:component-scan base-packageorg.star/!-- 数据源 --context:property-placeholder locationdb.properties/bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName value${db.driver}/property nameurl value${db.url}/property nameusername value${db.username}/property namepassword value${db.password}//bean!--配置sqlSessionFactory读取配置文件获取数据库相关的信息--bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/propertyproperty nametypeAliasesPackage valueorg.star.entity.model/propertyproperty namemapperLocations valueclasspath:mapper/*.xml/propertyproperty nameconfigurationbean classorg.apache.ibatis.session.Configurationproperty namelogImpl valueorg.apache.ibatis.logging.stdout.StdOutImpl/property/bean/property!-- 分页插件 --property namepluginsarraybean classcom.github.pagehelper.PageInterceptorproperty nameproperties!--reasonabletrue解释如果用户输入的不合理的pageSize参数pageHelper会自动进行调整--valuehelperDialectmysqlreasonabletrue/value/property/bean/array/property/bean!--配置mapper接口的位置并指定sqlSessionFactorysqlSession sqlSessionFactory.openSession();mapper sqlSession.getMapper();--bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valueorg.star.mapper/propertyproperty namesqlSessionFactoryBeanName valuesqlSessionFactory/property/bean!-- 配置事务管理器 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource/property/bean!--声明式事务配置spring中的事务是基于aop实现但使用的不是通知而是拦截器--tx:advice idtxAdvice transaction-managertransactionManager!-- 定义事务的对象信息 --tx:attributes!-- transfer*表示以transfer开头的所有方法 --tx:method nametransfer* propagationREQUIRED isolationDEFAULT//tx:attributes/tx:advice!-- aop配置 --aop:configaop:pointcut idtxPointCut expressionexecution(* org.star.service.*.*(..))/aop:advisor advice-reftxAdvice pointcut-reftxPointCut/aop:advisor/aop:config/beans
2.5、db.properties 同 系列四 db.properties 2.6、AccountDO.java 同 系列四 AccountDO.java 2.7、AccountMapper.java 同 系列四 AccountMapper.java 2.8、AccountMapper.xml 同 系列四 AccountMapper.xml 2.9、AccountService.java 同 系列四 AccountService.java 2.10、AccountServiceImpl.java
/*** Author : 一叶浮萍归大海* Date: 2023/11/24 8:25* Description:*/
Service
public class AccountServiceImpl implements AccountService {Resourceprivate AccountMapper accountMapper;/*** 转账** return*/Overridepublic void transferMoney() {try {// Jack 转出100元accountMapper.accountExpenditure(Jack, 100);// 模拟异常int i 10 /0;// Rose 入账100元accountMapper.accountEntry(Rose, 100);} catch (Exception e) {throw new RuntimeException(e);}}
}
2.11、SpringJunitTest.java 同 系列四 SpringJunitTest.java