我将为您详细讲解“Spring事务&Spring整合MyBatis的两种方式”的完整攻略。
一、Spring事务
Spring 提供了许多不同类型的事务进行管理,包括基于注解的事务和基于 XML 的声明式事务管理等。通常使用 Spring 进行事务管理有以下步骤:
1. 导入相关依赖
<!-- Spring事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.5</version>
</dependency>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
<!-- MySQL JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
2. 配置数据源和事务管理器
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
3. 开启事务注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 注解扫描包 -->
<context:component-scan base-package="com.example.springdemo"/>
4. 编写 Service 实现类和 DAO 接口
public interface AccountDao {
void update(Account account);
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
@Override
public void transfer(int fromId, int toId, double amount) {
Account from = accountDao.findById(fromId);
Account to = accountDao.findById(toId);
from.setBalance(from.getBalance() - amount);
to.setBalance(to.getBalance() + amount);
accountDao.update(from);
accountDao.update(to);
}
}
5. 基于注解的事务
@Transactional 注解可以标注在类或方法上,具体使用过程如下:
@Service
@Transactional
public class AccountServiceImpl implements AccountService {}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
@Override
public void transfer(int fromId, int toId, double amount) {}
}
6. 基于 XML 的声明式事务
可以使用 XML 配置文件以声明方式配置事务管理器和事务增强器,具体使用过程如下:
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<aop:config>
<aop:pointcut id="transferPointcut" expression="execution(* com.example.springdemo.service.AccountService.transfer(int, int, double))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transferPointcut"/>
</aop:config>
二、Spring整合MyBatis的两种方式
1. 基于 XML 的配置方式
1.1 导入相关依赖
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- MyBatis-Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- MySQL JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
1.2 配置数据源和 SqlSessionFactory
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.example.mybatisdemo.model"></property>
<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
</bean>
1.3 创建 Mapper 接口
public interface UserMapper {
User selectUserById(int id);
}
1.4 创建 Mapper.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserMapper">
<select id="selectUserById" resultType="com.example.mybatisdemo.model.User">
select * from user where id = #{id}
</select>
</mapper>
1.5 注入 Mapper 接口
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.example.mybatisdemo.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
1.6 测试查询方法
@Test
public void testSelectUserById() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean(UserMapper.class);
User user = userMapper.selectUserById(1);
System.out.println(user);
}
2. 基于注解的配置方式
2.1 导入相关依赖
和基于 XML 的配置方式相同。
2.2 配置数据源和 SqlSessionFactory
和基于 XML 的配置方式相同。
2.3 创建 Mapper 接口
public interface UserMapper {
@Select("select * from user where id = #{id}")
User selectUserById(int id);
}
2.4 注入 Mapper 接口
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mybatisdemo.mapper"></property>
</bean>
2.5 测试查询方法
@Test
public void testSelectUserById() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean(UserMapper.class);
User user = userMapper.selectUserById(1);
System.out.println(user);
}
以上就是 Spring 事务和 Spring 整合 MyBatis 的两种方式总结,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring事务&Spring整合MyBatis的两种方式 - Python技术站