Invalid bound statement (not found): com.xxx.orm.mapper.XxxMapper.selectByPrimaryKey

在一次编译运行SpringBoot+MyBatis项目时,发现调用mapper.selectByPrimaryKey方法报错

2023-01-31 14:34:58.419 [http-nio-6010-exec-3] ERROR com.xxx.controller.PostController - Invalid bound statement (not found): com.xxx.orm.mapper.XxxMapper.selectByPrimaryKey
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.orm.mapper.XxxMapper.selectByPrimaryKey
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:108)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:36)
    at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:95)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
    at com.sun.proxy.$Proxy115.selectByPrimaryKey(Unknown Source)

经检查,对应的Mapper.xml中,确实已经定义过selectByPrimaryKey。那么,为什么会找不到这个绑定关系,那么,问题应该出在Mapper.xml的加载环节。

此时,放开Mybatis的日志打印,配置如下:

mybatis:
  mapper-locations: classpath:/mybatis/mapping/*Mapper.xml
  configuration:
    cache-enabled: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

保存后调试运行项目,输出已加载的Mapper

由于需要的Mapper不在当前项目,而位于另一个被引入的项目。同时,此处显示未加载,那么,只能是Mybatis的Mapper加载路径设置有误。

检查后发现,路径配置中,classpath后缺少通配符*,导致调试运行时,仅加载了当前项目中的Mapper,而引入项目的Mapper未加载。

果断修改后,保存并运行,Mapper.xml加载正常,调用方法不再报错

mybatis:
  mapper-locations: classpath*:/mybatis/mapping/*Mapper.xml

注:

1、**表示可以表示任意多级目录

2、*表示多个任意字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值