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

做个什么样的网站比较好深圳住建局官网登录入口

做个什么样的网站比较好,深圳住建局官网登录入口,wordpress能做app,网页制作软件推荐目录 基于AOP的声明事务控制 Spring事务编程概述 搭建测试环境 基于xml声明式事务控制 详解 事务增强的AOP 平台事务管理器 Spring提供的Advice#xff08;重点介绍#xff09; 原理 #xff08;源码没有翻太明白#xff09; 基于注解声明式事务控制 基于AOP的声明…目录 基于AOP的声明事务控制 Spring事务编程概述 搭建测试环境 基于xml声明式事务控制 详解 事务增强的AOP 平台事务管理器 Spring提供的Advice重点介绍 原理 源码没有翻太明白 基于注解声明式事务控制 基于AOP的声明事务控制 Spring事务编程概述 事务是开发过程中必不可少的东西使用JDBC开发时我们使用connection对事务进行控制使用MyBatis时我们使用SqlSession对事物进行控制缺点显而易见当我们切换数据库访问技术时事务控制的方法总会改变。Spring就在这些技术的基础上提供了统一的控制事务接口。Spring的事务分为编程式事务和声明式事务控制。 事务控制方式解释编程式事务控制Spring提供了事务控制的类和方法使用编程的方法对业务代码进行事务控制事务控制代码和业务操作代码耦合在一起开发中不使用声明式事务Spring将事务控制的代码封装起来对外提供xml和注解的配置方式通过配置的方式完成事务的控制可以达到事务控制和业务操作代码的解耦开发中推荐使用 Spring事务编程相关的类主要有以下三个 类名功能PlatformTransactionManager平台事务管理器抽象了不同的事务技术如 JDBC、JTA下的事务管理器。它定义了事务的开始、提交和回滚等操作接口由具体实现提供相应的实现。Spring 提供了多种实现不同持久层有不同实现方案如 DataSourceTransactionManager、HibernateTransactionManager 和 JpaTransactionManager 等。TransactionDefinition事务定义用于定义事务的隔离级别、超时时间等属性。Spring 定义了多种常量值如 ISOLATION_DEFAULT、ISOLATION_READ_COMMITTED、ISOLATION_REPEATABLE_READ 等隔离级别TIMEOUT_DEFAULT、TIMEOUT_NONE 等超时时间。TransactionStatus事务状态包括是否新事务、是否已完成、是否回滚等状态。将该状态对象传递给事务管理器的 commit() 或 rollback() 方法可以控制事务的提交或回滚操作。虽然编程式事务控制我们不学习但是编程式事务控制对应的类我们需要理解一下因为我们在通过配置的方式进行声明式事务控制时也会看到这些类的影子。 搭建测试环境 搭建转账的环境dao层一个转出钱的方法service是一个转账业务的方法内部分别调用dao层转出钱和转入钱的方法准备工作如下 数据库准备一个账户表tb_account dao层准备一个AccountMapper包括incrMoney和decrMoney两个方法 package com.example.Mapper;import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; import org.springframework.stereotype.Repository;Repository(accountMapper) public interface AccountMapper {Update(update tb_account set money money #{money} where account_name#{accountName})public void incrMoney(Param(accountName) String accountName, Param(money) Integer money);Update(update tb_account set money money - #{money} where account_name#{accountName})public void decrMoney(Param(accountName) String accountName, Param(money) Integer money); }service层作为目标类使用了注解的方式将其交给Spring容器管理不需要再到xml配置文件中去配置准备一个transferMoney方法分别调用incrMoney和decrMoney方法 package com.example.Service.ServiceImpl;import com.example.Mapper.AccountMapper; import com.example.Service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service(accountService) public class AccountServiceImpl implements AccountService {Autowiredprivate AccountMapper accountMapper;Overridepublic void transferMoney(String outAccount, String inAccount, Integer money) {accountMapper.decrMoney(outAccount, money);System.out.println(outAccount 转出 money 元);accountMapper.incrMoney(inAccount, money);System.out.println(inAccount 转入 money 元);} }在applicationContext文件中进行Bean的管理配置 ?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd!-- 指定Spring组件扫描范围--context:component-scan base-packagecom.example/!--配置数据源信息--bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/db02/property nameusername valueroot/property namepassword value123456//bean!-- 配置SqlSessionFactoryBean,作用将SqlSessionFactoryBean存储到Spring容器中--bean classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource//beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.example.Mapper//bean/beans测试正常转账和异常转账 正常测试代码 package com.example.Test;//import com.example.Config.MyBatisConfig; import com.example.Service.AccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAccount {public static void main(String[] args) { // ApplicationContext context new AnnotationConfigApplicationContext(MyBatisConfig.class);ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);AccountService accountService (AccountService) context.getBean(accountService);accountService.transferMoney(tom, lucy, 500);} }运行结果如下 错误测试在业务层中加入错误  package com.example.Service.ServiceImpl;import com.example.Mapper.AccountMapper; import com.example.Service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service(accountService) public class AccountServiceImpl implements AccountService {Autowiredprivate AccountMapper accountMapper;Overridepublic void transferMoney(String outAccount, String inAccount, Integer money) {accountMapper.decrMoney(outAccount, money);System.out.println(outAccount 转出 money 元);int i 1 / 0;accountMapper.incrMoney(inAccount, money);System.out.println(inAccount 转入 money 元);} }同样运行上述测试代码 运行结果如下出现数据无法对应 基于xml声明式事务控制 综合我们上面学到的AOP技术很容易想到可以使用AOP对Service的方法进行事务增强 目标类自定义的AccounServiceImpl内部的方法是切点。不需要在xml配置文件中再次进行配置已经使用注解方式将其交给Spring容器管理。通知类Spring提供的通知方法已经定义好只需配置即可。分析 通知类是Spring提供的需要导入Spring事务相关的坐标配置目标类AccountServiceImpl使用advisor标签配置切面具体的配置文件如下 ?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd!-- 指定Spring组件扫描范围--context:component-scan base-packagecom.example/!--配置数据源信息--bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/db02/property nameusername valueroot/property namepassword value123456//bean!-- 配置SqlSessionFactoryBean,作用将SqlSessionFactoryBean存储到Spring容器中--bean classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource//beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.example.Mapper//bean!-- todo 以下是使用AOP进行事务管理部分的配置--!-- 配置平台事务管理器--bean nametransactionManager1 classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean!-- 配置Spring提供的通知advice--tx:advice idAccountServiceAdvice transaction-managertransactionManager1tx:attributestx:method name*//tx:attributes/tx:advice!-- 事务增强的AOP--aop:config!-- 配置切点表达式--aop:pointcut idAccountServiceexpressionexecution(* com.example.Service.ServiceImpl.AccountServiceImpl.*(..))/!-- 配置织入关系通知引用Spring所提供的--aop:advisor advice-refAccountServiceAdvice pointcut-refAccountService//aop:config/beans对于事务、AOP、代理对象的产生等相关知识可以前往我的主页进行关键字搜索自行查阅。 上述配置文件在最初配置文件的基础之上使用AOP将目标方法进行事务管理具体的做法如下 首先设置切点表达式确定目标方法后然后将切点表达式和Spring提供的通知进行织入而此时需要配置Spring提供的通知在配置该通知时需要配置事务平台管理器最后将事务平台管理器配置好之后就完成了对于目标方法的事务管理。事务平台管理器的作用 事务的创建事务平台管理器通过获取事务配置从而创建一个新的事务对象用于执行目标方法。 事务的提交在目标方法正常执行结束后事务平台管理器会将事务提交从而将所有修改操作永久保存到数据库。 事务的回滚在目标方法执行发生异常时事务平台管理器会自动回滚事务将所有修改操作撤销保持系统数据的一致性。 事务的传播行为事务平台管理器可以根据配置将事务从一个方法传播到另一个方法以保证不同方法之间的数据一致性。 事务隔离级别事务平台管理器可以设置事务的隔离级别从而决定不同事务之间可见的数据范围和并发度等问题。 Spring 支持多种事务平台管理器例如 DataSourceTransactionManager、HibernateTransactionManager、JpaTransactionManager 等。不同的事务平台管理器对应着不同的持久化技术和数据库访问框架在使用时需要注意选择合适的方案。 当我们使用 Spring 进行事务管理时事务平台管理器就是整个事务处理流程中的核心组件。它通过管理和控制事务保证了数据操作的原子性、一致性、隔离性和持久性等特性从而维护了数据库的完整性和系统的稳定性。 再次运行最初存在人为错误的情况下测试代码运行结果如下   数据一致性得到了保障AOP进行事务管理成功 详解 基于上述配置文件中使用AOP配置事务管理的配置来对事务管理的配置进行详解 事务增强的AOP 事务增强的AOP部分在往期介绍AOP的文章中已经进行详细的阐述了具体可以查阅Spring专栏中的AOP部分的相关文章。 平台事务管理器 平台事务管理器是一个接口实现该接口取决于当前DAO持久层使用的框架是什么不同的框架实现不同的接口正如上述配置中显示我们使用的是JDBC框架 其对应的平台事务管理器是DataSourceTransactionManagerMyBatis框架也是如此因为MyBatis底层实现也是JDBC进行操作的。  Spring提供的Advice重点介绍 主要配置事务的通知建议/advice而对于事务的操作则通过平台事务管理器实现所以引入上述配置的平台事务管理器。主要介绍不同方法的事务属性中的配置重点 其中method标签用于配置不同方法的事务属性 name属性指定方法名称*表示通配符 add*对应所有以add开头的方法此处需要区分的是切点表达式中指定的方法与此处的指定的方法有什么区别?切点表达式中是过滤哪些方法要解析AOP提供的事务管理增强事务属性信息的name是指定这写过滤后的方法哪些进行事务属性的配置。isolation属性指定事务的隔离级别事务并存在三大问题脏读不可重复读幻读/虚读。可以通过设置事务的隔离级别来保证并发问题的出现常用的是READ_COMMITTED和PEPEATABLE_READ。 isolation解释DEFAULT默认隔离级别取决于当前数据库隔离级别例如MySQL默认隔离级别是PEPEATABLE_READREAD_UNCOMMITTEDA事务可以读取B事务尚未提交的事务记录不能解决任何并发问题安全性最顶性能最高READ_COMMITTEDA事务只能读取到其它事务已经提交的事务不能提取到未提交的记录可以解决脏读问题但不可以解决不可重复读和幻读问题。PEPEATABLE_READA事务多次从数据库读取某条记录结果一致可以解决不可重复读、不可以解决幻读SERIALIZABLE串行化可以解决任何并发问题安全性最高但性能最低。timeout超时时间访问数据库的时间做出限制默认值为-1即没有超时时间单位为sread-only是否只读数据库默认值为false查询操作指定为只读操作propagation事务的传播行为主要解决A方法调用B方法时事务的传播方式的问题例如使用单方的事务还是A和B都是用自己的事务。解决事务嵌套问题即两业务方法都有事务调用谁的事务的问题。可以参照Java Web专栏中的文章事务管理-事务进阶-propagation属性-CSDN博客 事务传播行为解释前提都是A方法调用B方法REQUIRED默认值 A调用B,B需要事务如果A有事务B就加入A事务中如果A没有事务B就自己创建一个事务。 REQUIRED_NEWA调用BB需要新事物如果A有事务就挂起B自己创建一个新事务SUPPORTSA调用BB有无事务无所谓A有事务就加入到A事务中A无事务B就以非事务的方式执行NOT_SUPPORTS A调用BB就以无事务的方式进行A如果有事务就挂起。 NEVERA调用BB以无事务方式执行A如果有事务就抛出异常MANDATORYA调用BB要加入A的事务中如果A无事务就抛出异常NESTED A调用BB创建一个新事务A有事务就作为嵌套事务存在A没有事务就以创建的新事物执行 原理 源码没有翻太明白 目前跟着翻阅源代码还不是很清楚先学后面的 基于注解声明式事务控制 使用注方式就是对于XML配置文件中配置的替代。在老版本的Spring框架中还需要在配置文件中设置    tx:annotation-driven/从而注解生效。使用的是 Transactional将注解使用在类上则是将类中的所有方法都进行事务管理注解使用在方法上则是对该方法进行事务管理   使用全注解来对上述案例进行汇总 核心配置类 package com.example.Config;import com.alibaba.druid.pool.DruidDataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;//注解配置类替代配置文件,实现纯注解开发 Configuration // 表示该类是一个核心配置类同时将该类交给Spring容器管理内置了Component注解 ComponentScan({com.example})//context:component-scan base-packagecom.example/ PropertySource({classpath:jdbc.properties}) // 加载外部properties文件 MapperScan(com.example.Mapper) //加载对应的Mapper接口类 //EnableTransactionManagement Spring版本过低可能需要该注解 public class SpringConfig {Beanpublic DataSource dataSource(Value(${jdbc.driver}) String driver,Value(${jdbc.url}) String url,Value(${jdbc.username}) String username,Value(${jdbc.password}) String password) {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}Beanpublic SqlSessionFactoryBean SqlSessionFactory(DataSource dataSource) {SqlSessionFactoryBean sqlSessionFactoryBean new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource) {DataSourceTransactionManager dataSourceTransactionManager new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;} }使用全注解的方式则获取Spring容器的方式也需要相应改变测试类代码如下 package com.example.Test;//import com.example.Config.MyBatisConfig;import com.example.Config.SpringConfig; import com.example.Service.AccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestAccount {public static void main(String[] args) {ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService (AccountService) context.getBean(accountService);accountService.transferMoney(tom, lucy, 500);} }
http://www.zqtcl.cn/news/741250/

