美文网首页
mybaties-plus报错

mybaties-plus报错

作者: o_O小薯条 | 来源:发表于2024-06-27 10:42 被阅读0次

mybatis plus报Invalid bound statement (not found) 解决方法

出现Invalid bound statement (not found) 异常

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxxx.xxxx.service.UserService.list

    at com.baomidou.mybatisplus.core.override.PageMapperMethod$SqlCommand.<init>(PageMapperMethod.java:261)
    at com.baomidou.mybatisplus.core.override.PageMapperMethod.<init>(PageMapperMethod.java:58)
    at com.baomidou.mybatisplus.core.override.PageMapperProxy.cachedMapperMethod(PageMapperProxy.java:70)
    at com.baomidou.mybatisplus.core.override.PageMapperProxy.invoke(PageMapperProxy.java:63)
    at com.sun.proxy.$Proxy72.list(Unknown Source)
    at com.baomidou.mybatisplus.extension.service.IService.list(IService.java:279)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
    at com.baomidou.mybatisplus.core.override.PageMapperProxy.invokeDefaultMethod(PageMapperProxy.java:89)
    at com.baomidou.mybatisplus.core.override.PageMapperProxy.invoke(PageMapperProxy.java:58)
    at com.sun.proxy.$Proxy72.list(Unknown Source)
    at com.xx.xxxx.xxx.xxxx.serviceTest(OssTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

上述日志详情。

问题分析:

  1. 检查是不是引入 jar 冲突

2. 检查 Mapper.java 的扫描路径

3. 检查命名空间是否正常? 检查包扫描路径typeAliasesPackage是否正常?如果扫描不到,mapper无法进行预注入

4. 检查是否指定了主键?如未指定,则会导致 selectById 相关 ID 无法操作,请用注解 @TableId 注解表 ID 主键。当然 @TableId 注解可以没有!但是你的主键必须叫 id(忽略大小写)

5. SqlSessionFactory 不要使用原生的,请使用MybatisSqlSessionFactory (划重点)

解决方案:

2. 方法一:在 Configuration 类上使用注解 MapperScan

@Configuration
@MapperScan("com.yourpackage.*.mapper")
public class YourConfigClass{
  ...
}

方法二:在Configuration类里面,配置 MapperScannerConfigurer

@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
    MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
    //可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置文件配置了
    scannerConfigurer.setBasePackage("com.yourpackage.*.mapper");
    return scannerConfigurer;
}

5. 划重点!

@Primary
@Bean(name = "sysSqlSessionFactory")
public SqlSessionFactory sysSqlSessionFactory(@Qualifier("sysDataSource") DataSource dataSource)
        throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    return bean.getObject();
}

相关文章

网友评论

      本文标题:mybaties-plus报错

      本文链接:https://www.haomeiwen.com/subject/oocecjtx.html