使用SpringBoot注解方式处理事务回滚实现
程序员文章站
2024-01-13 22:13:52
我们在springboot和mybatis整合的时候,需要在springboot中通过注解方式配置事务回滚1 pojo类package com.zxf.domain;import java.util....
我们在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 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"); 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文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.2.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.zxf</groupid> <artifactid>xf_spring2</artifactid> <version>0.0.1-snapshot</version> <name>xf_spring2</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>2.1.2</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.13</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
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注解处理事务回滚内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!