报错如下:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.hqtc.live.admin.common.dao.UserDao.list
常规检查方式(网上其他博客所写):
1:Mapper.xml中的namespace不对应和mapper接口不对应
2:Mapper.xml中的方法(即id)和mapper接口中的方法名字不同或对应的方法不存在
3:返回类型不匹配(即没有正确配置ResultMap或者ResultType)
4:可能xml文件有缓存或者修改后没保存
5:可能没有配置MapperScan导致dao方法没有被扫描注入
6:配置文件中mybatis.mapper-locations或mybatis.typeAliasesPackage配置不正确
根据以上方式检查代码后发现都没有问题!!!!
通过打断点的方式,发现后台源代码
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
configuration);
if (ms == null) {
if (method.getAnnotation(Flush.class) != null) {
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
} else {
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}
通过以上源代码发现进入了ms==null的逻辑,通过调试发现springboot配置文件中,mapper-locations属性没有被解析到。回想到自定义数据源的时候,自定义过SqlSessionFactory的实现和注入,又可能是那个地方的值不对
@Bean(name = "SqlSessionFactory")
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*.mapping/*.xml"));
return bean.getObject();
}
检查发现上面的重定义的resource路径不对
PathMatchingResourcePatternResolver().getResources(“classpath*.mapping/.xml"));,中间的符号出现问题,正确方式为PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml”));,一个符号的问题找了两个小时,不过通过照bug的方式也学习了源码!!!
这个地方特别需要注意的是,自定义过SqlSessionFactory的实现和注入之后,springboot的配置文件中mapper-locations属性失效,要想mapper配置文件生效需要重新定义路径(如上)
Springboot自定义数据源方式,参考