Mybatis是否可以实现方法重载

以下为重载的两个方法:

1
2
List<TemplateDTO> selectAll();
List<TemplateDTO> selectAll(List<Integer> idList);
  1. 先让两个方法单独执行,确保两个方法不存在问题。

Mapper.java

1
2
List<TemplateDTO> selectAll();
//List<TemplateDTO> selectAll(List<Integer> idList);

Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<select id="selectAll" resultMap="DTOResultMap">
select
<include refid="DTO_Column_List"/>
from promotion_template
where is_deleted = 0
</select>
<!--<select id="selectAll" parameterType="java.util.List" resultMap="DTOResultMap">-->
<!--select-->
<!--<include refid="DTO_Column_List"/>-->
<!--from promotion_template-->
<!--where is_deleted = 0 and id in-->
<!--<foreach collection="list" open="(" close=")" separator="," item="id" index="i">-->
<!--#{id}-->
<!--</foreach>-->
<!--</select>-->

测试:

1
2
3
4
5
6
7
8
@Test
public void testOverLoadInMapper() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
TemplateMapper templateMapper = sqlSession.getMapper(TemplateMapper.class);
List<TemplateDTO> templateDTOList = templateMapper.selectAll();
templateDTOList.forEach(templateDTO -> System.out.println(templateDTO));
sqlSession.close();
}

测试结果:

测试通过,方法一没有问题。

Mapper.java

1
2
//List<TemplateDTO> selectAll();
List<TemplateDTO> selectAll(List<Integer> idList);

Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--<select id="selectAll" resultMap="DTOResultMap">-->
<!--select-->
<!--<include refid="DTO_Column_List"/>-->
<!--from promotion_template-->
<!--where is_deleted = 0-->
<!--</select>-->
<select id="selectAll" parameterType="java.util.List" resultMap="DTOResultMap">
select
<include refid="DTO_Column_List"/>
from promotion_template
where is_deleted = 0 and id in
<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
#{id}
</foreach>
</select>

测试:

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testOverLoadInMapper() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
TemplateMapper templateMapper = sqlSession.getMapper(TemplateMapper.class);
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2);
idList.add(21);
List<TemplateDTO> templateDTOList = templateMapper.selectAll(idList);
templateDTOList.forEach(templateDTO -> System.out.println(templateDTO));
sqlSession.close();
}

测试结果:

测试通过,方法二没有问题。

  1. 接下来进行重载测试。

Mapper.java

1
2
List<TemplateDTO> selectAll();
List<TemplateDTO> selectAll(List<Integer> idList);

Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<select id="selectAll" resultMap="DTOResultMap">
select
<include refid="DTO_Column_List"/>
from promotion_template
where is_deleted = 0
</select>
<select id="selectAll" parameterType="java.util.List" resultMap="DTOResultMap">
select
<include refid="DTO_Column_List"/>
from promotion_template
where is_deleted = 0 and id in
<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
#{id}
</foreach>
</select>

测试此时方法二是否可以成功执行。

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testOverloadInMapper() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
TemplateMapper templateMapper = sqlSession.getMapper(TemplateMapper.class);
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2);
idList.add(21);
List<TemplateDTO> templateDTOList = templateMapper.selectAll(idList);
templateDTOList.forEach(templateDTO -> System.out.println(templateDTO));
sqlSession.close();
}

测试结果:

执行失败,详细报错:

1
2
3
4
5
org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/ybl/clickcube/promotion/template/mapper/TemplateMapper.xml
### The error occurred while processing mapper_resultMap[DTOResultMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.ybl.clickcube.promotion.template.mapper.TemplateMapper.selectAll

stackoverflow上,有人解释说,“If you have the same method name, then mybatis throws the Mapped Statements collection already contains value error message. So the solution is to have different method names for different mapper statements even if the method signatures are different.”。

http://nihao-shijie.lofter.com/post/1cd58fc5_badfb6c解释了为什么mybatis不能实现方法重载。

-------------本文结束感谢您的阅读-------------

本文标题:Mybatis是否可以实现方法重载

文章作者:huihui

发布时间:2018年09月28日 - 11:09

最后更新:2019年02月14日 - 19:02

原始链接:http://101.200.47.120:8011/2018/09/28/Mybatis是否可以实现方法重载/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。