久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

使用SpringBoot注解方式處理事務(wù)回滾實現(xiàn)

瀏覽:10日期:2023-05-02 16:34:16

我們在SpringBoot和MyBatis整合的時候,需要在SpringBoot中通過注解方式配置事務(wù)回滾

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;@Repositorypublic 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接口和實現(xiàn)類

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 //實現(xiàn)事務(wù)的時候要在業(yè)務(wù)類中加入該注解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.Driverspring.datasource.url=jdbc:mysql://localhost:3306/onlinespring.datasource.username=rootspring.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//事務(wù)處理的時候啟動類必須要加的注解public class XfSpring2Application { public static void main(String[] args) { SpringApplication.run(XfSpring2Application.class, args); }}

7 也是最重要,也是很多人忽視的地方,就是MySQL要支持事務(wù)處理才可以

使用SpringBoot注解方式處理事務(wù)回滾實現(xiàn)

這里一定要記??;否則你的SpringBoot的事務(wù)沒有任何效果

到此這篇關(guān)于使用SpringBoot注解方式處理事務(wù)回滾實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot注解處理事務(wù)回滾內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美精品久久久 | 欧美色综合天天久久综合精品 | 国产二区视频 | 久久99国产精品久久99大师 | 精品一区二区三区视频 | 成人中文视频 | 亚洲人人 | 国产精品69久久久久水密桃 | 亚洲人人艹| 精品一区二区三区免费 | 精品成人在线视频 | 精品无人乱码区1区2区3区 | 成人欧美 | 国产日韩精品在线观看 | 国产精品不卡 | 2019中文字幕视频 | 色综合免费| 日韩91| 日韩视频一区二区三区 | 999久久久国产999久久久 | 日韩在线不卡 | 久久久久99精品国产片 | 亚州中文字幕 | 精品国产一区探花在线观看 | 九九热热九九 | 黄色毛片观看 | 欧美视频一级 | 亚洲视频区 | 日本一区二区高清不卡 | 噜噜噜噜噜在线视频 | 伊人av在线免费观看 | 久久精品 | 欧洲精品一区 | 色婷婷激情| 久久女同互慰一区二区三区 | 久久国产精品久久 | 国产精品久久久久久久久久东京 | 国产裸体永久免费视频网站 | 香蕉视频成人在线观看 | 精品国偷自产国产一区 | 一区二区视屏 |