今天使用MybatisPlus,测试时报错Invalid bound statement (not found)

使用自定义的mapper接口中的方法可以执行,而调用MybatisPlus中baseMapper中的方法会报错

因此可以排除是路径配置问题

查询网上各种解决方案依旧无果之后,从头到尾梳理了一下代码,找到了错误

package com.jt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component; import java.io.Serializable; //基于ORM思想,属性与表一致
@Data
@Accessors(chain=true)
@TableName//如果表明与类名一致可以省略
public class User implements Serializable {
//自动生成序列化编号,settings-editor-inspections-serializable without serialVersionUID
private static final long serialVersionUID = -7155610581355123677L;
//标识主键,并且主键自增
@TableId(type= IdType.AUTO)
//如果字段属性名称一致,可以省略配置
private Integer id;
//@TableField(value = "name")
private String name;
private Integer age;
private String sex;
}
package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Component; import java.util.List; //代理方式:1.JDK代理(默认配置)2.CGLIB代理
//如果被代理者是接口,默认采用JDK代理.规定:jdk代理,必须有接口
//如果被代理者没有接口(没有实现接口)默认采用CGLIB.规定:可以创建任何对象的代理,代理者是目标对象的子类
//@Mapper//将接口交给spring管理,接口并不能创建duixiang,管理的是UserMapper的代理对象
//@MapperScan("com.jt.mapper")将此注解添加到启动类,可以自动扫描mapper包下的所有注解,因此此处不用再添加@mapper
public interface UserMapper extends BaseMapper {
List<User> findAll();
}
package com.jt.springbootmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@MapperScan("com.jt.mapper")
public class SpringbootMybatisApplication {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
//可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置文件配置了
scannerConfigurer.setBasePackage("com.yourpackage.*.mapper");
return scannerConfigurer;
} public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class,args);
}
}
package com.jt.springbootmybatis;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.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; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper; @Test
public void testFindAll() {
List<User> list = userMapper.selectList(null);
System.out.println(list);
} }

启动测试类调用selectList方法报错,而调用findAll方法运行正常

解决方案:pojo的UserMapper中,通过@TableName与表名进行了关联,所以在继承BaseMapper接口时,要指定BaseMapper<User>的泛型

完美解决

MybatisPlus报错Invalid bound statement (not found)的解决方案的更多相关文章

  1. 解决Mybatis 报错Invalid bound statement (not found)

    解决Mybatis 报错Invalid bound statement (not found) 出现此错误的原因 1.xml文件不存在 2.xml文件和mapper没有映射上 namespace指定映 ...

  2. springboot+mybatis报错Invalid bound statement (not found)

    今天做项目时报了一个错提示说Invalid bound statement (not found),也就是说mapper接口绑定.xml文件出错了,找不到指定的sql:原因是程序没有把.xml文件编译 ...

  3. IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)

    困扰几个小时,终于查到解决办法及原因(可以直接到最后看解决方案) 环境就是用IDEA搭建的maven项目,主要jar包引入配置如下 <dependencies> <dependenc ...

  4. Mybatis plus 报错Invalid bound statement (not found) 终极解决办法

    我产生的错误原因是写的mapper继承BaseMapper没有添加泛型: 点进去: 为了解决这个bug,网上很多人也提出了解决办法:1.检查xml文件的namespace是否正确 2.Mapper.j ...

  5. 使用Mybatis时报错Invalid bound statement (not found):

    使用逆向工程时生成的.xml文件在conf目录下,而使用查询方法时,无法在dao包下找到xml文件,所以报错. 测试代码如下所示: @Test public void testSimple() thr ...

  6. 关于Maven整合SSM项目中报错Invalid bound statement (not found):的问题解决

    如图:控制不报错 页面就是报500的错误 查阅了好多资料  都说是Mapper文件写的不对  我仔细找了好几遍也解决不了问题.. 解决: 坑爹的问题害我找了一上午原因,原来是需要在pom.xml文件中 ...

  7. MyBatis切换至MyBatis-plus踩坑Invalid bound statement (not found):

    部分情况可以参考https://blog.csdn.net/wwrzyy/article/details/86034458 我的问题出现的根本原因就是没有扫描到mapper的xml文件 因为MyBat ...

  8. Invalid bound statement (not found): com.xxxx.dao.other.LoginDao.getUser"

    原来是能正常运行的,后想把登录相关的调整一下目录,对应登录的文件都调整到了other下边,启动服务,请求时报错: Invalid bound statement (not found): com.xx ...

  9. oracle+mybatis报错:BindingException("Invalid bound statement (not found): ")

    oracle+mybatis报错:BindingException("Invalid bound statement (not found): ") 从mysql转到oracle数 ...

随机推荐

  1. Spring与后端模板引擎的故事

    更多内容,欢迎关注微信公众号:全菜工程师小辉.公众号回复关键词,领取免费学习资料. 现在很多开发,都采用了前后端完全分离的模式,随着近几年前端工程化工具和MVC框架的完善,使得这种模式的维护成本逐渐降 ...

  2. .net core 单元测试之 JustMock第二篇

    JustMock标记方法 上篇文章在举例子的时候使用了returns的标记方法,JustMock还有很多标记方法: CallOriginal 跟Behaviors里的CallOriginal差不多意思 ...

  3. c++ 开发JNI

    c++ 开发JNI C的预处理命令 #开头的就是c/c++的预处理命令 在编译之前 先会走预编译阶段 预编译阶段的作用就是 把 include进来的头文件 copy到源文件中 define这些宏定义 ...

  4. codeblocks中文乱码原因及解决办法

    原因:(本地化做得不够好)默认情况下codeblocks编辑器保存源文件是保存为windows本地编码,就是WINDOWS-936字符集,即GBK:但CB的编辑器在默认编辑的时候是按照UTF-8来解析 ...

  5. 微信支付之扫码、APP、小程序支付接入详解

    做电商平台的小伙伴都知道,支付服务是必不可少的一部分,今天我们开始就说说支付服务的接入及实现.目前在国内,几乎90%中小公司的支付系统都离不开微信支付和支付宝支付.那么大家要思考了,为什么微信支付和支 ...

  6. TK可视化之文件内容查找(升级篇)

    升级为带有选择框 分三种查看格式一种是表格查看 一种是文本查看 一种是列表 1.列表查看类 # listbox 显示数据 import tkinter class ListShowData: def ...

  7. Badboy - variable setter

    参考: http://leafwf.blog.51cto.com/872759/1117646 http://www.51testing.com/html/00/130600-1367743.html ...

  8. ZOJ - 3962 - Seven Segment Display-17省赛-数位DP

    传送门:Seven Segment Display 题意:求一个给定区间每个数字的消耗值的和: 思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的: 还有就是到最大 ...

  9. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  10. Atcoder C - Nuske vs Phantom Thnook(递推+思维)

    题目链接:http://agc015.contest.atcoder.jp/tasks/agc015_c 题意:给一个n*m的格,蓝色的组成路径保证不成环,q个询问,计算指定矩形区域内蓝色连通块的个数 ...