@Bean
@Primary
//一定要使用 MybatisSqlSessionFactoryBean否则Statement not bound错误
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
// 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource作为数据源则不能实现切换
sessionFactory.setDataSource(dynamicDataSource);
sessionFactory.setTypeAliasesPackage("com.xxx"); // 扫描Model
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath*:com/**/dao/*.xml")); // 扫描映射文件
return sessionFactory;
}
如果使用SqlSessionFactoryBean则会报这个错误
换成MybatisSqlSessionFactoryBean 则没问题
搞了好久是个大坑 希望对其他人有帮助