zoukankan      html  css  js  c++  java
  • @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);

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

    严重: 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]

    总结:

    记者:为什么要写注解?

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

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

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

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

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

    简极(∞+∞=∞ ∞-∞=∞)
  • 相关阅读:
    GitTortoise + Git Source Control Provider使用笔记
    Manage your ViewEngine(Wirte by myself)
    JSON DATE 转换标准的一些资料(收集)
    IE 6崩溃第一条禁用所有插件
    invalid label problem
    使用jQuery Html() 作为客户端htmlEncode的问题
    在WIN7下使用XP Mode安装IE6+
    Jquery simple friendly scroll
    Cygwin使用指南
    How to enable Plain Text Password
  • 原文地址:https://www.cnblogs.com/liuhappy/p/8759584.html
Copyright © 2011-2022 走看看