此异常是mybatis中抛出的。意思是使用的这个方法找到。但是因为mapperScan()已经扫描到了Mapper类了,在绑定Mapper.xml时没有绑定到导致的。
1.具体异常信息
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.xxx.xxx.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
at com.sun.proxy.$Proxy202.findPageTemplateBasicInfoList(Unknown Source)
.....
2.使用mybatis的步骤
- 首先要创建一个interface接口Mapper类。假如是:
package com.xiaobai.front.cms.dao.mapper;
public interface PageTemplateMapper {
List<PageTemplateBasicInfo> findPageTemplateBasicInfoList(PageTemplateQueryDto pageTemplateQueryDto);
}
- 在启动类或者配置类加扫描Mapper类的路径。使用的是@MapperScan注解。里面配置的是Mapper类的包路径。
@Configuration
@MapperScan("com.xiaobai.front.cms.dao.mapper")
public class MybatisConfig {
}
- 在资源文件夹下配置对应mapper.xml文件,并且写对应上面方法(findPageTemplateBasicInfoList)的sql。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaobai.front.cms.dao.mapper.PageTemplateMapper">
<select id="findPageTemplateBasicInfoList" resultType="com.dffl.front.cms.domain.response.PageTemplateBasicInfo">
.......
</select>
</mapper>
- 在配置文件中配置mybatis扫描mapper.xml的路径
mybatis:
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath*:mapper/**/*Mapper.xml
3.出现BindingException原因分析
3.1第一种 如果mapper.xml中没有写对应的方法在启动项目的时候不会抛出该异常。而在用到该方法时会抛出异常
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);
}
}
}
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
String statementId = mapperInterface.getName() + "." + methodName;
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;
}
- configuration 对象是里面有很多的map。其中一个就是扫描mapper类和mapper.xml文件生成的每个方法对应的sql数据。mappedStatements key对应的是mapper类里面的全路径方法名比如:com.xiaobai.front.cms.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList。value对应的就是mapper.xml的信息对象。
- 当在调用这个findPageTemplateBasicInfoList方法的时候由于mapper.xml中没有对应的绑定这个方法,mappedStatements中就获取不到数据。
- 因为mappedStatements没有数据所以resolveMappedStatement方法返回null。
- 所以最后会抛出throw new BindingException("Invalid bound statement (not found): "
- mapperInterface.getName() + “.” + methodName);
3.2 没有配置@MapperScan(“com.xiaobai.front.cms.dao.mapper”)执行某方法时也会抛出此异常
public Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<Resource> resources = new ArrayList<Resource>();
if (this.mapperLocations != null) {
for (String mapperLocation : this.mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
// ignore
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
- 主要原因就是没有配置扫描mapper.xml。
- 导致mappedStatements里面没有数据。所以在使用时找不到。就会抛出该异常。