04Spring jdbc and transaction control

1、Spring JDBC

Spring JDBC database module is responsible for resource management and error handling, greatly simplifies the operation of the database developers, enables developers freed from tedious database operations, thus more energy into writing business logic.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-3Gw4OPuE-1579010543898) (01.png)]

1.1, Spring JdbcTemplate resolution

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-lpOdGDcb-1579010543900) (02.png)]
For the operation of the database, the Spring Framework provides JdbcTemplate class, which is the basis for the Spring framework data abstraction layer, so that other higher-level abstract class is built on JdbcTemplate class. It can be said, JdbcTemplate class is the core class Spring Jdbc

1.2, Spring JDBC configuration

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-iq5dlOdY-1579010543902) (03.png)]
Here is a configuration JdbcTemplate in xml configuration file:
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-EIS0zVA6-1579010543902) (04.png)]

1.3, Spring JdbcTemplate common method

It provides a number of methods to update and query the database in JdbcTemplate class:
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-oEsJaAgx-1579010543903) (05.png)]

1.3.1, execute () - execute SQL statements

1.3.2, update () - Update Data

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-p64gxPkF-1579010543904) (07.png)]



[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-yngqSpBB-1579010543905) (08.png)]

1.3.3query () - query data

Note BeanProperty RowMapper interface class is implemented, can automatically be mapped to a data table in the user-defined class (if class is a user-defined fields and to the data table corresponding to a field)
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-nWFxuLwT-1579010543906) (09.png)]

2, Transcation transaction control

Spring's transaction management simplifies the traditional transaction management processes, and reducing the workload of developers to some extent.

2.1, the concept of

2.1.1 transaction management core interface

JAR package comprising a spring-tx-4.3.6.RELEASE named in the Spring of all JAR package, the package is provided for dependencies Spring transaction management. Comprising three interface file in the JAR package org.Springframework.transcation packet: PlatformTranscationManager, TranscationDefinition and TranscationStatus.

2.1.1.1PlatFormTranscationManager

该接口是Spring提供的平台事务管理器,主要用于管理事务。该接口中提供了三个事务操作的方法,具体如下:

  • TranscationStatus getTranscation(TransactionDefinition defition):用于获取事务状态信息。该方法会根据TransactionDefinition参数返回一个TransactionStatus对象,TransactionStatus对象表示一个事务,被关联在当前执行的线程上。

    void commit(TransactionStatus status):用于提交事务
    	
    	void rollback(TransactionStatus status):用于回滚事务
    
  • PlatFormTransactionManager接口只是代表事务管理的接口,并不知道底层是如何管理事务的,它只需要事务管理提供上面的3个方法,但具体如何管理事务则由他们的实现类完成。

  • PlatFormTransaction接口有许多不同的实现类,常见的几个实现类如下:

    DataTransactionManager:用于配置JDBC数据源的事务管理器

    HibernateTransactionManager:用于配置Hibernate的事务管理器

    JtaTransactionManager:用于配置全局事务管理器

    当底层采用不同的持久层技术时,系统只需要使用不同的PlatformTransactionManager实现类即可。

2.1.2TransactionDefinition

改接口是事务定义(描述)的对象,该对象中定义了事务规则,并提供了获取事务相关信息的方法,具体如下:

  • getName():获取事务对象的名称
  • getLsolationLevel():获取事务的隔离级别
  • getPropagationBehavior():获取事务的传播行为
  • setTimeout():获取事务的超时时间
  • isReadOnly():获取事务是否只读

2.1.3TransactionStatus

TransactionStatus接口是事务的状态,描述了某一时间点上事务的状态信息。

2.2事务的管理方式

Spring中的事务管理分为两种方式:一种是传统的编程事务管理;另外一种是声明式事务管理。

  • 编程序事务管理:通过编写代码实现事务管理,包括定义事务的开始,正常执行后事务的提交和异常时事务的回滚
  • 声明式事务管理:通过AOP技术实现的事务管理,其主要思想是将事管理作为一个“切面”代码单独编写,然后通过AOP技术将事务管理的“切面”植入业务目标类中

声明式事务管理最大的优点在于开发者无需通过编程的方式来管理事务,只需要在配置文件中进行相关的事务规则声明,就可以将事务规则应用到业务逻辑中。这使得开发人员可以更加专注于核心业务逻辑代码的编写,在一定程度上减少了工作量,提高了开发效率。所以在实际开发中,通常都推荐使用声明式事务管理

2.3、基于XML实现事务控制

基于XML方式的声明式事务管理是通过在配置文件中配置事务规则的相关声明来实现的。

Spring2.0以后,提供了tx命名空间来配置事务,tx命名空间下提供了tx:advice元素来配置事务的通知(增强处理)。当使用tx:advice元素配置了事务的增强处理后,就可以通过编写AOP配置让Spring自动对目标代理

配置tx:advice元素时,通常需要指定id和transaction-manager属性,其中id属性是配置文件中唯一标志,transaction-manager属性用于指定事务管理器。除此之外,还需要配置一个tx:attributes子元素,该子元素可通过配置多个tx:method子元素类配置执行事务的细节。

<!--
        1、配置事务管理器:<bean>

    2、配置事务的通知  <tx:advice>
                此时需要导入事务的约束   即 AOP和事务的约束
                使用tx:advice标签配置事务通知
                        属性:
                                id给事务通知起一个唯一标志
                                transcation-manager:给事务通知提供一个事务管理器引用

                 在tx:advice标签内部 配置事务的属性 <tx:attribute>

    3、配置Aop中的通用切入点表达式  <aop-config>

    4、建立事务通知和切入点表达式的对应关系 

    5、配置事务的属性

            在事务的通知tx:advice标签内部
                isolation="" :用于指定事务隔离级别,默认值default,表示使用数据库的默认隔离级别
                        propagation="" :用于指定事务的传播行为,默认值为REQUIRED,表示一定会有事务,增删改的选择,查询方法可以选择SUPPORT

                read-only="" :用于指定事务是否为只读,只有查询方法才能设置为true。默认值为false,表述

                rollback-for="":用于指定一个异常,当产生一个一个异常时,事务回滚,产生其他异常时事务不回滚;没有默认值,表示任何异常都回滚

                 no-rollback-for="":用于指定一个异常,当产生该异常时,事务不会滚,产生其他异常时事务回滚。没有默认值,表示任何异常都回滚。

                timeout="":用于指定事务的超时时间,表示永不超时,如果指定了数值,以秒为单位

-->

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-fb9t1F4w-1579010543908) (10.png)]

2.4、基于注解实现事务控制

<context:component-scan base-package="com.itheima"></context:component-scan>

<!--配置JdbCTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="ds"></property>
</bean>

<!--配置数据源-->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/eesy?useUnicode=true&amp;characterEncoding=UTF-8&amp;userSSL=false&amp;serverTimezone=GMT%2B8"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
</bean>

<!--Spring中基于注解声明式事务控制步骤-->
<!--
    1、配置事务管理器
    2、开启spring对注解事务的支持
    3、在需要使用事务支持的地方使用@Transcationnal注解

-->
<!--配置事务管理器-->
<bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="ds"></property>

</bean>

<!--开启Spring对注解事务的支持吃-->
<tx:annotation-driven transaction-manager="transcationManager"></tx:annotation-driven>

Published 101 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/ZHOUJIAN_TANK/article/details/103980747