重构代码,方法抛出异常:BindingException: Invalid bound statement (not found)

提示信息很明显:mybatis没有提供某方法

先不解释问题原因和排查过程,因为使用SpringBoot集成Mybatis,主要配置点如下:

MyBatis 的真正强大在于它的映射器Mapper,它是开发者用于绑定映射语句(sql)的接口,而映射语句常规两种写法:annotation 和 xml 配置;

如果单纯使用annotation的方式,最主要是关心mapper java文件;

但是我们推荐sql配置在xml中,强大的逻辑判断、字段映射、sql复用...

1、mapper xml文件的扫描

如果使用xml配置sql,需要告诉SpringBoot扫描这些xml,常用以下两种配置方法

方法一:配置文件指定扫描路径(推荐)

 mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.winter.model # 注意:对应实体类的路径

方法二:配置 SqlSessionFactory

Mybatis万能的SqlSessionFactory接口(还有一个SqlSession接口,他俩是mybatis的核心),直接在他里面指定xml路径

@Autowired
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,
PageHelper pageHelper) throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
/** 添加mapper 扫描路径 */
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/sql/*.xml";
sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
/** 设置datasource */
sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });
return sqlSessionFactoryBean;
}

2、mapper接口的扫描

mapper接口是真正的java接口,使用动态代理,虽然只是接口定义,却实现了真正的sql执行、响应结果映射封装等,需要告诉SpringBoot扫描这些mapper接口,常用以下两种配置方法

方法一:接口上添加注解(推荐)

@Mapper
public interface PermissionMapper {
... 略 ...
}

方法二:指定扫描包路径

@MapperScan("com.XXX.XXX.services.mapper")

总之,SpringBoot中注意xml和mapper接口的扫描配置。

出现:BindingException: Invalid bound statement (not found) 这种异常,问题排查步骤:

1、先确认如上两个配置是否正常;

2、检查mapper文件,方法是否存在

3、检查xml文件,id=方法名 的sql是否存在,该xml对应的mapper接口是否存在

注意:

如果sql通过annotation注解写在mapper接口上,同时也使用了xml的方式,注意id不能重复,即使参数完全不同,id也必须不同(mybatis的xml里面可没有override的概念)

相同的id只能存在不同的namespace里面

BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析的更多相关文章

  1. 配置文件出错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): <!-- mybatis 配置- ...

  2. Spring boot结合mybatis开发的报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),经过排查确定是没有找到xml的原因 ...

  3. 【踩坑】遇到 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 报错

    今天在重做 iblog 客户端时,测试接口情况,发现了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  4. springboot项目下的Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    今天遇到mybatis-puls的报错Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...

  5. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): da.huying.usermanag ...

  6. Exception:HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    主要错误信息如下: HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  7. mybatis使用时org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):的错误

    最近在使用mybatis时,出现了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 这 ...

  8. IDEA异常解决: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    有时候解决问题不仅仅是解决问题.-----jstarseven 最近因为开发需要,需要搭建一个ssm开发框架,采用了开发工具IDEA. 整合完了SSM开发框架之后,发布的时候出现org.apache. ...

  9. MyBatis绑定错误--BindingException:Invalid bound statement (not found)

    如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因是Mapper i ...

随机推荐

  1. Iphone各个型号机型的详细参数,尺寸和dpr以及像素

    1.iPhone尺寸规格 2.单位inch(英吋) 1 inch = 2.54cm = 25.4mm 3.iPhone手机宽高 上表中的宽高(width/height)为手机的物理尺寸,包括显示屏和边 ...

  2. js权威指南学习笔记(四)对象

    1.创建对象 (1).通过对象直接量的方式创建 说明:对象直接量是由若干名/值对组成的映射表,名/值对中间用冒号分隔,名/值对之间用逗号分隔,整个映射表用花括号括起来. 如:       5 5   ...

  3. vue的事件

    vue的事件: vue事件简写: vue中事件是 v-on:click=' show()'    但是我嫌弃它写太长每次都要 v-on: 事件 vue中就有事件简写   @click='show()' ...

  4. Windows系统中Oracle11g R2 版本数据库卸载

    1. 停止"服务"中所有的ORCLE服务. 进入服务的方法很多,如: (1)在运行中输入services.msc,然后找到所有跟oracle 有关的服务. (2)开始->设置 ...

  5. HTML 5篇(持续更新)

    1.sessionStorage .localStorage 和 cookie 之间的区别 (一)共同点:都是保存在浏览器端,且同源的. (二)区别:cookie数据始终在同源的http请求中携带(即 ...

  6. GIT团队合作探讨之三--使用分支

    这篇文章是一个作为对git branch的综合介绍.首先,我们会看看创建branch,这有点像是请求一个新的项目历史.然后,我们看看git checkout是如何能够被用来选择一个branch,最后看 ...

  7. 如何使用cntlm配置代理上网

    https://blog.csdn.net/SdustLiYang/article/details/7034974 https://blog.csdn.net/bluishglc/article/de ...

  8. Lovable eccentric

    It took him four years to stage this elaborate joke simply to prove that critics do not always know ...

  9. [EffectiveC++]item33:避免遮掩继承而来的名称。

    先看看: ZT C++ 重载.覆盖和隐藏的区别 http://www.cnblogs.com/jeanschen/p/3405987.html 隐藏是指派生类的函数屏蔽了与其同名的基类函数,规则如下: ...

  10. Python函数式编程(进阶2)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6411915.html 本文出自:[Edwin博客园] Python函数式编程(进阶2) 1. python把 ...