How to Solve Mybatis error: invalid bound statement (not found)

Solve the mybatis error invalid bound statement (not found)

Reason for this error

1. XML file does not exist

2. The XML file and mapper are not mapped

Namespace specifies the path of mapper. The error ID is inconsistent with the method name in mapper

3. The XML file is in the Java directory instead of the resource directory, so there is no XML in the generated target

Scene

When using the mybatis plus framework, when customizing the mapper interface and XM file, because the MP automatic code generation plug-in is used, the mapper interface and XML file are in the Java directory. During compilation, the XML file under the Java path will not be automatically compiled, and the compilation will only identify the. Java file, Only XML files under resource can be compiled when packaged.

The following figure shows the XML and mapper directories of the MP auto generated code plug-in (no longer in the resource)

The compiled target directory is as follows:

Solution:

1. Add in POM file

    <build>
        <!-- The *.xml file in the java directory will also be packaged when the project is packaged -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2. Manually move the XML file in the Java directory to the resource directory and add it to the configuration file in spring boot

mybatis-plus:
	mapper-locations: classpath:**/*.xml //After the classpath add the directory of your xml file

The directory in mapper locations must be consistent with the directory where you place XML files, otherwise this error will occur even if there are XML files in target!!!

Read More: