[Java] [Spring boot] Mybatis generator 生成Mapper.xml无效的问题
创始人
2024-01-31 00:51:43
0

报错: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-serviceorg.springframework.bootspring-boot-maven-pluginorg.mybatis.generatormybatis-generator-maven-plugin1.3.2src/main/resources/generator.xmltruetruecom.mybatisplugingenerator-mysql-plugin-page1.0mysqlmysql-connector-java8.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-serviceorg.springframework.bootspring-boot-maven-plugin
























搞定Mapper.xml无效问题

相关内容

热门资讯

视频丨日本媒体人:“拥核”言论...   日本首相官邸一名负责安全保障事务的官员12月18日称“日本应该拥有核武器”,引发日本国内强烈批评...
如何避免感冒诱发心肌炎?哪些人...   央视网消息:近日,北京的一位患儿在甲流退烧后的第3天,因为骑车运动一小时产生呼吸不畅等症状,送医...
闻一多《七子之歌》发表100周...   中新社湖北浠水12月20日电 (马芙蓉 方俊)闻一多《七子之歌》发表100周年诗歌音乐会20日在...
26年前的那一夜!澳门回归,从...   点击收听《七子之歌》  音频  04:10  ▼《七子之歌》是闻一多众多诗篇中的重要篇章之一,载...
罗马“许愿池”将对游客收费   面对日益增加的客流量,意大利首都罗马标志性景点特雷维喷泉及“许愿池”从明年2月1日起将向游客收费...
西藏丁青布托湖:冬日冰湖 ...   冬日里的布托湖,湖面冰封如玉,湛蓝美如仙境。偶有斑头雁悠然于冰湖,为美丽的山水画卷添上灵动一笔。...
新一轮冷空气来袭 多地“以雪为...   央视网消息:近日,中东部地区持续回暖,不过,新一股冷空气将打断这波升温节奏。中央气象台预计,随着...
年终报道|2025“潮”起东方...   (海报部分素材由AI生成)  2025年  中国文化绽放出新魅力  动漫电影《哪吒之魔童闹海》 ...
澳门回归祖国26周年 | 琴澳...   濠江潮涌 莲花盛放  为庆祝澳门回归祖国26周年  中央广播电视总台大湾区之声  联合横琴粤澳深...
江西省文印中心原主任庞金艳接受...   央视网消息:据廉洁江西微信公众号消息,江西省文印中心原主任庞金艳涉嫌严重违纪违法,主动向组织交代...