前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@Select注解的情况下,重载的报错

@Select注解的情况下,重载的报错

作者头像
Happy、Liu
发布2019-04-24 16:23:27
5830
发布2019-04-24 16:23:27
举报

在编写代码的时候,我对查询这个方法进行了重载,这样调用的时候会根据参数的不同,进而去执行不同的操作,但是......问题来了。想法都是美好的,实际情况却不是我理想的状态。运行代码的时候他动了几下,然后一片红色。

我仔细看了看代码:

 1     @Select("INSERT  INTO comment (com_pro,user_name,com_content,com_score) VALUES (#{comment.com_pro},#{comment.user_name},#{comment.com_content},#{comment.com_score}")
 2     public int insertComment(Comment comment);
 3     
 4     @Select("DELETE FROM comment WHERE com_id =#{comment.com_id}")
 5     public int delCommentById(int id);
 6     
 7     @Select("DELETE FROM comment where user_name=#{comment.user_name}")
 8     public int delCommentAll(String name);
 9     
10     @Select("update comment set com_content=#{comment.com_content},com_score=#{comment.com_score}  where com_id=#{comment.com_id}")
11     public int updateComment(Comment comment);
12     
13     @Select("SELECT * FROM comment where com_pro=#{comment.com_pro}")
14     public List<Comment> findCommentAll(String proname);
15     
16     @Select("SELECT * FROM comment where com_pro=#{proname} and com_score=#{com_score}")
17     public List<Comment> findCommentAll(String proname,String com_score);

当我运行代码的时候,就会就收到如下错误:

严重: Error while adding the mapper 'interface com.sh.dao.CommentDao' to configuration.
java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.sh.dao.CommentDao.findCommentAll

告诉我这个方法“添加映射的接口的COM的错误。sh.dao commentdao”配置。”(百度翻译)

方法名是否真的不可以一样?

答案是肯定的,少侠。

mybatis @Select是不支持方法重载的(如果红字不算执行结果的话)。

封印代码:

 1     @Select("INSERT  INTO comment (com_pro,user_name,com_content,com_score) VALUES (#{comment.com_pro},#{comment.user_name},#{comment.com_content},#{comment.com_score}")
 2     public int insertComment(Comment comment);
 3     
 4     @Select("DELETE FROM comment WHERE com_id =#{comment.com_id}")
 5     public int delCommentById(int id);
 6     
 7     @Select("DELETE FROM comment where user_name=#{comment.user_name}")
 8     public int delCommentAll(String name);
 9     
10     @Select("update comment set com_content=#{comment.com_content},com_score=#{comment.com_score}  where com_id=#{comment.com_id}")
11     public int updateComment(Comment comment);
12     
13 //    @Select("SELECT * FROM comment where com_pro=#{comment.com_pro}")
14 //    public List<Comment> findCommentAll(String proname);
15 //    
16 //    @Select("SELECT * FROM comment where com_pro=#{proname} and com_score=#{com_score}")
17 //    public List<Comment> findCommentAll(String proname,String com_score);

运行我的Test:

1     @Test
2     public void test() {
3         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
4         ActivityService ad = ctx.getBean(ActivityService.class);
5         List<Activity> list = ad.findActivityAll();
6         for (Activity e : list) {
7             System.out.println(e.toString());
8         }
9     }

得到输出结果:

1 Activity [act_id=1, act_theme=双十一, act_img=12345, act_time=Sat Nov 11 00:00:00 CST 2017]

总结:

记者:为什么要写注解?

我:没有钱了,肯定要做啊,不做没有钱用。

记者:你有手有脚的怎么不去写重载?

我:重载方面......重载是不可能重载的 这辈子不可能重载的,做实现类又不会写,就是接口这种东西,才能维持得了生活这样子.

记者:那你觉得注解好还是看重载好?

我:不写重载的感觉像回家一样,我一年写重载,大年三十 晚上我都不回去,就平时重载出点事,我就回去看看这样子,注解的感觉,比重载感觉好多了,在重载的时候一个人很无聊,都没有朋友,女朋友玩 ,写了注解发现个个都是人才,说话又好听,超 喜欢写注解。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档