Spring框架入门-----事务

  • 事务的回滚
    • 在这里我用一个数据库的用户扣钱,加钱的例子:
      我随便建了一个数据库,里面有两个用户:
      在这里插入图片描述

然后在pom.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
        <!-- 这里是数据库资源文件的位置 -->
	<context:property-placeholder location="classpath:properties/jdbc.properties"/>
	
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName"
			value="${jdbcDriver}" />
		<property name="url" value="${jdbcUrl}" />
		<property name="username" value="${jdbcUsername}" />
		<property name="password" value="${jdbcPassword}" />
	</bean> 
</beans>

然后一个main方法:

ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("classpath:lesson04/prop.xml");
		DataSource bean = (DataSource) cac.getBean("dataSource");
		Connection c = bean.getConnection();
		try {
			String sql1 = "UPDATE money SET lmoney=lmoney-50 WHERE id=1";
			String sql2 = "UPDATE money SET lmoney=lmoney+50 WHERE id=2";
			c.setAutoCommit(false);// 设置手动提交
			c.createStatement().execute(sql1);// 自动提交
			int i = 5/0;
			c.createStatement().execute(sql2);
			c.commit();
		} catch (Exception e) {
			// TODO: handle exception
			c.rollback();
		}

还是上面那个例子,注解版在来一发:
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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://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
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
        <!-- 扫描注解 -->
	<context:component-scan
		base-package="com.ps.lesson04"></context:component-scan>
	<context:property-placeholder
		location="lesson04/jdbc.properties"></context:property-placeholder>
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbcDriver}" />
		<property name="url" value="${jdbcUrl}" />
		<property name="username" value="${jdbcUsername}" />
		<property name="password" value="${jdbcPassword}" />
	</bean>
	<!-- jdbc的模板类 通常用于操作 增删查改 注入数据源 -->
	<bean id="jdbc"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<!-- 事物管理器 用于提交和回滚 -->
	<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	 <aop:config >
		<!-- 切点一般都是拦截所有的service包 所有接口 所有方法 -->
		<aop:pointcut expression="execution(* com.ps.lesson04.*.transfer*(..))" id="aop"/>
		<aop:advisor pointcut-ref="aop" advice-ref="txAdvice"/>
	</aop:config>
	
	<!-- 通知       一般数据库的sql操作异常都是操作型异常
		所有定义的事务 运行时异常回滚 检查异常不会回滚
		rollback-for="你要回滚的检查异常"
		rollback-for="java.lang.Exception" >>>>>代表所有的检查异常会回滚
		
		no-rollback-for="什么样的运行时异常不回滚"
		no-rollback-for="java.lang.RuntimeException"  >>>>>代表所有的运行异常不会回滚
	 -->
	
	<tx:advice id="txAdvice" transaction-manager="tm">
		<tx:attributes>
		<!-- propagation:事务的传播特性
			read-only:禁止事务默认false
		 -->
			<tx:method name="transfer*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED"/>
			<!-- 除了上面所有定义的方法 以外都不用事物 -->
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice> 
	

</beans>

这里的代码可以复制,但是里面的路径可能就要自己改了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Flex是一个优雅的MyBatis增强框架,它非常轻量、性能高且灵活。我们可以使用MyBatis-Flex轻松地连接任何数据库,并且它内置的QueryWrapper可以帮助我们减少SQL编写的工作量,同时减少出错的可能性。\[1\] 如果项目未使用SpringBoot,只使用了Spring框架,需要参考MyBatis-Flex的FlexTransactionAutoConfiguration进行事务配置,才能正常使用@Transactional注解。\[2\] 而如果项目使用了SpringBoot,MyBatis-Flex已经支持Spring框架的@Transactional注解,可以直接使用@Transactional进行事务管理。同样,使用Spring的TransactionTemplate进行事务管理也是可行的。\[3\] #### 引用[.reference_title] - *1* [Mybatis-Flex快速入门教程](https://blog.csdn.net/qq_19309473/article/details/130417630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [探秘MyBatis-Flex:超越Mybatis-plus的优雅魅力!](https://blog.csdn.net/weixin_42084197/article/details/131862690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值