Mybatis的Mapper中的方法为什么不能重载?

本文探讨了Mybatis中Mapper接口方法无法重载的原因,通过分析源码,指出在Springboot与Mybatis集成环境下,由于Mapper接口方法与XML中的SQL映射标签一一对应,且映射集合不允许重复,导致方法重载会引发异常。解析过程涉及MapperFactoryBean、MapperScannerConfigurer以及XML映射文件的解析等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  • 在初入门Mybatis的时候可能都犯过一个错误,那就是在写Mapper接口的时候都重载过其中的方法,但是运行起来总是报错,那时候真的挺郁闷的,但是自己也查不出来原因,只能默默的改了方法名,哈哈,多么卑微的操作。

  • 今天就写一篇文章从源码角度为大家解惑为什么Mybatis中的方法不能重载?

环境配置

  • 本篇文章讲的一切内容都是基于Mybatis3.5SpringBoot-2.3.3.RELEASE

错误示范

  • 举个栗子:假设现在有两个需求,一个是根据用户的id筛选用户,一个是根据用户的性别筛选,此时在Mapper中重载的方法如下:

public interface UserMapper {
    List<UserInfo> selectList(@Param("userIds") List<String> userIds);

    List<UserInfo> selectList(Integer gender);
    }
  • 这个并没有什么错误,但是启动项目,报出如下的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [H:\work_project\demo\target\classes\mapper\UserInfoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [H:\work_project\demo\target\classes\mapper\UserInfoMapper.xml]'. Cause: java.lang.IllegalArgumentException: Mapped Statements collect
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值