以下为重载的两个方法:

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

  1. 先让两个方法单独执行,确保两个方法不存在问题。

Mapper.java

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

Mapper.xml

<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>-->

测试:

@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

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

Mapper.xml

<!--<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>

测试:

@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

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

Mapper.xml

<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>

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

@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();
}

测试结果:

执行失败,详细报错:

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 不能实现方法重载。