org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)完整解决方案思路和解决过程
你可以通过看下面这篇文章了解具体细节
SpringBoot整合MyBatis,入门教程,细节无敌,不能错过
本篇文章,我将通过程序启动步骤,逐一排查问题
1.先搞懂my batis程序启动流程图
【application.yml】项目启动配置文件-示例代码
我的名称:application.yml
这里指的是:application.yml
、application.yaml
、application.properties
文件
# Mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: jjycheng.springbootwebdemo.springboot_mybatis.domain
【*Mapper.xml】映射文件-示例代码
我的名称:UserInfoMapper.xml
<?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="jjycheng.springbootwebdemo.springboot_mybatis.mapper.UserInfoXmlMapper">
<select id="findAll" resultType="UserInfoModel" >
select * from userinfo
</select>
<select id="getTop1" resultType="UserInfoModel" >
select * from userinfo limit 0,1
</select>
</mapper>
项目结构截图-示例代码
实体模型文件domain-示例代码
我的名称:UserInfoModel.java
package jjycheng.springbootwebdemo.springboot_mybatis.domain;
public class UserInfoModel {
private int Id;
private String Name;
private int Age;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@Override
public String toString() {
return "UserInfoModel{" +
"Id=" + Id +
", Name='" + Name + '\'' +
", Age=" + Age +
'}';
}
}
接口文件-示例代码
我的名称:UserInfoXmlMapper.java
package jjycheng.springbootwebdemo.springboot_mybatis.mapper;
import jjycheng.springbootwebdemo.springboot_mybatis.domain.UserInfoModel;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserInfoXmlMapper {
public List<UserInfoModel> findAll();
public List<UserInfoModel> getTop1();
}
运行调用-示例代码
我的名称:SpringBootMyBatisApplicationTests.java
package jjycheng.springbootwebdemo.springboot_mybatis;
import jjycheng.springbootwebdemo.springboot_mybatis.domain.UserInfoModel;
import jjycheng.springbootwebdemo.springboot_mybatis.mapper.UserInfoMapper;
import jjycheng.springbootwebdemo.springboot_mybatis.mapper.UserInfoXmlMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
public class SpringBootMyBatisApplicationTests {
@Test
void contextLoads() {
}
/*----------注解开发----------*/
@Autowired
private UserInfoMapper userInfoMapper;
@Test
public void TestFindAll()
{
List<UserInfoModel> list =userInfoMapper.findAll();
System.out.println(list);
}
/*----------xml开发----------*/
@Autowired
private UserInfoXmlMapper userInfoXmlMapper;
@Test
public void TestXmlFindAll()
{
System.out.println(userInfoXmlMapper.findAll());
}
}
2. 检查项
2.1.检查*Mapper.xml
- 检查
*Mapper.xml
映射文件的namespace
值 - 检查
*Mapper.xml
映射文件的id
值 - 检查
*Mapper.xml
映射文件的resultType
值 - 检查
*Mapper.xml
映射文件的sql
语句 - 检查
*Mapper.xml
映射文件的头部代码 - 检查
*Mapper.xml
映射文件是否在src》main》resources》mapper
目录下 - 去掉xml文件中的中文注释
- 随意在xml文件中加一个空格或者空行然后保存
- 检查
*Mapper.xml
映射文件的其他项
示例代码:
UserInfoXmlMapper是UserInfoXmlMapper.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="jjycheng.springbootwebdemo.springboot_mybatis.mapper.UserInfoXmlMapper">
<select id="findAll" resultType="UserInfoModel" >
select * from userinfo
</select>
<select id="getTop1" resultType="UserInfoModel" >
select * from userinfo limit 0,1
</select>
</mapper>
*Mapper.xml
映射文件不在src》main》resources》mapper
目录下
-
在使用IDEA开发时,如果打包时*Mapper.xml没有自动复制到class输出目录的mapper类包下,则需要在pom文件中添加mybatis加载配置文件的配置!
-
如果我们的mapper.xml文件没有放置到src-main-resources下面,是不会被maven build plugin给默认扫描到的。此时需要修改启动的模块的pom文件,在build标签里面加入:
如下所示:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.2.检查application.yml
# Mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: jjycheng.springbootwebdemo.springboot_mybatis.domain
3.推荐相关文章
-
文章1:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 出现原因及解决办法 -
文章2:轻松解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误
-
文章3:关于idea 中使用mybastis报出 Invalid bound statement (not found)的错误解决方案
-
文章4:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
-
文章5:解决问题:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
-
文章6:org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
-
文章7:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
-
文章8:org.apache.ibatis.binding.BindingException异常报错原因以及详细解决方案
4.你看了本文和推荐文章,还没有解决你的问题,不放看下这个
我是搞了很久,看了许多文章,都没有能解决我的问题,最后,发现有2个application文件造成的
- 同一个项目里,
application.*
文件只能有一个,如果有多个 就会出现一些神奇问题 - 同一个项目里,
application.*
文件只能有一个,如果有多个 就会出现一些神奇问题 - 同一个项目里,
application.*
文件只能有一个,如果有多个 就会出现一些神奇问题 - 即
src>main>resources>application.yml
无论application.yml
、application.yaml
、application.properties
文件只能有一个