网站app建设方案3千元在家办厂

当前位置: 首页 > news >正文

网站app建设方案,3千元在家办厂,定制app开发平台,我想接app纯注册推广单spring#xff08;七#xff09;#xff1a;事务操作前言一、什么是事务二、事务四个特性#xff08;ACID#xff09;三、事务操作#xff08;搭建事务操作环境#xff09;四、事务操作#xff08;Spring 事务管理介绍#xff09;五、事务操作#xff08;注解声明式事… spring七事务操作前言一、什么是事务二、事务四个特性ACID三、事务操作搭建事务操作环境四、事务操作Spring 事务管理介绍五、事务操作注解声明式事务管理六、事务操作声明式事务管理参数配置1、Transactional2、propagation事务传播行为3、ioslation事务隔离级别4、timeout超时时间5、readOnly是否只读6、rollbackFor回滚7、noRollbackFor不回滚七、事务操作XML 声明式事务管理八、事务操作完全注解声明式事务管理前言 本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识有兴趣的小伙伴可以关注博主也许一个人独行可以走的很快但是一群人结伴而行才能走的更远 一、什么是事务 ⭕ 事务是数据库操作最基本单元逻辑上一组操作要么都成功如果有一个失败所有操作都失败 ⭕ 典型场景银行转账 lucy 转账 100 元 给 marylucy 少 100mary 多 100 二、事务四个特性ACID ⭕原子性 ⭕一致性 ⭕隔离性 ⭕持久性 三、事务操作搭建事务操作环境 ① 创建数据库表添加记录 ② 创建 service搭建 dao完成对象创建和注入关系 service 注入 dao在 dao 注入 JdbcTemplate在 JdbcTemplate 注入 DataSource Service public class UserService {//注入 daoAutowiredprivate UserDao userDao; } Repository public class UserDaoImpl implements UserDao {Autowiredprivate JdbcTemplate jdbcTemplate; }③ 在 dao 创建两个方法多钱和少钱的方法在 service 创建方法转账的方法 Repository public class UserDaoImpl implements UserDao {Autowiredprivate JdbcTemplate jdbcTemplate;//lucy 转账 100 给 mary//少钱Overridepublic void reduceMoney() {String sql update t_account set moneymoney-? where username?;jdbcTemplate.update(sql,100,lucy);}//多钱Overridepublic void addMoney() {String sql update t_account set moneymoney? where username?;jdbcTemplate.update(sql,100,mary);} }Service public class UserService {//注入 daoAutowiredprivate UserDao userDao;//转账的方法public void accountMoney() {//lucy 少 100userDao.reduceMoney();//mary 多 100userDao.addMoney();} }④ 上面代码如果正常执行没有问题的但是如果代码执行过程中出现异常有问题 public void accountMoney(int money){userDao.reduceMoney(money);int number 10 / 0;userDao.addMoney(money);}上面问题如何解决呢 使用事务进行解决 ⑤ 事务操作过程 //转账的方法public void accountMoney() { // try {//第一步 开启事务//第二步 进行业务操作//lucy少100userDao.reduceMoney();//模拟异常int i 100;//mary多100userDao.addMoney();//第三步 没有发生异常提交事务 // }catch(Exception e) {//第四步 出现异常事务回滚 // }}四、事务操作Spring 事务管理介绍 ⭕ 事务添加到 JavaEE 三层结构里面 Service 层业务逻辑层 ⭕ 在 Spring 进行事务管理操作 有两种方式 编程式事务管理和声明式事务管理使用 ⭕ 声明式事务管理 1基于注解方式使用 2基于 xml 配置文件方式 ⭕ 在 Spring 进行声明式事务管理底层使用 AOP 原理 ⭕ Spring 事务管理 API 1提供一个接口代表事务管理器这个接口针对不同的框架提供不同的实现类 五、事务操作注解声明式事务管理 1、在 spring 配置文件配置事务管理器 !–创建事务管理器– bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManager!–注入数据源–property namedataSource refdataSource/property /bean2、在 spring 配置文件开启事务注解 1在 spring 配置文件引入名称空间 tx beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/tx xsi: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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 2开启事务注解 !–开启事务注解– tx:annotation-driven transactionmanagertransactionManager/tx:annotation-driven3、在 service 类上面或者 service 类里面方法上面添加事务注解 1Transactional这个注解添加到类上面也可以添加方法上面 2如果把这个注解添加类上面这个类里面所有的方法都添加事务 3如果把这个注解添加方法上面为这个方法添加事务 Service Transactional public class UserService {六、事务操作声明式事务管理参数配置 1、Transactional 在 service 类上面添加注解Transactional在这个注解里面可以配置事务相关参数 2、propagation事务传播行为 1多事务方法直接进行调用这个过程中事务 是如何进行管理的 Service Transactional(propagation Propagation.REQUIRED)//默认也是Propagation.REQUIRED public class UserService {3、ioslation事务隔离级别 1事务有特性成为隔离性多事务操作之间不会产生影响。不考虑隔离性产生很多问题 2有三个读问题脏读、不可重复读、虚幻读 3脏读一个未提交事务读取到另一个未提交事务的数据 4不可重复读一个未提交事务读取到另一提交事务修改数据 5虚读一个未提交事务读取到另一提交事务添加数据 解决通过设置事务隔离级别解决读问题 Transactional(propagation Propagation.REQUIRED,isolation Isolation.REPEATABLE_READ) public class UserService {4、timeout超时时间 1事务需要在一定时间内进行提交如果不提交进行回滚 2默认值是 -1 设置时间以秒单位进行计算 5、readOnly是否只读 1读查询操作写添加修改删除操作 2readOnly 默认值 false表示可以查询可以添加修改删除操作 3设置readOnly 值是 true设置成 true 之后只能查询 6、rollbackFor回滚 1设置出现哪些异常进行事务回滚 7、noRollbackFor不回滚 1设置出现哪些异常不进行事务回滚 Transactional(readOnly false,timeout -1,propagation Propagation.REQUIRED,isolation Isolation.REPEATABLE_READ) public class UserService {} 七、事务操作XML 声明式事务管理 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/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd!– 组件扫描 –context:component-scan base-packagecom.atguigu/context:component-scan!– 数据库连接池 –bean iddataSource classcom.alibaba.druid.pool.DruidDataSourcedestroy-methodcloseproperty nameurl valuejdbc:mysql:///user_db /property nameusername valueroot /property namepassword valueroot /property namedriverClassName valuecom.mysql.jdbc.Driver //bean!– JdbcTemplate对象 –bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate!–注入dataSource–property namedataSource refdataSource/property/bean!–1 创建事务管理器–bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManager!–注入数据源–property namedataSource refdataSource/property/bean!–2 配置通知–tx:advice idtxadvice!–配置事务参数–tx:attributes!–指定哪种规则的方法上面添加事务–tx:method nameaccountMoney propagationREQUIRED/!–tx:method nameaccount/–/tx:attributes/tx:advice!–3 配置切入点和切面–aop:config!–配置切入点–aop:pointcut idpt expressionexecution( com.atguigu.spring5.service.UserService.*(..))/!–配置切面–aop:advisor advice-reftxadvice pointcut-refpt//aop:config /beans八、事务操作完全注解声明式事务管理 1、创建配置类使用配置类替代 xml 配置文件 Configuration //配置类 ComponentScan(basePackages com.atguigu) //组件扫描 EnableTransactionManagement //开启事务 public class TxConfig {//创建数据库连接池Beanpublic DruidDataSource getDruidDataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(com.mysql.jdbc.Driver);dataSource.setUrl(jdbc:mysql:///user_db);dataSource.setUsername(root);dataSource.setPassword(root);return dataSource;}//创建JdbcTemplate对象Beanpublic JdbcTemplate getJdbcTemplate(DataSource dataSource) {//到ioc容器中根据类型找到dataSourceJdbcTemplate jdbcTemplate new JdbcTemplate();//注入dataSourcejdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}//创建事务管理器Beanpublic DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {DataSourceTransactionManager transactionManager new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;} }测试 public void testAccount() {ApplicationContext context new AnnotationConfigApplicationContext(TxConfig.class);UserService userService context.getBean(userService, UserService.class);userService.accountMoney();}