https://blog.csdn.net/qq_33333654/article/details/127240048

前言

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

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

环境配置

本篇文章讲的一切内容都是基于Mybatis3.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_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 collection already contains value for cn.cb.demo.dao.UserMapper.selectList. please check file [H:\\\\work_project\\\\demo\\\\target\\\\classes\\\\mapper\\\\UserInfoMapper.xml] and file [H:\\\\work_project\\\\demo\\\\target\\\\classes\\\\mapper\\\\UserInfoMapper.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,如下图:

https://i-blog.csdnimg.cn/blog_migrate/482bbdd2ffd6a64768e05c13f668dd10.png

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

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