org.apache.ibatis.binding.BindingException: Invalidbound statement (not found)的解决方案和造成原因分析(超详细)

2年前 (2022) 程序员胖胖胖虎阿
257 0 0

一、问题描述 

        今天使用SpringBoot整合mybaits时报了绑定异常的错误:

AbstractHandlerExceptionResolver.java:194
|org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
|Resolved exception caused by handler execution: org.apache.ibatis.binding.BindingException:
Invalid bound statement (not found): com.guli.edu.mapper.CourseMapper.getCoursePublishVoById

 

二、问题分析 

         出现这种错误,首先检查一下自己的xml文件中的id、resultMap等属性有没有写错,我检查了一下,应该都是正确的。

        再看看target文件夹,xml文件夹为空

org.apache.ibatis.binding.BindingException: Invalidbound statement (not found)的解决方案和造成原因分析(超详细)

        那么这已经可以基本确定问题了。

三、错误原因 

        这个错误是maven默认加载机制造成的问题。maven加载时候,把java文件夹里面.java类型文件进行编译,如果是其他类型文件,则不会被加载。dao层编译后只有class文件,没有mapper.xml,因为maven工程在默认情况下src/main/java目录下的所 有资源文件是不发布到target目录下的。

四、解决方案

        1、把mybatis的xml文件直接复制到target目录中

        这个方法可以解决问题,但是每次手动复制也挺笨的,不推荐使用

        2、把xml文件放在resources目录下 

         resources目录下的文件是会编译的。但是这样的操作会破坏项目结构,特别是自动生成出来的代码,不建议使用这种方法。

        3、通过配置解决(推荐使用)

        项目中的两个地方都要配置,不要只配置一个地方

                (1)pom.xml中 

<!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 

                (2)application.properties中 

#配置mapper xml的路径
mybatis-plus.mapper-locations=classpath:com/shang/eduservice/mapper/xml/*.xml

 

        

        上述三种方法均能解决问题。 

相关文章

暂无评论

暂无评论...