解决mybatis plus 执行xml和接口无法映射异常,报错:org.apache.ibatis.binding.BindingException: Invalid bound statement

mybatis-plus执行报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
在这里插入图片描述

yml文件配置:在这里插入图片描述
原因:接口和xml文件无法映射成功
网上已经有很多文章说明可能导致这个报错的原因,无非是以下几种:
1.检查xml文件的namespace是否正确
2.Mapper.java的方法在Mapper.xml中没有,然后执行Mapper的方法会报此
3.xxxMapper.java的方法返回值是List,而select元素没有正确配置ResultMap,或者只配置ResultType
4.如果你确认没有以上问题,请任意修改下对应的xml文件,比如删除一个空行,保存.问题解决
5.看下mapper的XML配置路径是否正确
6.xml文件名和接口文件是否一致
如果上面几个方案都确认不是导致问题的原因,可以参考一下另外两种特殊情况。

在这里插入图片描述

  • 第一种:mapper.xml文件,没有被编译出来,正常在编译后的target文件中,会带有xml文件,如果编译后没有,需要修改pom文件
    在这里插入图片描述
    pom文件配置:
	<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.json</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
   </build>

第二种:自定义配置sqlSessionFactory,使默认的sqlSessionFactory失效,导致yml文件中配置的MapperLocationsTypeAliasesPackage没有生效。
自定义的sqlSessionFactory的配置中,直接在代码里面配置MapperLocationsTypeAliasesPackage
在这里插入图片描述

sqlSessionFactory配置代码

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(this.dataSource());
        // 可以在这里设置其他配置,比如MapperLocations等
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/*.xml"));
        factoryBean.setTypeAliasesPackage("com.test.cloud.service.mybatisplusdemo.domian");
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }

    @Bean("druidDataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setDriverClassName(driverClassName);
        return druidDataSource;
    }

参考链接:
解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
Invalid bound statement (not found) 终极解决办法
spring boot mybatis 报错Invalid bound statement (not found)解决过程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值