解决mybatis-plus通用mapper调用报错:Invalid bound statement

 更新时间:2021年09月01日 10:24:30   作者:今夜月色很美  
这篇文章主要介绍了解决mybatis-plus通用mapper调用报错:Invalid bound statement的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

mybatis-plus通用mapper调用报错

使用springboot整合mybatis-plus后,调用自定义的方法正常,调用BaseMapper中自带的方法报错如下:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.rkang.enterprise.mapper.EmployeeInfoMapper.selectOne
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.4.6.jar:3.4.6]
    at com.sun.proxy.$Proxy70.selectOne(Unknown Source) ~[na:na]
    at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.getOne(ServiceImpl.java:259) ~[mybatis-plus-extension-3.1.1.jar:3.1.1]
    at com.baomidou.mybatisplus.extension.service.IService.getOne(IService.java:192) ~[mybatis-plus-extension-3.1.1.jar:3.1.1]
    at com.baomidou.mybatisplus.extension.service.IService
FastClassBySpringCGLIB
FastClassBySpringCGLIB
f8525d18.invoke(<generated>) ~[mybatis-plus-extension-3.1.1.jar:3.1.1]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at cn.rkang.enterprise.local.service.EmployeeInfoService
EnhancerBySpringCGLIB
EnhancerBySpringCGLIB
7e12f21b.getOne(<generated>) ~[classes/:na]

跟进源码发现是methodProxy.invoke(target, argsToUse)报错

if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
    Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
    retVal = methodProxy.invoke(target, argsToUse);
} else {
    retVal = (new CglibAopProxy.CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy)).proceed();
}

在invoke方法组装sqlmand的时候,MappedStatement没有组装成功,始终是null

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
    String methodName = method.getName();
    Class<?> declaringClass = method.getDeclaringClass();
    MappedStatement ms = this.resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration);
    if (ms == null) {
        if (method.getAnnotation(Flush.class) == null) {
            throw new BindingException("Invalid bound statement (not found): " + mapperInterface.getName() + "." + methodName);
        }
 
        this.name = null;
        this.type = SqlCommandType.FLUSH;
    } else {
        this.name = ms.getId();
        this.type = ms.getSqlCommandType();
        if (this.type == SqlCommandType.UNKNOWN) {
            throw new BindingException("Unknown execution method for: " + this.name);
        }
    }
}

解决方法

将mybatis的sqlSessionFactory替换成mybatis-plusd的MybatisSqlSessionFactoryBean,添加配置类MybatisPlusConfig

package cn.rkang.enterprise.common.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;

@Configuration
public class MybatisPlusConfig {

    @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;

    /**
     *   mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }
    /**
     * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
     * 配置文件和mybatis-boot的配置文件同步
     * @return
     */
    @Bean
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            mybatisPlus.setPlugins(this.interceptors);
        }
        MybatisConfiguration mc = new MybatisConfiguration();
        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        //数据库字段设计为驼峰命名,默认开启的驼峰转下划线会报错字段找不到
        mc.setMapUnderscoreToCamelCase(false);
        mybatisPlus.setConfiguration(mc);
        if (this.databaseIdProvider != null) {
            mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
        }
        return mybatisPlus;
    }
}

问题解决!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring Boot详解五种实现跨域的方式

    Spring Boot详解五种实现跨域的方式

    跨域指的是浏览器不能执⾏其他⽹站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制,这篇文章主要介绍了springboot实现跨域的5种方式,需要的朋友可以参考下
    2022-06-06
  • 关于maven项目中使用BCrypt加密方式

    关于maven项目中使用BCrypt加密方式

    BCrypt是一种基于Blowfish加密算法的密码散列函数,用于安全存储和验证用户密码,它通过引入盐和工作因子增加计算复杂度,有效防止彩虹表攻击和破解,BCrypt具备适应性工作因子、成本参数调整、迭代哈希和密钥扩展等特点,被广泛应用于Web应用程序的安全性设计中
    2024-10-10
  • ZooKeeper入门教程二在单机和集群环境下的安装搭建及使用

    ZooKeeper入门教程二在单机和集群环境下的安装搭建及使用

    本文是ZooKeeper入门系列教程,涵盖ZooKeeper的安装使及单机集群环境搭建,通过实例和大量图表,结合实战,帮助学习者理解和运用,有需要的朋友可以借鉴参考下
    2022-01-01
  • JAVA中HTTP基本认证(Basic Authentication)实现

    JAVA中HTTP基本认证(Basic Authentication)实现

    HTTP 基本认证是一种简单的认证方法,本文主要介绍了JAVA中HTTP基本认证(Basic Authentication),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • Idea配置Maven阿里云镜像加速的实现

    Idea配置Maven阿里云镜像加速的实现

    这篇文章主要介绍了Idea配置Maven阿里云镜像加速的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Linux系统下安装和卸载JDK8的方式

    Linux系统下安装和卸载JDK8的方式

    这篇文章主要介绍了Linux安装和卸载JDK8,第一种是使用yum命令一键安装,默认安装目录在/usr/lib/jvm第二种是手动安装,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • springboot连接neo4j报错的解决方案

    springboot连接neo4j报错的解决方案

    这篇文章主要介绍了springboot连接neo4j报错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Java SpringBoot实现AOP

    Java SpringBoot实现AOP

    AOP包括连接点(JoinPoint)、切入点(Pointcut)、增强(Advisor)、切面(Aspect)、AOP代理(AOP Proxy),具体的方法和类型下面文章会举例说明,感兴趣的小伙伴和小编一起阅读全文吧
    2021-09-09
  • 详解SpringCloud Ribbon 负载均衡通过服务器名无法连接的神坑

    详解SpringCloud Ribbon 负载均衡通过服务器名无法连接的神坑

    这篇文章主要介绍了详解SpringCloud Ribbon 负载均衡通过服务器名无法连接的神坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • Java对接微信支付全过程

    Java对接微信支付全过程

    本文主要介绍了Java对接微信支付全过程,涵盖注册商户、生成预支付订单、处理回调和查询订单状态等,具有一定的参考价值,感兴趣的可以了解一下
    2025-03-03

最新评论