mybatis plus報Invalid bound statement (not found) 解決方法
出現(xiàn)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)
上述日志詳情。
問題分析:
- 檢查是不是引入 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();
//可以通過環(huán)境變量獲取你的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();
}