之前由于给springboot项目实现了读写分离的方案,结果发现调用dao层始终提示如下问题:
invalid bound statement (not found)
因为这个问题寻找了半天解决方案,网上的文章无非集中在namespace、mapper文件路径、启动扫描包、文件名不一致等等,但是无一例外,都没能解决我的问题
解决方法(Solve Problem):
在要实现SqlSessionFactory的地方,加入:
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml"));
如:
@Bean
public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DynamicDataSource dataSource) {
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 如果不写这里,会导致 mybatis 出现 invalid bound statement (not found) 的问题
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml"));
bean.setDataSource(dataSource);
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
发表回复