配置类代码:
package com.woniu.config;
import com.alibaba.druid.pool.ha.selector.RandomDataSourceRecoverThread;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @author yellow Docter
* @date 2022 -05-07
* @desc
*/
@Configuration
@ComponentScan("com.woniu")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
//spring中开启事务保证事务的完整性
@EnableTransactionManagement
public class SpringConfig {
}
错误原因:未配置事务管理器,spring无法对事务进行管理。
改进方法:添加事务管理器。
改进代码:
package com.woniu.config;
import com.alibaba.druid.pool.ha.selector.RandomDataSourceRecoverThread;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @author yellow Docter
* @date 2022 -05-07
* @desc
*/
@Configuration
@ComponentScan("com.woniu")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
//spring中开启事务保证事务的完整性
@EnableTransactionManagement
public class SpringConfig {
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
return dataSourceTransactionManager;
}
}