• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用SpringBoot注解方式处理事务回滚实现

springboot 搞代码 4年前 (2022-01-05) 21次浏览 已收录 0个评论

这篇文章主要介绍了使用SpringBoot注解方式处理事务回滚实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚

1 Pojo类

 package com.zxf.domain; import java.util.Date; public class User { private Integer id; private String name; private String pwd; private String head_img; private String phone; private Date create_time; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getHead_img() { return head_img; } public void<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong> setHead_img(String head_img) { this.head_img = head_img; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Date getCreate_time() { return create_time; } public void setCreate_time(Date create_time) { this.create_time = create_time; } } 

2 Mapper接口

我们这里使用注解的方式编写SQL语句

 package com.zxf.mapper; import com.zxf.domain.User; import org.apache.ibatis.annotations.Insert; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { @Insert("insert into user (name,pwd,head_img,phone,create_time) values(#{name},#{pwd},#{head_img},#{phone},#{create_time})") public int save(User user); }

3 Service接口和实现类

 package com.zxf.service; import com.zxf.domain.User; public interface UserService { public int save(User user); } 
 package com.zxf.service.impl; import com.zxf.domain.User; import com.zxf.mapper.UserMapper; import com.zxf.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional //实现事务的时候要在业务类中加入该注解 public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int save(User user) { int f= userMapper.save(user); // int x=5/0; return f; } } 

4 Controller层

 package com.zxf.controller; import com.zxf.domain.User; import com.zxf.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("api/v1/user") public class UserController { @Autowired private UserService userService; @RequestMapping("save") public Object save(){ User user=new User(); user.setName("zhang3"); user.setPwd("abc123"); user.setCreate_time(new Date()); user.setPhone("1789238734"); user.setHead_img("aabbddd.jpg-600"); userService.save(user); return user; } } 

5 application.properits 配置文件

 spring.datasource.driver-class-name =com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/online spring.datasource.username=root spring.datasource.password=****** mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

6 pom文件

   4.0.0 org.springframework.bootspring-boot-starter-parent2.3.2.RELEASE<!-- lookup parent from repository -->com.zxfxf_spring20.0.1-SNAPSHOTxf_spring2 1.8  org.springframework.bootspring-boot-starter-web org.mybatis.spring.bootmybatis-spring-boot-starter2.1.2 mysqlmysql-connector-java5.1.13 org.springframework.bootspring-boot-starter-testtest  org.junit.vintagejunit-vintage-engine   org.springframework.bootspring-boot-maven-plugin

6 SpringBoot启动类

 package com.zxf; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @MapperScan("com.zxf.mapper")//扫描mapper接口 @EnableTransactionManagement//事务处理的时候启动类必须要加的注解 public class XfSpring2Application { public static void main(String[] args) { SpringApplication.run(XfSpring2Application.class, args); } } 

7 也是最重要,也是很多人忽视的地方,就是MySQL要支持事务处理才可以

这里一定要记住;否则你的SpringBoot的事务没有任何效果

到此这篇关于使用SpringBoot注解方式处理事务回滚实现的文章就介绍到这了,更多相关SpringBoot注解处理事务回滚内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是使用SpringBoot注解方式处理事务回滚实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用SpringBoot注解方式处理事务回滚实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址