Spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
优点
Spring 是一个开源的免费的框架(容器)
Spring 是一个轻量级的、非入侵式的框架
控制反转(IOC)、面向切面编程(AOP)
支持事务处理
对框架整合的支持
组成
拓展
现代化的 Java 开发就是基于 Spring 的开发
Spring Boot:
- 一个快速开发的脚手架
- 基于 Spring Boot 可以快速开发单个微服务
- 约定大于配置
Spring Cloud:
- Spring Cloud 是基于 Spring Boot 实现的
IOC 理论推导
UserDao 接口
UserDaoImpl 实现类
UserService 业务接口
UserServiceImpl 业务实现类
在之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改源代码,如果程序代码量十分大,修改一次代码的成本十分昂贵
使用 set 接口实现
public class UserServiceImpl implements UserService{ private UserDao userDao; // 利用 set 实现动态值的注入 public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
- 之前程序主动创建对象,控制权在程序员手上
- 使用 set 注入后,程序不再具有主动性,而是变成了被动接收对象
这种思想,从本质上解决了问题,程序员不用再去管理对象的创建,系统的耦合性大大降低,可以专注在业务的实现上,这是 IOC 原型
IOC 本质
控制反转(Inversion of Control)是一种设计思想,DI(依赖注入)是实现 IOC 的一种方法,也有人认为 DI 是 IOC 的另一种说法,没有 IOC 的程序中,我们使用面向对象编程,对象的创建与对象的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,所谓控制反转,就是获得依赖对象的方式反转了
采用 XML 方式配置 Bean 的时候,Bean 的定义信息是和实现分离的,而采用注解方式可以把两者合为一体,Bean 的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的
控制反转是一种通过描述(XML 或 注解)并通过第三方去生产或获取特定对象的方式,在 Spring 中实现控制反转的是 IOC 容器,其实现方法是 依赖注入(DI)
HelloSpring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mySQLImpl" class="com.jarvis.dao.UserDaoMySQLImpl"></bean>
<bean id="OracleImpl" class="com.jarvis.dao.UserDaoOracleImpl"></bean>
<bean id="userServiceImpl" class="com.jarvis.service.UserServiceImpl">
<!--
ref:引用 Spring 中已经存在的对象
value:具体的值(基本数据类型)
-->
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
public class MyTest {
public static void main(String[] args) {
// 获取 ApplicationContext
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) applicationContext.getBean("userServiceImpl");
userServiceImpl.getUser();
}
}
到了现在,彻底不用再去程序中改动,要实现不同的操作,只需要在 XML 配置文件中进行修改,所谓 IOC:对象由 Spring 创建、管理、装配
IOC 创建对象的方式
- 使用无参构造创建对象(默认)
- 假设要使用有参构造创建对象
1. 使用下标赋值
<!--第一种:下标赋值--> <bean id="user" class="com.jarvis.pojo.User"> <constructor-arg index="0" value="doom"/> </bean>
2. 通过类型创建
<!--第二种:通过类型创建,不建议使用,假如参数需要两个 String 就没法用了--> <bean id="user" class="com.jarvis.pojo.User"> <constructor-arg type="java.lang.String" value="doom"/> </bean>
3. 通过参数名
<!--第三种:直接通过参数名设置--> <bean id="user" class="com.jarvis.pojo.User"> <constructor-arg name = "name" value="doom"/> </bean>
在配置文件加载的时候,容器中的所有对象(Bean)就已经初始化了
Spring 配置
别名(alias)
<!--如果添加了别名,也可以使用别名获取到这个对象-->
<alias name="user" alias="userNew"/>
Bean 的配置
<!--id:bean 的唯一标识符,也就是相当于变量名
class:bean 对象所对应的全限定名:包名 + 类名
name: 也是别名,而且 name 可以同时取多个别名
-->
import
一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
假设现在项目中由多个人开发,这些人负责不同的类开发,不同的类需要注册在不同的 bean 中,我们可以利用 import 将所有人的 beans.xml 合并为一个总的,使用的时候直接使用总的配置就可以了
<import resource="applicationContext.xml"/>
依赖注入(DI)
构造器注入
Set 方式注入
依赖注入:Set 注入
- 依赖:bean 对象创建依赖于容器
- 注入:bean 对象中的所有属性由容器来注入
【环境搭建】
- 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> game;
private String wife;
private Properties info;
}
- beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.jarvis.pojo.Address">
<property name="address" value="地址"/>
</bean>
<bean id="student" class="com.jarvis.pojo.Student">
<!--普通值注入 value-->
<property name="name" value="jarvis"/>
<!--bean 注入,ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--List-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>看电影</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="123"/>
<entry key="银行卡" value="456"/>
</map>
</property>
<!--Set-->
<property name="game">
<set>
<value>游戏</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--properties-->
<property name="info">
<props>
<prop key="学号">1</prop>
</props>
</property>
</bean>
</beans>
- 测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student.toString());
}
}
拓展方式注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p 命名注入,可以直接注入属性的值 property-->
<bean id="user1" class="com.jarvis.pojo.User" p:name="jarvis" p:age="18"/>
<!--c 命名注入,通过构造器注入 construct-args-->
<bean id="user2" class="com.jarvis.pojo.User" c:age="18" c:name="jarvis"/>
</beans>
bean 的作用域
- 单例Bean(Spring 默认机制)
这里的 单例Bean 并不是单例模式
<bean id="user2" class="com.jarvis.pojo.User" c:age="18" c:name="jarvis" scope="singleton"/>
- 原型模式:每次从容器中 get 的时候,都会产生一个新的对象
<bean id="user2" class="com.jarvis.pojo.User" c:age="18" c:name="jarvis" scope="prototype"/>
- 其余的 request、session、application,这些个只能在 web 开发中使用到
bean 的自动装配
- 自动装配是 Spring 满足 bean 依赖的一种方式
- Spring 会在上下文中自动寻找,并自动给 bean 装配属性
在 Spring 中有三种装配方式
- 在 xml 中显式配置
- 在 java 中显式配置
- 隐式的自动装配 bean
byName、byType 自动装配
<bean id="cat" class="com.jarvis.pojo.Cat"/>
<bean id="dog" class="com.jarvis.pojo.Dog"/>
<!--byName:会自动在容器上下文中查找,和自己对象 set 方法后面的值对应的 bean id
注意 id 首字母得小写(bean id="cat" 中 c 得是小写,否则报空指针异常)
byType:会自动在容器上下文中查找,和对象属性类型相同的 bean(弊端:两个类型一样的就不行)
-->
<bean id="person" class="com.jarvis.pojo.Person" autowire="byName">
<property name="name" value="HUTUTU"/>
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
</bean>
使用注解实现自动装配
使用注解须知:
- 导入约束:context 约束
- 配置注解的支持:context:annotation-config/
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
- 在属性上使用即可,也可以在 set 上使用
- 使用 @Autowired 可以不用编写 set 方法,前提是这个自动装配的属性在 IOC 容器中存在(@Autowired 会首先根据 byType,但如果其中相同类型数量大于1,再根据 byName)
@Nullable 字段标记了这个注解,说明这个字段可以为 null
@Autowired(required = false) 允许该属性在 IOC 容器中不存在
// @Autowired 配合 @Qualifier 指定一个唯一的 bean 对象注入
@Autowired
@Qualifier(value = "cat22")
private Cat cat;
@Resource 注解:默认按照 byName,如果没有匹配就 byType
@Resource(name = "dog22")
private Dog dog;
使用注解开发
在 Spring4 之后,要使用注解开发,必须要保证 AOP 的包导入
使用注解需要导入 context 约束,增加注解的支持
bean
@Component:组件,放在类上,说明这个类被 Spring 管理了,就是 bean
属性如何注入
// 等价于 <bean id="user" class="com.jarvis.pojo.User"/>
// @Component 组件
@Component
public class User {
public String name;
// 等价于 <property name="name" value="胡图图"/>
@Value("胡图图")
public void setName(String name) {
this.name = name;
}
}
衍生的注解
@Component 有几个衍生的注解,在 web 开发中,会按照 mvc 三层架构分层
- dao【@Repository】
- service【@Service】
- controller【@Controller】
这四个注解功能一样,都是代表将某个类注册到 Spring 容器中,装配 bean
自动装配
@Autowired 和 @Resource
作用域
// 等价于 <bean id="user" class="com.jarvis.pojo.User"/>
// @Component 组件
@Component
@Scope("singleton")
public class User {
public String name;
// 等价于 <property name="name" value="胡图图"/>
@Value("胡图图")
public void setName(String name) {
this.name = name;
}
}
小结
xml 与 注解:
- xml 更加万能,适用于任何场合,维护简单方便
- 注解 不是自己的类使用不了,维护相对复杂
xml 与 注解最佳实践:
- xml 用来管理 bean
- 注解只负责属性的注入
- 在使用的过程中,只需要注意必须让注解生效,就需要开启注解的支持
使用 Java 的方式配置 Spring
现在不使用 Spring 的 XML 配置,全权交给 Java
javaConfig 是 Spring 的一个子项目,在 Spring4 之后,它成为了一个核心功能
方式一
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("胡图图")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
@Configuration
// @Configuration 代表这是一个配置类
@ComponentScan("com.jarvis.pojo")
public class JarvisConfig {
}
@Test
public void test01(){
ApplicationContext context = new AnnotationConfigApplicationContext(JarvisConfig.class);
User user = (User) context.getBean("user");
System.out.println(user.getName());
}
方式二
public class User {
private String name;
public String getName() {
return name;
}
@Value("胡图图")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
public class JarvisConfig {
@Bean
public User getUser(){
return new User();
}
}
@Test
public void test02(){
ApplicationContext context = new AnnotationConfigApplicationContext(JarvisConfig.class);
User user = (User) context.getBean("getUser");
System.out.println(user.getName());
}
这种纯 Java 的配置方式在 Spring Boot 中随处可见
代理模式
代理模式 是 SpringAOP 的底层
代理模式:静态代理 和 动态代理,可以增加一些附属操作
AOP
AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
AOP 在 Spring 中的作用
使用 Spring 实现 AOP
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一:使用 Spring 的 API 接口【主要是 Spring API 接口实现】
public class Log implements MethodBeforeAdvice {
// method: 要执行的目标对象的方法
// args: 参数
// target: 目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了" + method.getName() + "返回结果为:" + o);
}
}
<!--注册 bean-->
<bean id="userService" class="com.jarvis.service.UserServiceImpl"/>
<bean id="log" class="com.jarvis.log.Log"/>
<bean id="afterLog" class="com.jarvis.log.AfterLog"/>
<!--方式一:使用原生 Spring API 接口-->
<!--配置 AOP:需要导入 AOP 的约束-->
<aop:config>
<!--切入点 execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* com.jarvis.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
方式二:使用自定义类实现 AOP【主要是切面定义】
public class DIYPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
<!--注册 bean-->
<bean id="userService" class="com.jarvis.service.UserServiceImpl"/>
<!--方式二:自定义类-->
<bean id="diy" class="com.jarvis.diy.DIYPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.jarvis.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式三:使用注解实现
<!--注册 bean-->
<bean id="userService" class="com.jarvis.service.UserServiceImpl"/>
<!--方式三:-->
<bean id="annotationPointCut" class="com.jarvis.diy.AnnotationPointCut"/>
<!--开启注解支持 默认proxy-target-class="false"(JDK) proxy-target-class="true"(cglib)-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
@Aspect// 标注这个类为一个切面
public class AnnotationPointCut {
@Before("execution(* com.jarvis.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行前");
}
@After("execution(* com.jarvis.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行后");
}
}
整合 Mybatis
http://mybatis.org/spring/index.html
导入相关 jar 包
- junit
- mybatis
- mysql 数据库
- spring 相关的
- aop 织入
- mybatis-spring【新】
编写配置文件
测试
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
回忆 Mybatis
- 编写实体类
- 编写核心配置文件
- 编写接口
- 编写 Mapper.xml
- 测试
Mybatis-Spring
Spring 中能注册实体类,接口不行,因此需要创建 UserMapper 的实体类👇
方式一(对应之前 MyBatisUtils 的创建流程)
- 编写数据源配置
- sqlSessionFactory
- sqlSessionTemplate
- 给接口加实现类
- 将实现类注入到 Spring 中
- 测试
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.jarvis.pojo"/>
</typeAliases>
</configuration>
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--DataSource:使用 Spring 的数据源替换 Mybatis 的配置
这里使用 Spring 提供的 JDBC
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定 Mybatis 配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/jarvis/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate 就是之前使用的 sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入 SqlSessionTemplate, 因为它没有 Set 方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
@Data
public class User {
private int id;
private String name;
private String pwd;
}
public interface UserMapper {
List<User> selectUser();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jarvis.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from user;
</select>
</mapper>
public class UserMapperImpl implements UserMapper{
// 原来所有的操作都使用 sqlSession 来执行,现在用 SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="applicationContext-mybatis.xml"/>
<bean id="userMapper" class="com.jarvis.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
public class MyTest {
@Test
public void test01() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
List<User> userList = userMapper.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
}
方式二
创建实体类时继承 SqlSessionDaoSupport,直接获取 sqlSession,不需要注册 SqlSessionTemplate
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="applicationContext-mybatis.xml"/>
<bean id="userMapper2" class="com.jarvis.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
声明式事务
回顾事务
- 把一组业务当成一个业务来做;要么都成功,要么都失败
- 事务在项目开发中十分重要,涉及到数据的一致性问题
- 确保 完整性、一致性
事务的 ACID 原则:
- 原子性
- 一致性
- 隔离性(多个业务可能操作同一个数据,防止数据损坏)
- 持久性(事务一旦提交,无论系统发生什么问题,结果都不会被影响,被持久化写到存储器中)
Spring 中的事务管理
- 声明式事务:AOP
- 编程式事务:需要在代码中进行事务的管理
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<!--结合 AOP 实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务传播特性:propagation -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.jarvis.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
如果不配置事务,会出现数据不一致的情况
像👇这种复合的操作