1. 程式人生 > >SpringBoot整合Mybatis 時開啟事務

SpringBoot整合Mybatis 時開啟事務

1、首先在啟動類加上

@EnableTransactionManagement(proxyTargetClass = true) 

注:proxyTargetClass預設為false,springboot的事務是以 JDK dynamic proxy 實現的。我不加之前啟動報錯

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.

2、啟動類中加上以下程式碼,注入 DataSourceTransactionManager 例項

@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}