Mapper类中存在名称相同的方法重载报错 您所在的位置:网站首页 识别重复文件的方法 Mapper类中存在名称相同的方法重载报错

Mapper类中存在名称相同的方法重载报错

2024-07-12 06:11| 来源: 网络整理| 查看: 265

项目场景:

官网项目开发

问题描述: Mapper类中存在名称相同的方法重载报错

为了省事,直接在mapper层通过@select@update等注解形式写sql (此时大都是单表查询没有创建mapper.xml文件)代码如下:

@Update("") void updateSortBySort(Integer newSort, Integer platform, Integer classification); @Update("") void updateSortBySort(Integer oldSort, Integer newSort, Integer platform, Integer classification);

此时可以正常使用 接下来因为业务原因,需要进行多表查询,仅使用mybatis自带的不行,需要使用mapper.xml文件写sql脚本,进行多表查询

@Update("") void updateSortBySort(Integer newSort, Integer platform, Integer classification); @Update("") void updateSortBySort(Integer oldSort, Integer newSort, Integer platform, Integer classification); IPage getlistPage(IPage page, @Param("Param1")Integer Param1, @Param("Param2")Integer Param2);

此时调用第一个updateSortBySort方法的接口,会报一个“Parameter ‘oldSort’ not found 就是参数不存在的错误”

原因分析:

找到了原因:在使用@Select等注解的情况下,方法名即为mapper的id,id相同。重载的方法会报这个错 假如项目启动时候通过反射获取mapper方法数组顺序先是在第一个 此时调用方法,正常,不会有问题; 因为mybatis查找mapper内的方法是靠方法名,和参数无关。方法名就是mapper.xml里面的id一样

当不进行方法的重载时,即:每个方法都有唯一的命名时,在xml中进行映射后,就可以执行,不会出现异常。

所以mybatis中mapper.xml是不会准确映射到Java中的重载方法的。最好不要在mapper接口中使用方法重载。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有