报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
百度出来的解决方案:
1:Mapper.xml中的namespace不对应和mapper接口不对应
2:Mapper.xml中的方法(即id)和mapper接口中的方法名字不同或对应的方法不存在
3:返回类型不匹配(即没有正确配置ResultMap或者ResultType)
4:可能xml文件有缓存或者修改后没保存
5:可能没有配置MapperScan导致dao方法没有被扫描注入
6:配置文件中mybatis.mapper-locations或mybatis.typeAliasesPackage配置不正确
不管用
我是把Mybatis generator集成到自己的项目中

generator.xml配置
特别注意下上述配置的插件com.mybatis.plugin.MysqlPaginationPlugin
这是我自己本地重写的分页插件jar包。
以下是配置的pom
user-service org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 src/main/resources/generator.xml true true com.mybatisplugin generator-mysql-plugin-page 1.0 mysql mysql-connector-java 8.0.13
我使用的nacos配置的yaml文件
spring:
#数据库配置datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/quick_project?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword: 123456filters: config#设置日志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示
logging:level:root: INFOmybatis:mapper-locations: classpath:mybatis/*.xmltype-aliases-package: com.quick.user.model
以上的都配置好了,而且给种网上提出的问题都已经检查过,但是还是报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
打断点在问题逻辑处
找到MapperMethod.SqlCommand方法
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);}}}
通过调试分析,得出问题出在ms==null这一块上面。接下来继续分析MapperMethod.resolveMappedStatement这个方法
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.hasStatement(statementId)会判断有没有这个sql对应的方法。分析发现,org.apache.ibatis.session.Configuration方法中的属性 mappedStatements 为空。
那么为什么mappedStatements为空呢,我怀疑mybatis.mapper-locations属性没有被正确加载解析,导致无法正确的找到每个接口方法对应的sql。
而且我这边也没有相关的重写sqlSessionFactory这个方法。
此时应该想到自己写了分页插件包可能导致引包冲突的问题。
针对以上分析原因大概率得出包冲突造成的。因此在pom的build去掉自己本地插件包generator-mysql-plugin-page
最终的pom
user-service org.springframework.boot spring-boot-maven-plugin
搞定Mapper.xml无效问题