Mybatis的XxxMapper.xml中能否配置重载方法

本文探讨了在Mybatis的Mapper接口和XML配置文件中,方法重载的问题。根据报错信息,Mybatis不支持XML配置文件中的方法重载,因为ID不允许重复,这会导致Mapper动态代理过程出错。因此,Mapper接口中的方法名也必须唯一。

大家是否都知道如何区分方法的重载?

具体以来与以下几点:

①方法名相同,参数列表长度相同参数类型不同

②方法名相同,参数列表长度不同

符合上面几点的都可以被称之为方法重载!

在接触到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接口中的方法名不允许重复!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值