1.事务介绍
1.1 事务添加到javaEE的service
1.2 声明式事务和编程试
1.3 声明式
xml 事务开发
注解方式
1.4 在Spring中进行事务开发底层用的aop原理
1.5 Spring事务管理Api
2. 步骤
2.1 开启事务管理器
2.2 开始事务注解
2.3 在service层中加入,可以加载类上面,也可以加在方法上面
<?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" 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"> <context:component-scan base-package="com.demo"> </context:component-scan> <!-- 引入外部属性文件--> <context:property-placeholder location="classpath*:jdbc.properties"/> <!-- 数据库连接池--> <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${prop.driverClassName}"></property> <property name="url" value="${prop.url}"/> <property name="username" value="${prop.username}"/> <property name="password" value="${prop.password}"/> </bean> <!-- jdbc模板对象--> <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="druid"></property> </bean> <!-- 开启事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="druid"></property> </bean> <!-- 开启注解--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
package com.demo.service; import com.demo.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class UserService { @Autowired private UserDao userDao; public void account(){ userDao.addMoney(); int a = 10/0; userDao.reduceMoney(); } }
标签:事务,userDao,service,springframework,org,import,spring5 From: https://www.cnblogs.com/cciscc/p/16609853.html