在整合mybatis时遇到了这样的坑,报错如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.atguigu.eduservice.mapper.EduTeacherMapper.teacherListQuery
at com.baomidou.mybatisplus.core.override.PageMapperMethod$SqlCommand.<init>(PageMapperMethod.java:261) ~[mybatis-plus-core-3.0.5.jar:na]
at com.baomidou.mybatisplus.core.override.PageMapperMethod.<init>(PageMapperMethod.java:58) ~[mybatis-plus-core-3.0.5.jar:na]
at com.baomidou.mybatisplus.core.override.PageMapperProxy.cachedMapperMethod(PageMapperProxy.java:70) ~[mybatis-plus-core-3.0.5.jar:na]
at com.baomidou.mybatisplus.core.override.PageMapperProxy.invoke(PageMapperProxy.java:63) ~[mybatis-plus-core-3.0.5.jar:na]
at com.sun.proxy.$Proxy90.teacherListQuery(Unknown Source) ~[na:na]
at com.atguigu.eduservice.service.impl.EduTeacherServiceImpl.teacherListQuery(EduTeacherServiceImpl.java:31) ~[classes/:na]
at com.atguigu.eduservice.service.impl.EduTeacherServiceImpl$$FastClassBySpringCGLIB$$ba8ae57e.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
看报错日志大概能够知道是接口方法和mapper.xml之间的映射关系出现了问题,找不到对应的xml,mapper.xml文件没有加载到或者加载错误。具体是什么原因还要进一步排查。
1.首先查看mapper.xml的namespace是否是对应接口类的全名(包名+类名)
<mapper namespace="com.atguigu.eduservice.mapper.EduTeacherMapper">
2.查看配置文件中mapper的扫描路径是否正确(按自己的项目配置)
mybatis-plus:
configuration:
#这个配置会将执行的sql打印出来
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#mapper文件扫描路径
mapper-locations: classpath:com/atguigu/eduservice/mapper/*/*.xml
3.以上都修改正确后问题依然没有解决。网上还有说可能是因为目录层级问题。我这里没有这种情况。所以需要继续排查。
4.最后需要注意的是,如果你的项目是maven项目,请你在编译后,到target目录下看一看有没有对应的xml文件,因为maven默认是不编译的。我的问题在于我是直接把xml文件放到了java类的目录里。对应的maven项目,只会把resource目录的文件输出,所以当然就找不到对应的mapper.xml文件了。
因此,你需要在你的pom.xml的<build></build>里面,加这么一段:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
加上以后,重启项目,可以发现target目录下加载了mapper.xml。问题终于得到解决。