spring boot事務管理


spring boot集成事務十分的簡單,只需要在啟動類上面增加@EnableTransactionManagement注解,然后在需要實現事務的方法上添加@Transactional注解就可以了。下面我們根據上一次的代碼來演示下。

首先,我們修改下啟動類

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@MapperScan(basePackages = ("com.example.demo.mapper"))
@EnableTransactionManagement//開啟springboot事務的支持
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

在service中添加一個修改方法

package com.example.demo.service;

import com.example.demo.model.Student;

public interface GetStudentService {

    public Student getStudentInfo();

    public int update();
}
package com.example.demo.service.impl;

import com.example.demo.mapper.StudentMapper;
import com.example.demo.model.Student;
import com.example.demo.service.GetStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GetStudentServiceImpl implements GetStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getStudentInfo() {
        Student student = studentMapper.selectByPrimaryKey("122528");
        return student;
    }

    @Transactional//增加事務注解
    @Override
    public int update(){
        Student student = new Student();
        student.setId("122528");
        student.setName("李四");
        int ret = studentMapper.updateByPrimaryKey(student);

        int i = 100/0; //觸發異常,測試更新事件會不會回滾
        return ret;
    }
}

在上面的類中特意引發了異常,用於我們的測試。最后在controlle中添加對修改方法的調用。

package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.service.GetStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetStudentController {

    @Autowired
    private GetStudentService getStudentService;

    @RequestMapping("/getStudent")
    public String getStudent(){
        Student student = getStudentService.getStudentInfo();
        return "學號=" + student.getId() + ";姓名=" + student.getName();
    }

    @RequestMapping("/updateStudent")
    public String updateStudent(){
        getStudentService.update();
        return "success";
    }
}

數據庫中原始的姓名是張三,現在將他改為李四,訪問地址http://127.0.0.1:8088/demo/updateStudent,最終結果報了異常,然后查看數據庫中的值,發現沒有發生變化,因此我們的事務注解生效了。


 

 

在上面的操作過程中,我發現了一個問題,在controller中如果使用private GetStudentServiceImpl getStudentServiceImpl;啟動的時候就會報錯:

Description:

The bean 'getStudentServiceImpl' could not be injected as a 'com.example.demo.service.impl.GetStudentServiceImpl' because it is a JDK dynamic proxy that implements:
    com.example.demo.service.GetStudentService


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.


Process finished with exit code 1

對此有兩種解決方法,一種是我上面的,將代碼中的引用由實現類改為接口類即可。另外一種是在開啟事務的注解上增加屬性。即@EnableTransactionManagement(proxyTargetClass = true),開啟CGLIB代理也能解決。

因為開啟事務時,會自動開啟動態代理,默認的是開啟的jdk動態代理。詳細解釋地址:https://blog.csdn.net/huang_550/article/details/76492600。這塊目前還不清楚什么原理,后面再細研究。

 

spring boot熱部署配置,增加pom.xml依賴

 <!-- spring boot熱部署插件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>true</scope>
            <optional>true</optional>
        </dependency>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM