使用SpringBoot注解方式处理事务回滚实现
我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚
1Pojo类
packagecom.zxf.domain; importjava.util.Date; publicclassUser{ privateIntegerid; privateStringname; privateStringpwd; privateStringhead_img; privateStringphone; privateDatecreate_time; publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetPwd(){ returnpwd; } publicvoidsetPwd(Stringpwd){ this.pwd=pwd; } publicStringgetHead_img(){ returnhead_img; } publicvoidsetHead_img(Stringhead_img){ this.head_img=head_img; } publicStringgetPhone(){ returnphone; } publicvoidsetPhone(Stringphone){ this.phone=phone; } publicDategetCreate_time(){ returncreate_time; } publicvoidsetCreate_time(Datecreate_time){ this.create_time=create_time; } }
2Mapper接口
我们这里使用注解的方式编写SQL语句
packagecom.zxf.mapper; importcom.zxf.domain.User; importorg.apache.ibatis.annotations.Insert; importorg.springframework.stereotype.Repository; @Repository publicinterfaceUserMapper{ @Insert("insertintouser(name,pwd,head_img,phone,create_time)values(#{name},#{pwd},#{head_img},#{phone},#{create_time})") publicintsave(Useruser); }
3Service接口和实现类
packagecom.zxf.service; importcom.zxf.domain.User; publicinterfaceUserService{ publicintsave(Useruser); }
packagecom.zxf.service.impl; importcom.zxf.domain.User; importcom.zxf.mapper.UserMapper; importcom.zxf.service.UserService; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importorg.springframework.transaction.annotation.Transactional; @Service @Transactional//实现事务的时候要在业务类中加入该注解 publicclassUserServiceImplimplementsUserService{ @Autowired privateUserMapperuserMapper; @Override publicintsave(Useruser){ intf=userMapper.save(user); //intx=5/0; returnf; } }
4Controller层
packagecom.zxf.controller; importcom.zxf.domain.User; importcom.zxf.service.UserService; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importjava.util.Date; @RestController @RequestMapping("api/v1/user") publicclassUserController{ @Autowired privateUserServiceuserService; @RequestMapping("save") publicObjectsave(){ Useruser=newUser(); user.setName("zhang3"); user.setPwd("abc123"); user.setCreate_time(newDate()); user.setPhone("1789238734"); user.setHead_img("aabbddd.jpg"); userService.save(user); returnuser; } }
5application.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
6pom文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.zxf xf_spring2 0.0.1-SNAPSHOT xf_spring2 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.2 mysql mysql-connector-java 5.1.13 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
6SpringBoot启动类
packagecom.zxf; importorg.mybatis.spring.annotation.MapperScan; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @MapperScan("com.zxf.mapper")//扫描mapper接口 @EnableTransactionManagement//事务处理的时候启动类必须要加的注解 publicclassXfSpring2Application{ publicstaticvoidmain(String[]args){ SpringApplication.run(XfSpring2Application.class,args); } }
7也是最重要,也是很多人忽视的地方,就是MySQL要支持事务处理才可以
这里一定要记住;否则你的SpringBoot的事务没有任何效果
到此这篇关于使用SpringBoot注解方式处理事务回滚实现的文章就介绍到这了,更多相关SpringBoot注解处理事务回滚内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。