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

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

1.检查版本冲突

<dependency>
  <groupId>com.baomidougroupId>
  <artifactId>mybatis-plusartifactId>
  <version>3.4.3version>
  <scope>compilescope>
dependency>

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.4.3version>
dependency>

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-extensionartifactId>
    <version>3.4.3version>
    <scope>compilescope>
dependency>

mybatis-plus 调用自带方法报错 Invalid bound statement_第1张图片

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

  • 检查扫描编译路径

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

    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>
        <resource>
    	    <directory>src/main/resourcesdirectory>
    	    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 已经自动加载的资源。不手动指定 

配置文件和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)

你可能感兴趣的:(MybatisPlus,报错)