大战熟女丰满人妻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ù)被更新

全部教程
主站蜘蛛池模板: 精品一区二区三区在线视频 | 日韩精品麻豆 | 亚洲精品123区 | 欧美精品观看 | www在线小视频免费 www中文字幕 | 久久精品国产99国产精品免费看 | 有啥免费毛片呢 | 天天天天色| 国产免费私人影院永久免费 | 欧美日韩一区二区三区久久 | 欧美日韩在线观看区一二 | 久久综合九色综合国产 | 日本免费一区视频 | 狠狠躁日日躁人人爽 | 全部免费的毛片在线看青青 | 国产成人欧美一区二区三区的 | 国产永久在线观看 | 在线色片| 久久精品亚洲一区二区三区浴池 | 免费的毛片网站 | 九九久久久久久久爱 | 全部免费特黄特色大片视频 | 国产高清美女一级a毛片久久 | 国产精品a人片在线观看 | 91亚洲精品一区二区福利 | 四虎永久免费影院在线 | 国产区精品福利在线观看精品 | 欧美整片在线观看 | 久久98| 香蕉视频在线观看网站 | 天天拍拍夜夜出水 | 亚洲99在线的 | 国产九九精品 | 天天操天天干天天拍 | 久草国产精品 | 四虎综合九九色九九综合色 | 久久国产一区二区三区 | 欧美日韩免费大片 | 成人性视频免费网站 | 最新中文字幕在线观看 | 午夜三级做爰视频在线看 |