Tag Archives: Invalid bound statement (not found)

How to Solve Error: Invalid bound statement (not found)

When spring integrates mybatis, such an error is reported because the package name is created incorrectly when the mapper.xml file is created in the resources directory. Because the package cannot be created under resources, only the folder directory can be created. The key point is (the folder cannot be in the form of “.”), so it can’t be lazy and easy. The directory should be created level by level

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!!!

Mybatis error under Springboot project: Invalid bound statement (not found)

Mybatis reports an error: Invalid bound statement (not found). There are many reasons, but just like the error message, the SQL statement in the xml cannot be found. There are three situations in which the error is reported:

The first type: grammatical error

Java DAO layer interface

public  void delete(@Param("id")String id);

Mapper.xml file corresponding to Java

<? 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 ="xxx.xxx.xxx.Mapper" > 
    <!-- Delete data --> 
    < delete id ="delete" parameterType ="java.lang.String" >
        DELETE FROM xxx WHERE id=#{id}
    </ delete > 
</ mapper >

Check: 1. Whether the method name (delete) in the interface is consistent with id=”delete” in the xml file

2. Whether the path in namespace=”xxx.xxx.xxx.Mapper” in the xml file is consistent with the interface file path

3. Whether parameterType and resultType are accurate; resultMap and resultType are different.

The second: compilation error

Locate the project path: under the error path in target\classes\, and look for the corresponding xml file.

(1) If there is no corresponding xml file, you need to add the following code in pom.xml:

< build > 
    < resources > 
         < resource > 
             < directory > src/main/java </ directory > 
             < excludes > 
                 < exclude > **/*.java </ exclude > 
             </ excludes > 
         </ resource > 
         < resource > 
             < directory > src/main/resources </ directory > 
             < includes > 
                 < include >**/*.* </include > 
             </ includes > 
        </ resource > 
    </ resources > 
</ build >

Delete the files in the classes folder, recompile, and the corresponding xml file will appear.

(2) If there is an xml file, open the xml file and check whether the error part is consistent with the source file.

First clear the files in the classes folder, execute the command: mvn clean to clean up the content, and then recompile.

The third type: configuration error

  When specifying the scan package in the configuration file, there is a problem with the configuration path. For example: the “basePackage” attribute package name specified in the spring configuration file must be specific to the package where the interface is located, instead of writing the parent or higher level package, otherwise problems may occur; cn.dao and cn.* may also cause errors ; When the annotation is scanned, the package may not be scanned.

How to Solve org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) Error

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) problem, that is, there is a problem when mapping and binding the dao interface and mapper configuration file in mybatis. Simply put, the interface and xml are either not found It is either

After the modification, the problem still exists. In the end, it took a lot of effort to find the source of my code problem. The file name of the dao interface is inconsistent with that of the xml.

Both the interface name and the interface file name are DepartmentDao, and the configuration file name is DeparmentDao.xml. It took a lot of effort to find a t letter in the names of both. After the modification, everything is normal.

This is a point that is easy to overlook. Remember: the interface name and the Mybatis mapping file name must be exactly the same.