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/xxx/**/*.xml"));
    

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <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/xxx/**/*.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)

  • 18
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值