大家是否都知道如何区分方法的重载?
具体以来与以下几点:
①方法名相同,参数列表长度相同参数类型不同
②方法名相同,参数列表长度不同
符合上面几点的都可以被称之为方法重载!
在接触到Mybatis后,发现XxxMapper.java的接口以及XxxMapper.xml中却不允许重载方法的配置了。
期间遇到报错信息如下:
java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.whw.mapper.MovieMapper.getMoviesByGenre
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:872)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:844)
at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:668)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:302)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:109)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:135)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:128)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:173)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:124)
at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
阅读报错信息后发现其说明"已有com.whw.mapper.MovieMapper.getMoviesByGenre存在了"
观察MovieMapper.xml中的配置:
<!--根据模糊条件查询电影列表的片段-->
<select id="getMoviesByGenre" resultType="Movie">
select * from `movie` where gids like #{gids} limit #{startIndex},#{range};
</select>
<!--根据模糊条件查询电影列表的所有-->
<select id="getMoviesByGenre" resultType="Movie">
select * from `movie` where gids like #{gids};
</select>
所以Mybatis中不允许方法重载的出现,会导致Mapper动态代理过程中无法锁定getMoviesByGenre方法
所以MovieMapper.xml中的ID不允许重复,这也就导致了mapper接口中的方法名不允许重复!
本文探讨了在Mybatis的Mapper接口和XML配置文件中,方法重载的问题。根据报错信息,Mybatis不支持XML配置文件中的方法重载,因为ID不允许重复,这会导致Mapper动态代理过程出错。因此,Mapper接口中的方法名也必须唯一。

1068

被折叠的 条评论
为什么被折叠?



