Mapper
接口的时候都重载过其中的方法,但是运行起来总是报错(注:在 mybatis-plus 里面不会报错,但是会使用第一个)。和
SpringBoot-2.3.3.RELEASE`。public interface UserMapper {
List selectList(@Param("userIds") List userIds);
List 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
的工厂。MybatisAutoConfiguration
,其中有一段代码就是创建 sqlSessionFactory
解析
XML
文件和Mapper
接口,将 Mapper 中的方法与 XML 文件中、
等标签一一对应,那么 Mapper 中的方法如何与 XML 中
这些标签对应了,当然是唯一的
id
对应了。
![[Pasted image 20241202110426.png]]
SqlSessionFactory
的创建过程中,前面的部分代码都是设置一些配置,并没有涉及到解析 XML 的内容,因此答案肯定是在最后一行 return factory.getObject();
,于是此处打上断点,一点点看。于是一直到了 org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory
这个方法中,其中一段代码如下:xmlMapperBuilder.parse();
就是解析 XML 文件与 Mapper 接口,继续向下看。org.apache.ibatis.builder.xml.XMLMapperBuilder#configurationElement
这个方法中有一行重要的代码,如下图: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 mappedStatements = new StrictMap("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 中的方法不能重载。
进入 mapper 方法!这里直接进到 com.baomidou.mybatisplus.core.override.MybatisMapperProxy#invoke
![[Pasted image 20241202112810.png]]
这里都是 mybatis-plus 的代理。
进入跟进,进入到 com.baomidou.mybatisplus.core.override.MybatisMapperMethod#execute
![[Pasted image 20241202112824.png]]
这里相当于执行
Object result = sqlSession.selectOne("com.demo.mapper.xxxxMapper.xxxx", param);
![[Pasted image 20241202112850.png]]
在这里会进入 Mybatis 的 slectList 方法(org.apache.ibatis.session.defaults.DefaultSqlSession#selectList
)。
![[Pasted image 20241202112857.png]]
从 configuration
中生成所有的 mappedStatements,然后从 statements 中获取根据 id,也就是方法的全路径,获取当前的 statements。
![[Pasted image 20241202112904.png]]
com.demo.mapper.xxxxMapper.xxxx
在这里面只有一个。mapper[xxx] is ignored, because it exists, maybe from xml file
List userInfos = userMapper.selectList(Arrays.asList("192","198"));
一行简单的调用到底如何找到对应的SQL呢?其实就是根据
id
从Map
中查找对应的mappedStatements MappedStatement
。
org.apache.ibatis.session.defaults.DefaultSqlSession#selectList
方法有这一行代码如下图:MappedStatement ms = configuration.getMappedStatement(statement);
这行代码就是根据 id
从 mappedStatements
获取对应的 MappedStatement
,源码如下:public MappedStatement getMappedStatement(String id) {
return this.getMappedStatement(id, true);
}
id=Mapper的全类名+'.'+方法名
,导致不能重载。参与评论
手机查看
返回顶部