SpringBoot+MyBatis报错Invalid bound statement (not found)
首先我使用的开发工具是idea
Springboot的目录结构是
这里我就不一一介绍了,我的mapper层代码如下
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
List<Map> execSQL(Page page);
List<Map> getStudentList();
}
StudentMapper需要有两个注解为@Mapper和@Repository并且继承BaseMapper
下面定义的是自己想要的接口 首先我自己写了一个getStudentList()接口对应的xml文件如下:
StudentMapper.xml
namespace这里必须指向StudentMapper id对应方法名称getStudentList
检查: 1、 接口中方法名(getStudentList)与xml文件中 id="getStudentList"是否一致
2、 xml文件中的 namespace=“xxx.xxx.xxx.Mapper” 中的路径是否与接口文件路径一致
3、.parameterType类型 与 resultType类型是否准确;resultMap与resultType是不一样的。
<?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">
<!-- namespace必须指向mapper接口 -->
<mapper namespace="com.ccut.ebusiness.module.school.mapper.StudentMapper">
<select id="execSQL" parameterType="com.ccut.ebusiness.module.tool.Page" resultType="map">
${exec_sql}
</select>
<select id="getStudentList" resultType="map">
select * from student where deltag = '0' order by c_time desc
</select>
</mapper>
在pom.xml 文件需要配置扫描xml包
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
mybaits所需的依赖包
<!--baomidou-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.0.1</version>
</dependency>
<!--Spring Boot Mybatis 依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
最后再检查
applicatio.yml文件里面配置的mybaits扫描mapper包的路径是否正确
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.ccut.ebusiness.module.school.entity
我报错的原因是由于少了一个依赖包导致的,其他部分都是上网对了很多遍一点问题都没有,而且自己在xml文件里写SQL语句能正常访问读取数据,但是用mapper里面的自带方法就会报错 Invalid bound statement (not found) 最终添加这个依赖包解决了
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>