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

本文解决在SpringBoot项目中整合Mybatis-Plus后,调用BaseMapper自带方法报错的问题,通过替换sqlSessionFactory并添加MybatisPlusConfig配置类实现。
部署运行你感兴趣的模型镜像

使用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$$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$$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;
    }

}
问题解决

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

<think>嗯,用户遇到了MyBatis-PlusMapper继承BaseMapper时报BindingException错误的问题,具体是Invalidbound statement(notfound)。我需要仔细分析可能的原因和解决方案。首先,根据引用[4],若依框架的例子中,SqlSessionFactoryBean可能不支持MyBatis-Plus的功能,必须使用MybatisSqlSessionFactoryBean。这可能是一个常见问题,用户是否配置了正确的SqlSessionFactoryBean?需要检查配置类,确保使用MyBatis-Plus的工厂类。然后,引用[5]提到mapper-locations的配置是否正确。用户可能没有正确设置XML文件的位置,或者路径不匹配。需要确认application.yml或.properties文件中mybatis-plus.mapper-locations是否正确指向了XML文件的位置,包括通配符是否正确。 另外,引用[2]和[3]中的案例显示,当项目从MyBatis迁移到MyBatis-Plus时,可能出现BaseMapper方法找不到的情况。这可能是因为MyBatis-Plus需要生成对应的SQL实现,如果Mapper接口没有正确扫描或缺少注解,也会导致此问题。需要检查是否有@Mapper注解或在启动类上添加了@MapperScan注解,确保接口被正确注册。 还有,XML文件的命名空间是否与Mapper接口的全限定名一致?如果命名空间错误,即使方法存在,也会找不到对应的语句。需要核对XML中的namespace和接口的包名、类名是否完全匹配。 另外,可能是依赖冲突或版本不兼容。例如,Spring Boot3.x与MyBatis-Plus的版本是否兼容?用户的环境是SpringBoot3.2.2和MyBatis-Plus3.5.9,可能需要确认是否支持。根据经验,MyBatis-Plus3.5.x应该兼容Spring Boot3,但最好查阅官方文档确认。还有,是否在项目中同时配置了原生MyBatisMyBatis-Plus的SqlSessionFactory?这可能导致冲突,需要确保只使用MyBatis-Plus的配置。 最后,构建工具是否清理了旧的缓存?有时候target目录没有重新编译,或者IDE没有及时更新资源文件,导致XML未被复制到输出目录。可以尝试清理项目并重新构建。总结可能的原因:SqlSessionFactory配置错误、XML路径不正确、接口未扫描、命名空间不匹配、版本不兼容、构建缓存问题。需要逐一排查这些方面,给出具体的解决步骤。</think>### 解决MyBatis-Plus中`Invalid bound statement`错误的步骤 此错误通常由**Mapper接口与XML映射文件未正确绑定**或**MyBatis-Plus核心配置缺失**导致。以下是具体排查方案: --- #### 1. 检查`SqlSessionFactory`配置 确保使用MyBatis-Plus专用的`MybatisSqlSessionFactoryBean`(而非原生的`SqlSessionFactoryBean`)[^4]。 **示例配置类**: ```java @Configuration public class MybatisPlusConfig { @Bean public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean; } } ``` --- #### 2. 验证XML文件路径配置 在`application.yml`中明确指定Mapper XML文件路径,并确保路径包含通配符: ```yml mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml # 支持多级目录 ``` --- #### 3. 核对Mapper接口与XML命名空间 确保XML中的`namespace`与Mapper接口全限定名**完全一致**: ```xml <!-- CharacteristicsMapper.xml --> <mapper namespace="com.example.mapper.CharacteristicsMapper"> <!-- 方法定义 --> </mapper> ``` --- #### 4. 添加接口扫描注解 在Mapper接口上添加`@Mapper`或在启动类添加`@MapperScan`: ```java @Mapper public interface CharacteristicsMapper extends BaseMapper<Characteristics> { } ``` 或 ```java @SpringBootApplication @MapperScan("com.example.mapper") // 指定Mapper接口包路径 public class Application { } ``` --- #### 5. 检查依赖兼容性 Spring Boot 3.x需搭配MyBatis-Plus 3.5.3+版本。建议检查依赖树是否冲突: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.9</version> </dependency> ``` --- #### 6. 清理构建缓存 执行Maven/Gradle清理命令并重新编译,确保XML文件被复制到`target/classes`目录。 --- ### 常见问题总结 | 现象 | 解决方案 | |------|----------| | XML文件未加载 | 检查`mapper-locations`路径通配符 | | 接口未被扫描 | 添加`@MapperScan`或`@Mapper`注解 | | 版本冲突 | 升级MyBatis-至3.5.9+ | ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值