相关文章:

  • 花生壳内网穿透网站如何做seo优化鞍山58同城网
  • 怎么为一个网站做外链跨境电商app
  • 医疗网站不备案seo技巧课程
  • 网页和网站有什么区别湖南省郴州市邮编
  • 公考在哪个网站上做试题武威做网站的公司
  • 河南如何做网站常州网站建设价位
  • 昆山网站建设培训班成都百度
  • 兰山网站建设郑州最好的网站建设
  • 手机网站后台源码枣庄市建设局网站
  • 网站建设傲鸿wordpress 获取分类下的文章
  • 网站运行速度优化wordpress国内优化
  • wordpress全站网易云音乐播放网站建设案例公司
  • 湘潭网站建设多少钱 报价表湘潭磐石网络北京百度seo点击器
  • 什么做的网站电子商务网站的建设的原理
  • 河北建站科技网络公司媒体平台
  • 做同城信息类网站如何赚钱石景山网站建设多少钱
  • 用ip的网站要备案吗网站的建设维护及管理制度
  • dedecms 百度网站地图南宁比优建站
  • 沈阳大熊网站建设制作怎么增加网站的权重
  • 网站建设 价格低建设网站大约多少钱
  • 好看简单易做的网站北京网站建设华网天下定制
  • 黑群晖做网站云主机搭建网站
  • 网站首页默认的文件名一般为微信app网站建设
  • 珠海网站建设方案优化一个企业是如何做网站建设的
  • 重庆网站建设重庆最加科技潜江人才网官网
  • 网站建设规划书百度文库陕西做网站找谁
  • 昆明网站的优化网站访问跳出率
  • 四川省建设厅官方网站三内人员沈阳医大男科怎么样
  • 个性个人网站模板公司电子版简介模板
  • 网站建设百度做棋牌网站多少钱