mybatis-plus报错解决Invalid bound statement (not found)错误
异常信息
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): XXMapper.findTagList
也就是在mybatis中dao层xxxMapper接口与xxxMapper.xml文件在做映射绑定的时候出现问题,也就是xxxMapper接口无法匹配到操作sql语句的方法id~
源码解析
首先断点打在调用mapper方法的地方
tagMapper.findTagList();
继续走,进入MapperMethod.java类:
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
//methodName就是调用的方法名
final String methodName = method.getName();
//declaringClass就是 Mapper接口类
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);
}
}
}
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
//XXMapper.xxMethod
String statementId = mapperInterface.getName() + "." + methodName;
//configuration有一个大集合,缓存了所有的Mapper及所有的方法
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
return null;
}
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
if (declaringClass.isAssignableFrom(superInterface)) {
MappedStatement ms = resolveMappedStatement(superInterface, methodName,
declaringClass, configuration);
if (ms != null) {
return ms;
}
}
}
return null;
}
}
问题就在这里 ,mappedStatements没有工程的mapper,原因就是没有扫描到,即定位到扫描时配置问题!
解决方案
1. 检查xml映射文件中<mapper>标签绑定包名地址是否正确(即namespace的值)
2. 检查xxxMapper接口中的方法,对应xml映射文件中是否有
3. 检查<select>标签中的resultType是否与xxxMapper接口中的方法返回值类型一致,若一个是对象一个是集合,那也会报错~
4. 检查yml配置文件中的mybatis配置
配置项 mybatis -> mybatis-plus
mybatis-plus:
mapper-locations: classpath*:com/xxx/*Mapper.xml
typeAliasesPackage: com.xxx.entity
5. xml资源配置
maven:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
gradle:
processResources {
from('src/main/java') {
include '**/xml/*.xml'
}
}
问题解决!