SoFunction
Updated on 2025-03-08

Detailed explanation of exception BindingException in mybatis

BindingException exception

This exception is thrown in mybatis.

It means that this method is found, but because mapperScan() has scanned the Mapper class, it is not bound to it during binding.

Specific exception information

: Invalid bound statement (not found):
    at $SqlCommand.<init>(:227)
    at .<init>(:49)
    at (:65)
    at (:58)
    at .$(Unknown Source)
.....

Steps to Using Mybatis

First, you need to create an interface interface Mapper class.

package ;
public interface PageTemplateMapper {
     List<PageTemplateBasicInfo> findPageTemplateBasicInfoList(PageTemplateQueryDto pageTemplateQueryDto);
}

Add the path to scan the Mapper class in the startup class or configuration class. The @MapperScan annotation is used. The package path of the Mapper class is configured.

@Configuration
@MapperScan("")
public class MybatisConfig {
}

Configure the corresponding file in the resource folder and write the sql corresponding to the above method (findPageTemplateBasicInfoList).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/">

<mapper namespace="">
  <select  resultType="">
    .......
  </select>
</mapper>

Configure the path to mybatis scan in the configuration file

mybatis:
  # Configure mapper scan to find all mapping files  mapperLocations: classpath*:mapper/**/*

Analysis of the reason why BindingException occurs

The first type

If there is no corresponding method written in it, the exception will not be thrown when starting the project. When using this method, an exception will be thrown

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      final String methodName = ();
      final Class<?> declaringClass = ();
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      if (ms == null) {
        if (() != null) {
          name = null;
          type = ;
        } else {
          throw new BindingException("Invalid bound statement (not found): "
              + () + "." + methodName);
        }
      } else {
        name = ();
        type = ();
        if (type == ) {
          throw new BindingException("Unknown execution method for: " + name);
        }
      }
    }
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
        Class<?> declaringClass, Configuration configuration) {
      String statementId = () + "." + methodName;
      if ((statementId)) {
        return (statementId);
      } else if ((declaringClass)) {
        return null;
      }
      for (Class<?> superInterface : ()) {
        if ((superInterface)) {
          MappedStatement ms = resolveMappedStatement(superInterface, methodName,
              declaringClass, configuration);
          if (ms != null) {
            return ms;
          }
        }
      }
      return null;
    }
  1. The configuration object is a map with many maps inside. One of them is to scan the SQL data corresponding to each method generated by the mapper class and file. mappedStatements key corresponds to the full path method name in the mapper class, for example:. The corresponding value is the information object.
  2. When the findPageTemplateBasicInfoList method is called, the data cannot be obtained from mappedStatements.
  3. Because mappedStatements has no data, resolveMappedStatement method returns null.
  4. So in the end, throw new BindingException("Invalid bound statement (not found): "
  5. () + “.” + methodName);

The second type

This exception will also be thrown when executing a method without configuration

 public Resource[] resolveMapperLocations() {
    ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    List<Resource> resources = new ArrayList<Resource>();
    if ( != null) {
      for (String mapperLocation : ) { 
        try {
          Resource[] mappers = (mapperLocation);
          ((mappers));
        } catch (IOException e) {
          // ignore
        }
      }
    }
    return (new Resource[()]);
  }
  • The main reason is that there is no configuration scan.
  • This results in no data in mappedStatements. So it cannot be found when using it. The exception will be thrown.

This is the end of this article about the detailed explanation of BindingException in Java development. For more related content on BindingException, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!