写一段Java代码,利用springboot带的事务管理器控制事务

时间: 2023-11-04 13:29:38 浏览: 31
好的,以下是一段利用Spring Boot的事务管理器控制事务的Java代码示例: ```java @Service public class MyService { @Autowired private MyRepository myRepository; @Transactional public void saveMyEntity(MyEntity entity) { myRepository.save(entity); } @Transactional public void updateMyEntity(Long id, String name) { MyEntity entity = myRepository.findById(id).orElse(null); if (entity != null) { entity.setName(name); myRepository.save(entity); } } @Transactional public void deleteMyEntity(Long id) { myRepository.deleteById(id); } } ``` 在以上示例中,我们使用了Spring Boot的`@Transactional`注解来标记需要被事务管理器控制的方法。这些方法在运行时,如果发生了异常或者错误,事务管理器会自动回滚事务,保证数据的完整性和一致性。 当我们调用`saveMyEntity()`、`updateMyEntity()`、`deleteMyEntity()`方法时,它们都会受到事务管理器的控制。 需要注意的是,Spring Boot的事务管理器默认的隔离级别为`ISOLATION_DEFAULT`,传播行为为`REQUIRED`。如果需要更改这些默认值,可以在`@Transactional`注解上设置对应的属性值。

相关推荐

package com.sgave.mall.db.service; import com.github.pagehelper.PageInfo; import com.sgave.mall.db.domain.SmartmallComment; import org.junit.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestContextManager; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Arrays; import java.util.Collection; import java.util.List; import static org.junit.Assert.*; @SpringBootTest @RunWith(SpringRunner.class) /*@RunWith(Parameterized.class)*/ @Transactional public class SmartmallCommentServiceTest { @Autowired private SmartmallCommentService scs; @Before public void setUp() throws Exception { TestContextManager testContextManager = new TestContextManager(getClass()); testContextManager.prepareTestInstance(this); SmartmallCommentService scs = new SmartmallCommentService(); } @After public void tearDown() throws Exception { scs=null; } @Test public void query() { Byte type = (byte)0; Integer valueId = 9008001; Integer showType = 2; Integer offset = 0; Integer limit = 1; /*List<SmartmallComment> comments = scs.query(0,9008001,0,0,5);*/ /*List<SmartmallComment> comments = scs.query(1,9008002,1,0,5);*/ /*List<SmartmallComment> comments = scs.query(1,9008001,3,0,5);*/ if (showType == 0 || showType == 1) { List<SmartmallComment> comments = scs.query(type,valueId,showType,offset,limit); long act=PageInfo.of(comments).getTotal(); if (showType == 0) { long exp = 2; assertEquals(exp,act); } else if (showType == 1) { long exp = 1; assertEquals(exp,act); } }else { String exp="showType不支持"; String act = assertThrows(RuntimeException.class,() ->scs.query(type,valueId,showType,offset,limit)).getMessage() ; assertEquals(exp,act); } } }中各代码的意思

@Slf4j @SpringBootApplication @MapperScan({"org.jeecg.**.mapper", "com.sky.**.mapper"}) @EnableTransactionManagement() //@EnableFeignClients(basePackages = {"org.jeecg", "com.sky"}) @ComponentScan(basePackages = {"org.jeecg", "com.sky"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {DynamicDataSourceConfig.class, DynamicTenantInterceptor.class})) public class SkyPlatformSystemApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SkyPlatformSystemApplication.class); } /** * 监听器:监听HTTP请求事件 * 解决RequestContextHolder.getRequestAttributes()空指针问题 * * @return */ @Bean public RequestContextListener requestContextListener() { return new RequestContextListener(); } public static void main(String[] args) throws UnknownHostException { // log.info("数据库密码:" + SecurityExtUtil.jiami("sky_mall")); ConfigurableApplicationContext application = SpringApplication.run(SkyPlatformSystemApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); String path = oConvertUtils.getString(env.getProperty("server.servlet.context-path")); log.info("\n-----------------------------------------------------------\n\t" + "Application Sky-Platform-Boot is running! Access URLs:\n\t" + "Local: \t\t\thttp://localhost:" + port + path + "/\n\t" + "External: \t\thttp://" + ip + ":" + port + path + "/\n\t" + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n" + "-----------------------------------------------------------"); }这是什么意思

最新推荐

recommend-type

springboot手动事务回滚的实现代码

主要介绍了springboot手动事务回滚的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

基于SpringBoot的代码生成器的设计和实现.doc

本文在基于减少Java Web开发者代码编写量的需求上,使用Java EE技术和Spring Boot框架设计了一个B/S模式的代码生成器系统。本设计包含了以下3个主要内容:1. 用户对数据库的实体模型进行设计和管理;2. 系统根据实体...
recommend-type

Java(springboot) 读取txt文本内容代码实例

主要介绍了Java(springboot) 读取txt文本内容代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

java代码实现银行管理系统

主要为大家详细介绍了java代码实现银行管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

SpringBoot文件上传控制及Java 获取和判断文件头信息

主要介绍了SpringBoot文件上传控制的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性

![MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性](https://picx.zhimg.com/80/v2-8132d9acfebe1c248865e24dc5445720_1440w.webp?source=1def8aca) # 1. MATLAB结构体基础** MATLAB结构体是一种数据结构,用于存储和组织相关数据。它由一系列域组成,每个域都有一个名称和一个值。结构体提供了对数据的灵活访问和管理,使其成为组织和处理复杂数据集的理想选择。 MATLAB中创建结构体非常简单,使用struct函数即可。例如: ```matlab myStruct
recommend-type

详细描述一下STM32F103C8T6怎么与DHT11连接

STM32F103C8T6可以通过单总线协议与DHT11连接。连接步骤如下: 1. 将DHT11的VCC引脚连接到STM32F103C8T6的5V电源引脚; 2. 将DHT11的GND引脚连接到STM32F103C8T6的GND引脚; 3. 将DHT11的DATA引脚连接到STM32F103C8T6的GPIO引脚,可以选择任一GPIO引脚,需要在程序中配置; 4. 在程序中初始化GPIO引脚,将其设为输出模式,并输出高电平,持续至少18ms,以激活DHT11; 5. 将GPIO引脚设为输入模式,等待DHT11响应,DHT11会先输出一个80us的低电平,然后输出一个80us的高电平,
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。