mybatis中的異常BindingException詳解
BindingException異常
此異常是mybatis中拋出的。
意思是使用的這個方法找到,但是因為mapperScan()已經(jīng)掃描到了Mapper類了,在綁定Mapper.xml時沒有綁定到導致的。
具體異常信息
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)
.....
使用mybatis的步驟
首先要創(chuàng)建一個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
出現(xiàn)BindingException原因分析
第一種
如果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數(shù)據(jù)。mappedStatements key對應的是mapper類里面的全路徑方法名比如:com.xiaobai.front.cms.dao.mapper.PageTemplateMapper.findPageTemplateBasicInfoList。value對應的就是mapper.xml的信息對象。
- 當在調(diào)用這個findPageTemplateBasicInfoList方法的時候由于mapper.xml中沒有對應的綁定這個方法,mappedStatements中就獲取不到數(shù)據(jù)。
- 因為mappedStatements沒有數(shù)據(jù)所以resolveMappedStatement方法返回null。
- 所以最后會拋出throw new BindingException("Invalid bound statement (not found): "
- mapperInterface.getName() + “.” + methodName);
第二種
沒有配置@MapperScan(“com.xiaobai.front.cms.dao.mapper”)執(zhí)行某方法時也會拋出此異常
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里面沒有數(shù)據(jù)。所以在使用時找不到。就會拋出該異常。
到此這篇關于Java開發(fā)中的異常BindingException詳解的文章就介紹到這了,更多相關BindingException異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Mybatis報錯日志BindingException的解決
- Java錯誤org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sjks.mapper.Use
- Mybatis中BindingException異常的產(chǎn)生原因及解決過程
- 解決org.apache.ibatis.binding.BindingException:?Invalid?bound?statement?(not?found)問題(最新推薦)
- org.apache.ibatis.binding.BindingException異常報錯原因以及詳細解決方案
- MyBatis綁定錯誤提示BindingException:Invalid bound statement (not found)的解決方法
相關文章
Springboot獲取jar包中resources資源目錄下的文件
今天在項目中遇到一個業(yè)務場景,需要用到resources資源目錄下的文件,本文主要介紹了Springboot獲取jar包中resources資源目錄下的文件,感興趣的可以了解一下2023-12-12
ThreadLocal數(shù)據(jù)存儲結(jié)構原理解析
這篇文章主要為大家介紹了ThreadLocal數(shù)據(jù)存儲結(jié)構原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Java實現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】
這篇文章主要介紹了Java實現(xiàn)的可選擇及拖拽圖片的面板功能,涉及java基于swing組件選擇與操作圖片元素的相關實現(xiàn)技巧,需要的朋友可以參考下2018-01-01
老生常談Java?網(wǎng)絡編程?——?Socket?詳解
這篇文章主要介紹了Java?網(wǎng)絡編程?——?Socket?相關知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05

