-
事务作用:在数据层保证一系列的数据库操作同成功同失败
-
Spring事务作用:在数据层或者业务层保障一系列的数据库操作同成功同失败
-
接口:PlatfromTransactionManager
-
实现类:DataSourceTransactionManager (底层是jdbc,和spring一样)
-
Spring 事务角色
-
事务管理员:在业务层开启的事务,其它的事务都加入这个事务中,统一完成事务,做到了同成功同失败
-
事务协调员:就是被加入的事务,指的是数据层的方法,也可以是业务层的方法。
-
config
package com.yang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
jdbcConfig
package com.yang.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class JdbcConfig {
service
package com.yang.service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
package com.yang.service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
//可以加方法上;也可以加接口上,表示所有的方法都开始起事务
AccountServiceImpl.java
package com.yang.service.Impl;
import com.yang.dao.AccountDao;
import com.yang.service.AccountService;
import com.yang.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
test
package com.yang;
import com.yang.config.SpringConfig;
import com.yang.service.AccountService;
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.SpringJUnit4ClassRunner;
import java.io.IOException;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现