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

前言

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

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

环境配置

  • 本篇文章讲的一切内容都是基于 Mybatis 3.5 SpringBoot-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_projectdemotargetclassesmapperUserInfoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [H:work_projectdemotargetclassesmapperUserInfoMapper.xml]'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for cn.cb.demo.dao.UserMapper.selectList. please check file [H:work_projectdemotargetclassesmapperUserInfoMapper.xml] and file [H:work_projectdemotargetclassesmapperUserInfoMapper.xml]
 at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
 at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1509)
 ... 81 more
  • 这么一大串什么意思?懵逼了~

  • 大致的意思:cn.cb.demo.dao.UserMapper.selectList这个id已经存在了,导致创建sqlSessionFactory 失败。

 

为什么不能重载?

通过上面的异常提示可以知道创建 sqlSessionFactory 失败了,这个想必已经不陌生吧,顾名思义,就是创建SqlSession的工厂。

Springboot与Mybatis会有一个启动器的自动配置类 MybatisAutoConfiguration,其中有一段代码就是创建 sqlSessionFactory,如下图:

 

既然是创建失败,那么肯定是这里出现异常了,这里的「大致思路」就是:

解析 XML 文件和 Mapper 接口,将Mapper中的方法与XML文件中<select><insert>等标签一一对应,那么Mapper中的方法如何与XML中 <select> 这些标签对应了,当然是唯一的id对应了,具体如何这个id的值是什么,如何对应?下面一一讲解。

如上图的 SqlSessionFactory 的创建过程中,前面的部分代码都是设置一些配置,并没有涉及到解析XML的内容,因此答案肯定是在最后一行 return factory.getObject();,于是此处打上断点,一点点看。于是一直到了org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory 这个方法中,其中一段代码如下:

 

这里的 xmlMapperBuilder.parse();就是解析XML文件与Mapper接口,继续向下看。

略过不重要的代码,在org.apache.ibatis.builder.xml.XMLMapperBuilder#configurationElement 这个方法中有一行重要的代码,如下图:

 

此处就是根据XML文件中的 select|insert|update|delete 这些标签开始构建 MappedStatement 了。继续跟进去看。

略过不重要的代码,此时看到org.apache.ibatis.builder.MapperBuilderAssistant#addMappedStatement 这个方法返回值就是MappedStatement,不用多说,肯定是这个方法了,仔细一看,很清楚的看到了构建id的代码,如下图:

 

从上图可以知道,创建 id 的代码就是 id = applyCurrentNamespace(id, false);,具体实现如下图:

 

上图的代码已经很清楚了,MappedStatement 中的 id = Mapper的全类名+'.'+方法名。如果重载话,肯定会存在id相同的 MappedStatement

到了这其实并不能说明方法不能重载啊,重复就重复呗,并没有冲突啊。这里需要看一个结构,如下:

protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection")
      .conflictMessageProducer((savedValue, targetValue) ->
          ". please check " + savedValue.getResource() + " and " + targetValue.getResource());

构建好的MappedStatement都会存mappedStatements 中,如下代码:

public void addMappedStatement(MappedStatement ms) {
    //key 是id 
    mappedStatements.put(ms.getId(), ms);
  }

StrictMap put(k,v)方法如下图:

 

到了这里应该理解了吧,这下抛出的异常和上面的异常信息对应起来了吧。这个StrictMap不允许有重复的key,而存入的key就是id。因此 Mapper 中的方法不能重载。

如何找到XML中对应的SQL?

在使用Mybatis的时候只是简单的调用Mapper中的方法就可以执行SQL,如下代码:

List<UserInfo> userInfos = userMapper.selectList(Arrays.asList("192","198"));

一行简单的调用到底如何找到对应的SQL呢?

其实就是根据 id Map<String,MappedStatement> mappedStatements

查找对应的 MappedStatement

org.apache.ibatis.session.defaults.DefaultSqlSession#selectList方法有这一行代码如下图:

 

MappedStatement ms = configuration.getMappedStatement(statement);这行代码就是根据idmappedStatements 获取对应的 MappedStatement ,源码如下:

public MappedStatement getMappedStatement(String id)
{ 
     return this.getMappedStatement(id, true);
}

总结

  • 文章写到这,想必已经很清楚Mapper中的方法为什么不能重载了,归根到底就是因为这个这个id = Mapper的全类名 + '.' + 方法名

原文链接:Mybatis的Mapper中的方法为什么不能重载? | 程序员灯塔

该文章为蚂蚁P8大佬写的文章,仅供学习参考

如果各位看完感触颇深,可以关注这位大佬的公众号,里边有很多宝藏文章

 

  • 24
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值