mybatis-plus 调用自带方法报错 Invalid bound statement

Posted Shinka_YXS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-plus 调用自带方法报错 Invalid bound statement相关的知识,希望对你有一定的参考价值。

mybatis-plus 调用自带方法报错 Invalid bound statement

1.检查版本冲突

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>3.4.3</version>
  <scope>compile</scope>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.3</version>
    <scope>compile</scope>
</dependency>

2.检查路径以及namespace对应关系

  • 检查扫描编译路径

    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/publicDB/**/*.xml"));
    

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <resource>
    	    <directory>src/main/resources</directory>
    	    </resource>
        </resources>
    </build>
    
  • 检查引入的BaseMapper路径

    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    
  • 检查xml文件的namespace、函数名称对应等,保证编译不报错

  • 去掉xml文件中的中文注释

3.使用MybatisSqlSessionFactory

/**
 * 创建 SqlSessionFactory
 */
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception 
    
    // SqlSessionFactory不要使用原生的,应该使用MybatisSqlSessionFactory
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/publicDB/**/*.xml"));

    return bean.getObject();

原博文配置的MybatisPlusConfig配置类:

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 已经自动加载的资源。不手动指定 <p> 配置文件和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(true);
        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;
    

4.参考博文

mybatis-plus的BaseMapper调用报错:Invalid bound statement

使用mybatis-plus BaseMapper 遇到的小毛病Invalid bound statement (not found)

以上是关于mybatis-plus 调用自带方法报错 Invalid bound statement的主要内容,如果未能解决你的问题,请参考以下文章

mybatis-plus 调用自带方法报错 Invalid bound statement

mybatis-plus调用自身的 selectById 方法报错:org.apache.ibatis.binding.BindingException:

项目中途引入Mybatis-plus后报错:Caused by: java.lang.ClassNotFoundException: org.mybatis.logging.LoggerFactory

如何解决mybatis-plus调用update方法时,自动填充字段不生效问题

日常Exception第三十四回:mybatis-plus插入insert时null值导致报错,ExpressionSyntaxException: Malformed OGNL expressi

MyBatis-Plus--使用雪花算法生成主键ID--使用/分析