mybatis替换成mybatisplus后报错mybatisplus Invalid bound statement (not found):
项目原来是mybatis,之后由于生成代码不方便,觉得替换成mybatisplus,引入mybatisplus后,启动项目报错mybatisplus Invalid bound statement (not found):
解决方法:
1.根据错误信息发现是MapperMethord中MappedStatement返回结果为null,原来是新加入的dao中的方法没有被扫描到,导致调用该方法是,报错mybatisplus Invalid bound statement (not found):
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName, Class<?> declaringClass, Configuration configuration) {
String statementId = mapperInterface.getName() + "." + methodName;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
return null;
} else {
Class[] var6 = mapperInterface.getInterfaces();
int var7 = var6.length; for(int var8 = 0; var8 < var7; ++var8) {
Class<?> superInterface = var6[var8];
if (declaringClass.isAssignableFrom(superInterface)) {
MappedStatement ms = this.resolveMappedStatement(superInterface, methodName, declaringClass, configuration);
if (ms != null) {
return ms;
}
}
} return null;
}
}
}
2.网络上搜索相关问题,排除掉MapperScan问题后认为是MybatisSqlSessionFactoryBean没有创建,因为依赖包中引用了底层依赖包,其中一个包还有mabatis。网上尝试集中创建MybatisSqlSessionFactoryBean的方法,最终下面的方式成功。
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource;
import java.io.IOException;
/**
* Mybatis Plus 分页拦截器配置
*
* @author Josh
*/
//@EnableTransactionManagement
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
@MapperScan(basePackages={"com.xxx.xxxx.**.dao.*"})
public class MybatisPlusConfig {
@Autowired
private Environment env; @Autowired
private DataSource dataSource; @Autowired
private MybatisProperties properties; @Autowired
private ResourceLoader resourceLoader = new DefaultResourceLoader(); @Autowired(required = false)
private Interceptor[] interceptors; @Autowired(required = false)
private DatabaseIdProvider databaseIdProvider; @Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
} @Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setVfs(SpringBootVFS.class);
String configLocation = this.properties.getConfigLocation(); if (org.apache.commons.lang.StringUtils.isNotBlank(configLocation)) {
mybatisPlus.setConfigLocation(this.resourceLoader.getResource(configLocation));
}
mybatisPlus.setPlugins(this.interceptors);
MybatisConfiguration mc = new MybatisConfiguration();
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
// 数据库和java都是驼峰,就不需要,
//mc.setMapUnderscoreToCamelCase(false);
mybatisPlus.setConfiguration(mc);
if (this.databaseIdProvider != null) {
mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
}
mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
// 设置mapper.xml文件的路径
String mapperLocations = env.getProperty("mybatis-plus.mapper-locations");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources(mapperLocations);
mybatisPlus.setMapperLocations(resource);
return mybatisPlus;
}
}
3.mybatisplus版本为3.3.2,其中依赖的mybatis版本为3.5.4,而依赖的底层jar中mybatis版本为3.4.2,所以在pom文件中将底层jar中的myabatis排除
<!--Mybatis Plus 提供Mybatis封装,支持分页-->
<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>${mybatis.plus.version}</version>
<exclusions> </exclusions>
</dependency>
<dependency>
<groupId>com.xxx.xxxx</groupId>
<artifactId>xxx-xxxx-model</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion> </exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
4.配置文件中加入xml文件的地址
mybatis-plus.mapper-locations=classpath*:com/xxx/xxxx/**/mapping/*.xml
#实体扫描,多个package用逗号或者分号分隔
mybatis-plus.typeAliasesPackage=com.cfas.cloud.**.entity
mybatis-plus.configuration.mapUnderscoreToCamelCase=true
mybatis-plus.configuration.call-setters-on-nulls=true
mybatis-plus.configuration.return-instance-for-empty-row=false
5.如果xml文件在java目录里,而不是在resources下,需要在pom文件中加入下面配置
<build>
<!-- 产生的构件的文件名,默认值是${artifactId}-${version}。 -->
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src\main\java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<targetPath>${build.outputDirectory}</targetPath>
</resource>
</resources>
</build>
至此问题解决,总计花费10h左右解决。
mybatis替换成mybatisplus后报错mybatisplus Invalid bound statement (not found):的更多相关文章
- mybatis-plus配置多数据源invalid bound statement (not found)
mybatis-plus配置多数据源invalid bound statement (not found) 错误原因 引入mybatis-plus应该使用的依赖如下,而不是mybatis <de ...
- mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- Springboot项目下mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- mybatis 报错: Invalid bound statement (not found)
错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): test.dao.Produc ...
- SpringBoot报错:Invalid bound statement (not found)
错误原因: 没有发现Mybatis配置文件的路径 解决方法: 检查Mapper包名与xml文件标签的namespace数据名称是否相同 <mapper namespace="com.t ...
- idea报错:Invalid bound statement (not found)
在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...
- 项目报错:Invalid bound statement (not found):
出现这种错误有好多种情况,常见的错误有以下这些: 1.检查xml文件所在package名称是否和Mapper interface所在的包名 <mapper namespace="com ...
- IDEA报错: Invalid bound statement (not found): com.test.mapper.UserMapper.selectByPrimaryKey(转发:https://www.cnblogs.com/woshimrf/p/5138726.html)
学习mybatis的过程中,测试mapper自动代理的时候一直出错,在eclipse中可以正常运行,而同样的代码在idea中却无法成功.虽然可以继续调试,但心里总是纠结原因.百度了好久,终于找到一个合 ...
- spring boot报错:Invalid bound statement (not found): com.
经检查发现mapper的namespace没写全导致的 正确应该写成这样就可以了:
随机推荐
- P6657-[模板]LGV 引理
正题 题目链接:https://www.luogu.com.cn/problem/P6657 题目大意 给出$n\times n$的棋盘,$m$个起点第$i$个为$(1,a_i)$,对应$m$个终点第 ...
- PyCharm中文下载与安装教程【2021年更新】
第一章:下载与安装 1.1 [版本介绍]多个版本的介绍与选择 Jetbrain 公司是一家专业的 IDE 生产商,只要是市面上主流的编程语言,Jetbrain 都有相应的产品. 比如:Python ...
- Phalcon如何创建多模块并能进行访问 《Phalcon入坑指南系列 四》
本系列目录 一.Phalcon在Windows上安装 <Phalcon入坑指南系列 一> 二.Phalcon入坑必须知道的功能<Phalcon入坑指南系列 二> 三.Phalc ...
- keepalived-master-slave
Master配置 global_defs { notification_email { root@localhost } notification_email_from keeplived@local ...
- (Java)面向对象的三大特征
封装.继承与多态 封装 封装的作用(好处) 提高程序安全性,保护数据 隐藏代码的实现细节 统一接口 增加系统可维护性 属性私有(关键字private) 加上Private可使该属性私有于一个类,在其他 ...
- 多图详解万星 Restful 框架原理与实现
rest框架概览 我们先通过 go-zero 自带的命令行工具 goctl 来生成一个 api service,其 main 函数如下: func main() { flag.Parse() var ...
- Idea进行java应用的远程调试Remote debugging
本文可以解决如下两个问题: 1.如何处理和调试那些只发生在生产环境(或其他远程环境)而本地开发环境可能没办法重现的"问题". 2.只有一个可以部署的war/jar包,只有class ...
- ElasticSearch IK热词自动热更新原理与Golang实现
热更新概述 ik分词器本身可以从配置文件加载扩张词库,也可以从远程HTTP服务器加载. 从本地加载,则需要重启ES生效,影响比较大.所以,一般我们都会把词库放在远程服务器上.这里主要有2种方式: 借助 ...
- v72.01 鸿蒙内核源码分析(Shell解析) | 应用窥伺内核的窗口 | 百篇博客分析OpenHarmony源码
子曰:"苟正其身矣,于从政乎何有?不能正其身,如正人何?" <论语>:子路篇 百篇博客系列篇.本篇为: v72.xx 鸿蒙内核源码分析(Shell解析篇) | 应用窥视 ...
- Java(30)集合五Set
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228440.html 博客主页:https://www.cnblogs.com/testero ...