第一章:准备工作

1.1 环境搭建

  • IDEA 2021+。
  • JDK 11。
  • Maven 3.8。
  • MySQL 5.7。

1.2 导入依赖

  • pom.xml
  1. <!-- Spring -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>5.3.12</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-aspects</artifactId>
  10. <version>5.3.12</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-test</artifactId>
  15. <version>5.3.12</version>
  16. </dependency>
  17. <!-- Spring 持久化层支持jar包 -->
  18. <!-- Spring 在执行持久化层操作、与持久化层技术进行整合过程中,需要使用orm、jdbc、tx三个jar包 -->
  19. <!-- 导入 orm 包就可以通过 Maven 的依赖传递性把其他两个也导入 -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-orm</artifactId>
  23. <version>5.3.12</version>
  24. </dependency>
  25. <!-- MySQL驱动 -->
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. <version>8.0.19</version>
  30. </dependency>
  31. <!-- junit单元测试 -->
  32. <dependency>
  33. <groupId>junit</groupId>
  34. <artifactId>junit</artifactId>
  35. <version>4.13.2</version>
  36. <scope>test</scope>
  37. </dependency>
  38. <!-- 数据库连接池 -->
  39. <dependency>
  40. <groupId>com.alibaba</groupId>
  41. <artifactId>druid</artifactId>
  42. <version>1.2.8</version>
  43. </dependency>

1.3 数据库属性文件

  • db.properties
  1. jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  2. jdbc.driverClass=com.mysql.cj.jdbc.Driver
  3. jdbc.username=root
  4. jdbc.password=123456

1.4 SQL 脚本

CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `test`
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(255) DEFAULT NULL,
  `emp_salary` double DEFAULT NULL,
  PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `emp` VALUES (1, '张三', 5000);
INSERT INTO `emp` VALUES (2, '李四', 6000);
INSERT INTO `emp` VALUES (3, '王五', 7000);

1.5 Spring 的配置文件

  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.github.fairy.era"></context:component-scan>

    <!-- 导入数据库连接信息 -->
    <context:property-placeholder location="db.properties"></context:property-placeholder>

    <!-- 配置数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

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

</beans>

1.6 创建测试类

package com.github.fairy.era.bean;

import com.github.fairy.era.service.EmpService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author 许大仙
 * @version 1.0
 * @since 2021-11-05 11:02
 */
@RunWith(SpringRunner.class)
@ContextConfiguration(value = {"classpath:applicationContext.xml"})
public class SpringTest {

    @Autowired
    private EmpService empService;

    @Test
    public void test() {
        empService.updateEmpSalary(1, 5000.21);
    }
}

1.7 创建组件

1.7.1 持久层组件

  • EmpDao.java
package com.github.fairy.era.dao;

/**
 * @author 许大仙
 * @version 1.0
 * @since 2021-11-09 14:23
 */
public interface EmpDao {

    void updateEmpNameById(Integer empId, String empName);

    void updateEmpSalaryById(Integer empId, double empSalary);

    String selectEmpNameById(Integer empId);
}
  • EmpDaoImpl.java
package com.github.fairy.era.dao.impl;

import com.github.fairy.era.dao.EmpDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author 许大仙
 * @version 1.0
 * @since 2021-11-09 14:23
 */
@Repository
public class EmpDaoImpl implements EmpDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void updateEmpNameById(Integer empId, String empName) {
        jdbcTemplate.update(" UPDATE emp SET emp_name = ? WHERE emp_id = ? ", empName, empId);
    }

    @Override
    public void updateEmpSalaryById(Integer empId, double empSalary) {
        jdbcTemplate.update(" UPDATE emp SET emp_salary = ? WHERE emp_id = ? ", empSalary, empId);
    }

    @Override
    public String selectEmpNameById(Integer empId) {
        return jdbcTemplate.queryForObject(" SELECT emp_name FROM emp WHERE emp_id = ? ", String.class, empId);
    }
}

1.7.2 创建业务层组件

  • EmpService.java
package com.github.fairy.era.service;

/**
 * @author 许大仙
 * @version 1.0
 * @since 2021-11-09 14:45
 */
public interface EmpService {

    void updateEmpSalary(Integer empId, Double empSalary);
}
  • EmpServiceImpl.java
package com.github.fairy.era.service.impl;

import com.github.fairy.era.dao.EmpDao;
import com.github.fairy.era.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 许大仙
 * @version 1.0
 * @since 2021-11-09 14:46
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Override
    public void updateEmpSalary(Integer empId, Double empSalary) {
        // 模拟异常
        int i = 10 / 0;
        empDao.updateEmpSalaryById(empId, empSalary);
    }
}

第二章:修改 Spring 的配置文件

  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd     http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.github.fairy.era"></context:component-scan>

    <!-- 导入数据库连接信息 -->
    <context:property-placeholder location="db.properties"></context:property-placeholder>

    <!-- 配置数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

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

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 事务管理器的bean只需要装配数据源,其他属性保持默认值即可 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <aop:config>
        <!-- 配置切入点表达式,将事务功能定位到具体方法上 -->
        <aop:pointcut id="txPoincut" expression="execution(* *..*Service.*(..))"/>
        <!-- 将事务通知和切入点表达式关联起来 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoincut"/>
    </aop:config>

    <!-- tx:advice标签:配置事务通知 -->
    <!-- id属性:给事务通知标签设置唯一标识,便于引用 -->
    <!-- transaction-manager属性:关联事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- tx:method标签:配置具体的事务方法 -->
            <!-- name属性:指定方法名,可以使用星号代表多个字符 -->
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>

            <!-- read-only属性:设置只读属性 -->
            <!-- rollback-for属性:设置回滚的异常 -->
            <!-- no-rollback-for属性:设置不回滚的异常 -->
            <!-- isolation属性:设置事务的隔离级别 -->
            <!-- timeout属性:设置事务的超时属性 -->
            <!-- propagation属性:设置事务的传播行为 -->
            <tx:method name="save*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>
            <tx:method name="update*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>
            <tx:method name="delete*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>
        </tx:attributes>
    </tx:advice>

</beans>

第三章:注意

  • 即使需要事务功能的目标方法已经被切入点表达式涵盖到了,但是如果没有给它配置事务属性,那么这个方法就还是没有事务。所以事务属性必须配置。