新建maven项目
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shrimpking</groupId>
<artifactId>demo7</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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.14</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>
数据库
drop table if exists ay_account;
create table ay_account(
id int not null auto_increment primary key comment '',
account_name varchar(100) not null comment '',
money int default null comment ''
) comment '账户表';
insert into ay_account values (null,'tom',5000);
insert into ay_account values (null,'jerry',5000);
accountMapper
package com.shrimpking.mapper;
import org.apache.ibatis.annotations.Param;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/2/2 20:46
*/
public interface AccountMapper
{
public void incrMoney(@Param("accountName") String accountName,@Param("money") Integer moeny);
public void decrMoney(@Param("accountName") String accountName,@Param("money") Integer money);
}
accountMapper.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.shrimpking.mapper.AccountMapper">
<update id="incrMoney">
update ay_account set money=money+#{money}
where account_name = #{accountName}
</update>
<update id="decrMoney">
update ay_account set money=money-#{money}
where account_name = #{accountName}
</update>
</mapper>
accountService
package com.shrimpking.service;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/2/2 21:00
*/
public interface AccountService
{
void transferMoney(String outAccount,String inAccount,Integer money);
void registerAccount(String accountName);
}
accountServiceImpl
package com.shrimpking.service;
import com.shrimpking.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/2/2 21:01
*/
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService
{
@Autowired
private AccountMapper accountMapper;
@Override
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = -1)
public void transferMoney(String outAccount, String inAccount, Integer money)
{
this.accountMapper.decrMoney(outAccount,money);
//int i = 1/0;
this.accountMapper.incrMoney(inAccount,money);
}
@Override
public void registerAccount(String accountName)
{
}
}
applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.shrimpking"/>
<!-- 加载资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 将sqlSessionFactory注入spring容器-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shrimpking.mapper"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 增强通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
<!--
isolation隔离级别
propagation事务
timeout超时
read-only只读
-->
<tx:method name="registerAccount" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.shrimpking.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
applicatinContext2.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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="com.shrimpking"/>
<!-- 加载资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 将sqlSessionFactory注入spring容器-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shrimpking.mapper"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimeZone=UTC
jdbc.username=root
jdbc.password=mysql123
txconfig
package com.shrimpking.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 org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/2/3 10:39
*/
@Configuration
@ComponentScan({"com.shrimpking"})
@PropertySource(value = "classpath:jdbc.properties")
@MapperScan({"com.shrimpking.mapper"})
//<tx:annotation-driven transaction-manager="transactionManager"/>
@EnableTransactionManagement
public class TxConfig
{
@Bean
public 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;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
apptest
package com.shrimpking;
import com.shrimpking.config.TxConfig;
import com.shrimpking.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.CollectionUtils;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/2/2 21:10
*/
public class AppTest
{
public static void main(String[] args)
{
// ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// AccountService accountService = context.getBean(AccountService.class);
// accountService.transferMoney("tom","jerry",500);
// ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
// AccountService accountService = context.getBean(AccountService.class);
// accountService.transferMoney("tom","jerry",500);
ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
AccountService accountService = context.getBean(AccountService.class);
accountService.transferMoney("tom","jerry",500);
}
}
标签:money,spring,transactional,springframework,context,import,org,com,注解
From: https://blog.csdn.net/modern358/article/details/136005860