How to quickly solve Mybatis exception: Invalid bound statement (not found)

Mao Xishi 2022-11-24 20:27:28 阅读数:285










前几日,A colleague who has been working for a while asked me to help him solve one Mybatis 的问题.He wrote a CRUD,But an error is reported when starting the program:Invalid bound statement (not found) .He tries to resolve the exception,Spent an hour and still no solution,So ask me for help.所谓当局者迷,I helped him sort out the development logic,After a few minutes he found the problem,and resolved the exception.Developers who have just worked for two or three years are especially prone to this exception,但是只要理清思路,It is easy to solve the problem.I will record the thinking of analyzing the problem,希望能帮到你.



Mybatis 开发有两种方式,One is primitive Dao 开发方式,另一种是 Mapper 动态代理开发方式.在开发项目时,我们会用 Mapper 动态代理开发方式,Dynamic proxy will be automatically generated for us Mapper 的代理类,Encapsulate the details.但是,在分析问题的时候,We use raw Dao 开发方式,可以让我们了解到 statement Details of the binding map file.

下面使用原始 Dao Demonstrate how to develop statement is how it is bound in the mapping file Sql 语句的.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-// Mapper 3.0//EN"
<mapper namespace="com.test.mapper.UserMapper">
<!-- 查询所有的用户名 -->
<select id="findUserNameList" resultType="java.lang.String">
select user_name userName from user


public class Test {
private SqlSessionFactory sqlSessionFactory;
public void createSqlSessionFactory() throws IOException {
// 配置文件
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder()
// 查询所有的用户名
public void testFindUserNameById() {
// 数据库会话实例
SqlSession sqlSession = null;
try {
// 创建数据库会话实例sqlSession
sqlSession = sqlSessionFactory.openSession();
// 查询所有的用户名
List<String> list = sqlSession.selectList("com.test.mapper.UserMapper.findUserNameById");
} catch (Exception e) {
} finally {
if (sqlSession != null) {

在上面的测试中,使用了 sqlSession 的 selectList 方法,The middle parameter is statement .This parameter is used to determine in the mapping file Sql 语句,In other words use this statement in the binding map file Sql 语句.

This parameter is used in the mapping file namespace 和 select 标签的 id 组合而成的,The item is unique.of the mapping file in the example namespace 是:com.test.mapper.UserMapper,select 标签的 id 是:indUserNameById ,So put together statement 是 com.test.mapper.UserMapper.findUserNameById

如果 statement 写错了,It will result in not being able to find in the mapping file Sql 语句,就会报 Invalid bound statement (not found) .So the cause of the exception is statement 和 Sql 语句不匹配.

It will be used in actual projects Mapper 动态代理的开发方式,自动生成 mapper 接口的实现类,This exception is more likely to occur.



Now that we know the cause of the exception,就容易解决了,如果通过 statement Could not find in mapping file Sql 就会报错误:Invalid bound statement (not found) .It is generally used in projects Mapper 动态代理,So what we have to solve is also the problem of dynamic proxy.先说一下 Mapper 动态代理的实现原理,After reading it, you will know how to solve the exception.

Mapper Dynamic proxies only require development definitions Mapper 接口,Mybatis A dynamic proxy object will be created based on the interface.Developed in this way,Certain development specifications need to be followed,规范如下 4 点:

  1. Mapper.xml 文件中的 namespace 与 mapper 接口的类路径相同.
  2. Mapper 接口方法名和 Mapper.xml 中定义的每个 statement 的 id 相同
  3. Mapper 接口方法的输入参数类型和 mapper.xml 中定义的每个 Sql 的 parameterType 的类型相同
  4. Mapper 接口方法的输出参数类型和 mapper.xml 中定义的每个 Sql 的 resultType 的类型相同 

所以,我们在使用 Mapper A dynamic proxy encountered this exception,It is to check whether the development specification is followed,Because of carelessness or a wrong word,It is easy to cause program errors.

I have listed several steps to check,You follow the steps to check,Definitely find where the problem is.步骤如下 7 点:

  1. 检查 target 目录中有没有 Mybatis 的映射文件
  2. 检查 mybatis.mapper-locations 配置,The configuration is put mapper 的位置告诉 Mybatis .默认是mapper-locations: classpath*:/mapper/**/*.xml
  3. Check if the mapping file is placed resources/mapper 目录中
  4. Check the namespace 与 mapper Is the classpath of the interface the same
  5. Check the statement 是不是和 Mapper The interface method names are the same
  6. Check the statement 的 parameterType 和 resultType 是不是和 Mapper The input and output parameters of the interface method are the same
  7. 如果你使用 Idea 开发,检查一下 resources 目录,The subdirectories created under this directory cannot be created at one time,After creating a directory, create another sub-directory

另外,If you have encountered this exception,And the errors encountered are not among the above.May wish to put forward in the message area,大家一起讨论一下,A warning to latecomers,avoid pits.


copyright:author[Mao Xishi],Please bring the original link to reprint, thank you.