做毕设的时候遇到的这个问题
首先我的项目在不适用 XML 的前提下的 CURD 都是成功的,但是只要一使用 XML ,那么就报上面的异常,项目结构如下
我的 Spring 版本是 3.4.2 、mybatis-plus 版本是 3.5.9
Mapper 接口中加入了 @Mapper 注解,启动类里也加入了 MapperScan 扫描
resource 中的 xml 我是在 application 中指定了扫描路径的
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
xml 文件中的接口名和命名空间都没有问题,下面是具体的 XML 代码
<?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.rolin.orange.orangecommon.mapper.UserMapper">
<select id="getAll" resultType="com.rolin.orange.orangecommon.model.user.entity.User">
select * from user
</select>
</mapper>
Mapper 代码也没有问题,可以成功编译,可以成功启动
在编译后的结果里,可以找到对应的 xml 文件
感觉不管是哪里都没有问题,但是只要调用接口,就会报下面的异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rolin.orange.orangecommon.mapper.UserMapper.getAll
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:229) ~[mybatis-3.5.16.jar:3.5.16]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:99) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:36) ~[mybatis-3.5.16.jar:3.5.16]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:97) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) ~[mybatis-plus-core-3.5.9.jar:3.5.9]
at jdk.proxy2/jdk.proxy2.$Proxy98.getAll(Unknown Source) ~[na:na]
at com.rolin.orange.orangecommon.service.UserService.getAll(UserService.java:84) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
我最后怀疑会是依赖的问题,但是查了下,springboot3.4.2 对应的 mybatisplus 版本就是 3.5.9 ,这个应该也没问题
根据上一个帖子下面的回复,是我 xml 的路径配置有问题,所以我改动了结构,将 xml 文件直接放在 mapper 下,具体来说是下面这样的
在编译后的文件里也能正确找到这两个 xml 文件
启动之后的情况是,对于 UserMapper 的接口,是可以正确调用的,但是对于 Attachment 的接口,却还是报上面的问题
AttachmentMapper 的代码如下
@Mapper
public interface AttachmentMapper extends BaseMapper<Attachment> {
List<Attachment> findNeedCleared();
}
XML 的代码如下
<?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.rolin.orange.orangecommon.mapper.AttachmentMapper">
<select id="findNeedCleared" resultType="com.rolin.orange.orangecommon.model.attachment.entity.Attachment"
databaseId="mysql">
select *
from attachment
where confirmation_flag=0
and to_days(now()) - to_days(update_date)>30
and status=1
</select>
</mapper>
我是哪里出了问题?这个问题又该如何解决呢?有没有懂得大佬来救下,小弟我是感激不尽啊
![]() |
1
Suaxi 6 天前 via Android
|
3
EMMMMMMMMM 6 天前 via Android
大于号转义一下试试
|
4
AkaGhost 5 天前 via Android
跟随了最佳实践(放在 resources 里)的话,你可以试试直接把 mapper-locations 的设置从 properties 里面移除,clean 一下再试试看,尽量不要手动指定避免引入人为错误。
|
![]() |
5
nice2cu 5 天前
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml 你是 mapper 开头的路径 但是看你代码是不是不对 |
![]() |
6
RedBeanIce 5 天前
你现在还是 mubatis ,不是 plus ,,有个 config 配置类
|
![]() |
7
Leoking222 5 天前
@nice2cu 是的,他的包名和他的配置文件不符合
|
![]() |
8
billbob 5 天前
|
![]() |
9
listen2wind 5 天前
|
![]() |
10
Van426326 5 天前
个人觉得还是对应版本的问题 很久之前升级 springboot 大版本的时候,因为这个问题也是焦头烂额。 后来还是更换 mybatisplus 的版本解决的
|
11
tiRolin OP @RedBeanIce 这个配置类里其实只配置了 swagger 的内容,mybatis 我确定我没配置
|
12
tiRolin OP @AkaGhost 试过了,无法解决这个问题
@nice2cu 已经将 mapper 的路径改为 classpath*:/mapper/*.xml ,未能解决该问题 @listen2wind 不是这里的问题,已经尝试过了,解决不了 @Van426326 可以麻烦你告知我一下当时你更换的是什么版本的 mybatisplus 吗? |
![]() |
14
ldyisbest 4 天前
日志等级设置为 debug ,看下启动日志,依赖是否都正常加载
|