大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

SpringBoot教程
SpringBoot入門案例
SpringBoot框架Web開發(fā)
SpringBoot非web應用程序
SpringBoot使用攔截器
SpringBoot中使用Servlet
SpringBoot中使用Filter
SpringBoot項目配置字符編碼
SpringBoot打包與部署
SpringBoot使用Actuator
SpringBoot集成Thymeleaf模板
SpringBoot總結及綜合案例
SpringBoot工程下使用Mybatis反向工程

SpringBoot事務管理

SpringBoot 使用事務非常簡單,底層依然采用的是Spring本身提供的事務管理

• 在入口類中使用注解 @EnableTransactionManagement 開啟事務支持

• 在訪問數(shù)據(jù)庫的Service方法上添加注解 @Transactional 即可

案例思路

通過SpringBoot +MyBatis實現(xiàn)對數(shù)據(jù)庫學生表的更新操作,在service層的方法中構建異常,查看事務是否生效;

項目名稱:012-springboot-web-mybatis-transacation

該項目是在011的基礎上添加新增方法,在新增方法中進行案例的演示。

實現(xiàn)步驟

1.在StudentController中添加更新學生的方法

@Controller
public class SpringBootController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/springBoot/update")
    public @ResponseBody Object update() {
        Student student = new Student();
        student.setId(1);
        student.setName("Mark");
        student.setAge(100);

        int updateCount = studentService.update(student);

        return updateCount;
    }

}

2.在StudentService接口中添加更新學生方法

public interface StudentService {

    /**
     * 根據(jù)學生標識更新學生信息
     * @param student
     * @return
     */
    int update(Student student);
}

3.在StudentServiceImpl接口實現(xiàn)類中對更新學生方法進行實現(xiàn),并構建一個異常,同時在該方法上加@Transactional注解

@Override
@Transactional //添加此注解說明該方法添加的事務管理
public int update(Student student) {

    int updateCount = studentMapper.updateByPrimaryKeySelective(student);

    System.out.println("更新結果:" + updateCount);

    //在此構造一個除數(shù)為0的異常,測試事務是否起作用
    int a = 10/0;

    return updateCount;
}

4.在Application類上加@EnableTransactionManagement開啟事務支持

@EnableTransactionManagement可選,但是@Service必須添加事務才生效

@SpringBootApplication
@EnableTransactionManagement //SpringBoot開啟事務的支持
public class Application {

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

}

5.啟動Application,通過瀏覽器訪問進行測試

瀏覽器

控制臺

數(shù)據(jù)庫表

通過以上結果,說明事務起作用了。

6.注釋掉StudentServiceImpl上的@Transactional測試

數(shù)據(jù)庫的數(shù)據(jù)被更新

全部教程
主站蜘蛛池模板: 高清一区高清二区视频 | 亚洲日韩欧美综合 | 久草精品在线播放 | 美女被爆羞羞网站 | 桃色成人精品网站 | 一级片免费网站 | 日本在线观看不卡免费视频 | 国产精品国偷自产在线 | 亚洲精品高清视频 | 九一毛片| 亚洲 日本 欧美 中文幕 | 精品一区二区日本高清 | 欧美在线视频免费看 | 在线观看一级 | 久久国产亚洲高清观看5388 | 国产亚洲精品久久麻豆 | 香蕉爱视频 | 成人午夜爱爱爱爱爱 | 91亚洲国产系列精品第56页 | www.日本一区二区 | 亚洲成人中文字幕 | 日韩天堂在线 | 国产精品大片天天看片 | 日本高清二区 | 亚洲人成网站色7799在线观看 | 综合啪啪| 久久免费区一区二区三波多野 | 九九精品影院 | 久久国产视频一区 | 综合久久99久久99播放 | 免费一级欧美毛片 | 91高清免费国产自产拍2021 | 2019偷偷狠狠的日日 | 欧美最猛的24k毛片视频 | 四虎影视884a精品国产古代 | 亚洲精品欧洲久久婷婷99 | 老司机午夜性大片 | 国产香蕉在线视频一级毛片 | 国产欧美国产精品第二区 | 国产精品亚洲综合一区在线观看 | 奇米影视四色狠狠888俺去啦 |