网站开场flash怎么做的,p2p免费网站建设,成都抖音推广公司,wordpress mysuc cms目录
Spring事务操作-事务引入
1.模拟异常
2.测试异常
3.没有使用spring框架的时候异常该如何处理
4.使用spring框架的时候异常该如何处理
5.在spring 进行声明式事务管理#xff0c;底层使用AOP
6.spring 事务管理API
7.事务操作#xff08;注解声明式事务管理…目录
Spring事务操作-事务引入
1.模拟异常
2.测试异常
3.没有使用spring框架的时候异常该如何处理
4.使用spring框架的时候异常该如何处理
5.在spring 进行声明式事务管理底层使用AOP
6.spring 事务管理API
7.事务操作注解声明式事务管理
1在spring的配置文件中配置事务管理器
2在spring 配置文件中开启事务注解
3在service 类上面或者service类里面的方法上面 添加事务注解
4Transactional这个注解添加到类上面也可以添加到方法上面
5测试 上一章的代码如果正常执行 不会出现问题 如果在代码执行过程中出现异常会出现问题 因此我们引入事务这个概念 Spring事务操作-事务引入 1.模拟异常
在业务逻辑层中的service层中模拟异常
package org.example.spring.service;import org.example.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class UserService {Autowiredprivate UserDao userDao;// 转账方法调用public void accountMoney(){//lucy少100userDao.reduceMoney();//模拟异常int i10/0;//mary多100userDao.addMoney();}}2.测试异常
原来的数据库值 后来的数据库值 分析lucy的钱少了mary 的钱却没有增加 3.没有使用spring框架的时候异常该如何处理
package org.example.spring.service;import org.example.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class UserService {Autowiredprivate UserDao userDao;// 转账方法调用public void accountMoney(){
// 1.开启事务try{
// 2.进行业务操作//lucy少100userDao.reduceMoney();//模拟异常int i10/0;//mary多100userDao.addMoney();// 3.没有发生异常提交事务}catch(Exception e){
// 4.如果发生了异常从第3跳到第4进行事务回滚}}}4.使用spring框架的时候异常该如何处理
1事务添加到javaEE三层结构里面的service层 业务逻辑层
2在spring 进行事务管理操作有两种方式
【第一种】编程式事务管理相当于3. 里面的操作
【第二种】声明式事务管理常用 1.基于注解方式常用 2.基于xml 配置方式 5.在spring 进行声明式事务管理底层使用AOP 6.spring 事务管理API
1spring 提供了一个接口代表是事务管理器事务操作都封装在里面 这个接口针对不同的框架提供不同的实现类例如以下的红框就是整合JDBC框架的子类 实现类 7.事务操作注解声明式事务管理
1在spring的配置文件中配置事务管理器
?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: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 http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd!-- 开启组件扫描--context:component-scan base-packageorg.example/context:component-scan
!--数据库连接池--bean iddataSource classcom.alibaba.druid.pool.DruidDataSource destroy-methodcloseproperty nameurl valuejdbc:mysql://localhost:3306/user_db?useSSLfalseamp;useUnicodetrueamp;characterEncodingUTF-8/property nameusername valueroot/property namepassword valuesise/property namedriverClassName valuecom.mysql.jdbc.Driver//bean!-- 创建jdbcTemplate对象--bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate
!--需要注入数据源信息--property namedataSource refdataSource/property/bean!-- 创建事务管理器--
bean iddataSourceTransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource/property
/bean
/beans 2在spring 配置文件中开启事务注解
引入名称空间tx 开启事务注解 3在service 类上面或者service类里面的方法上面 添加事务注解 4Transactional这个注解添加到类上面也可以添加到方法上面 如果把这个注解添加类上面这个类里面所有的方法都添加事务 如果把这个注解添加方法上面仅仅为这个方法添加事务 package org.example.spring.service;import org.example.spring.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 {Autowiredprivate UserDao userDao;// 转账方法调用public void accountMoney(){
//1.开启事务
// try{
// 2.进行业务操作//lucy少100userDao.reduceMoney();//模拟异常int i10/0;//mary多100userDao.addMoney();// 3.没有发生异常提交事务// }catch(Exception e){
// 4.如果发生了异常从第3跳到第4进行事务回滚// }}}5测试
原来数据 后来数据不变 只是在控制台提示报错信息