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

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

SpringBoot事務管理

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

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

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

案例思路

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

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

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

實現步驟

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 {

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

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

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

    int updateCount = studentMapper.updateByPrimaryKeySelective(student);

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

    //在此構造一個除數為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,通過瀏覽器訪問進行測試

瀏覽器

控制臺

數據庫表

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

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

數據庫的數據被更新

全部教程
主站蜘蛛池模板: www深夜视频在线观看高清 | 免费观看羞羞视频网站 | 婷婷综合社区 | 中文字幕在线免费观看 | 在线观看三级拍拍视频 | 欧美精品免费在线 | 一区免费在线观看 | 国产综合久久久久久 | 亚洲精品一区二区不卡 | 可以免费观看的一级毛片 | 免费国产午夜高清在线视频 | 精品一区二区三区四区 | 久久亚洲视频 | 欧美特级黄色大片 | 奇米狠狠操 | 成人性生活免费看 | www.av天天| 欧美精品在线看 | 美国一级毛片aa | 欧洲色片| 91九色蝌蚪在线 | 日本免费人做人一区在线观看 | 日本在线亚州精品视频在线 | 96精品国产高清在线看入口 | 麻豆国产精品免费视频 | 尤物福利在线 | 日韩综合nv一区二区在线观看 | 夜夜操夜夜爽 | 97超视频在线观看 | 久久精品一区二区国产 | 国产精品夜夜春夜夜爽久久 | 日韩精品a | 国产成人在线小视频 | 精品国产97在线观看 | 5g国产精品影院天天5g天天爽 | 99久久国产综合精品麻豆 | 91狠狠 | α片毛片 | 欧美亚洲h在线一区二区 | 亚洲国产精品久久久久久网站 | 五月婷婷在线视频 |