mybatis出现Invalid bound statement

出现错误:

情景一:Invalid bound statement (not found): **.dao.mapper.DataSetMapper.selectList

情景二: Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 35

当情景一和情景二同时出现时可能存在以下可能性:

可能一: 没有读取到mapper文件

检查配置@MapperScan(basePackages = "com.xxx.mapper")

检查mybatis.mapper-locations: classpath:mapper/*.xml的配置路径是否正确

注意项目的target目录下是否有mapper.xml文件,如果没有则要在pom.xml加入如下配置

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

可能二: mybatis puls的坑

使用mybatis-plus时不能使用自带的SqlSessionFactory,要使用MybatisSqlSessionFactory

@Slf4j
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
    @Autowired
    private Environment environment;

    @Autowired
    private DataSource mysqlDataSource;

    @Bean
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration globalConfiguration() {
        return new MybatisConfiguration();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean ();
        sqlSessionFactoryBean.setDataSource(mysqlDataSource);
        sqlSessionFactoryBean.setConfiguration(globalConfiguration());
        sqlSessionFactoryBean.setTypeAliasesPackage(environment.getProperty("mybatis-plus.type-aliases-package"));
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(environment.getProperty("mybatis-plus.mapper-locations")));

        return sqlSessionFactoryBean.getObject();
    }
}

数据库配置项

# Mybatis-plus配置
mybatis-plus:
  # 配置Mapper映射文件
  mapper-locations: classpath*:/mapper/*Mapper.xml
  # 配置Mybatis数据返回类型别名(默认别名是类名)
  type-aliases-package: com.**.**.api.model.entity
  configuration:
    # 自动驼峰命名
    map-underscore-to-camel-case: false
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Mybatis报"Invalid bound statement"错误一般有三种可能的原因。首先,可能是由于mapper.xml文件不存在导致的。其次,可能是mapper.xml文件中的内容有误。最后,可能是mapper.xml文件的路径配置有误。解决这个问题的方法可以从以下几个方面入手。 首先,检查mapper.xml文件是否存在,确保文件路径和文件名拼写正确。如果文件不存在,需要创建或者重新配置路径。 其次,仔细检查mapper.xml文件中的内容,确保语法正确,标签闭合正确且与数据库表结构对应。特别注意检查SQL语句是否正确,包括表名、列名、参数等。 最后,检查mybatis的配置文件,确保mapper.xml文件的路径配置正确。可以在配置文件中查找mapper-locations属性,确认路径是否正确设置为mapper.xml所在的位置。 如果以上方法都没有解决问题,可以尝试在报错的位置进行调试。根据中的代码片段,可以定位到具体的报错位置并进一步排查问题。 总结来说,解决"Invalid bound statement"错误可以通过检查mapper.xml文件是否存在、确认文件内容和路径配置是否正确来解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis/mybatis plus报错:Invalid bound statement (not found) 解决方法汇总](https://blog.csdn.net/mashangzhifu/article/details/122808181)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [mybatis报错:Invalid bound statement (not found)](https://blog.csdn.net/weixin_44736853/article/details/124857282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值