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'
}
}
問題解決!
