mybatis参数问题:
-------------------------------------------------------------------<select id="countForToday" resultType="int" parameterType="string">
  select count(*) from knowledgearticle where ka_date = current_date
  <if test="classid!=null and classid != ''">
   and ka_classid = #{classid}
  </if>
</select>-------------------------------------------------------------------
如上参数是一个string类型的字符串,运行时会报错:
ReflectionException: There is no getter for property named 'classid' in 'class java.lang.String'这是个字符串不是实体啊,哪有getter for property named 'classid' ,晕...疑惑:在select中如何用这个传入的string参数? 没有名字啊,不像实体一样,我这里用的classid随便取的,叫id效果也是一样的,好像不能代表传入的那个字符串
请问各位高手,mybatis是怎样处理这种情况的? 怎样处理单个参数在下面的引用?

解决方案 »

  1.   

    可以传入个Object参数 里面有个属性为classid 提供set get
      

  2.   

    ibatis 没有if,不知道Mybatis
    ibatis应该为:
    <isNotEmpty prepend="and" property="classid">
                    ka_classid = #classid#
                </isNotEmpty>
      

  3.   

    又是你啊!首先,参数是可以这么传的,parameterType="java.lang.String"
    #{classid}是否和你 传入的参数名称一样?
    在确认下,绝对可以是你那样传的,调试下,看看是否类型错误!
      

  4.   

    一楼的大哥让人很郁闷哦,一个参数用得着用object封装个实体,加一个属性吗? 你就是这么做项目的?<select id="countForToday" resultType="int" parameterType="string">
      select count(*) from knowledgearticle where ka_date = current_date
      <if test="classid!=null and classid != ''">
       and ka_classid = #{classid}
      </if>
    </select>这是mybatis的写法,语法是没错的,mybatis没有property="classid"这种属性。
    继续等高手... 
      

  5.   


    <select id="countForToday" resultType="int" parameterType="string">
        select count(*) from knowledgearticle where ka_date = current_date
        <if test="classid!=null and classid != ''">
            and (ka_classid = #{classid} or ka_classid in (select kno_dbid from knowledgeclass where kno_parent = #{classid}))
        </if>
    </select> /**
     * 今日更新的数量
     */
    public int countForToday(String classid)
    {
    SqlSession session = sessionFactory.openSession();
    try 
    {
    int article = (Integer)session.selectOne("Article.countForToday",classid);
    return article;
    }
    catch(Exception e) 
    {
    log.error("获取今日更新的数量失败",e);
    }
    finally
    {
    session.close();
    }
    return 0;
    }### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'classid' in 'class java.lang.String'
      

  6.   

    苦啊, 从昨晚搞到现在,找不出核心问题原因啊..nazhawenxi 这位朋友能否加QQ聊啊,给你留言了.
      

  7.   

    <if test="classid!=null and classid != ''">
       and ka_classid = #{classid}
      </if>改为 and ka_classid = #{classid}把标签去掉就可以了,标签是针对JAVABEAN或者MAP的,STRING不能用标签
      

  8.   

    如果非要用就把参数改成MAP,在传参数的时候,把STRING放在MAP里就这些
      

  9.   

    在拼写Sql语句的那个xml里加上如同这段的Code试试
    <resultMap type="sExtension" id="sExtensionResult">
    <result property="id" column="id" />
    <result property="euid" column="euid"/>
    <result property="extnum" column="extnum" />
    <result property="extcode1" column="extcode1" />
    <result property="extcode2" column="extcode2" />
    <result property="extcode3" column="extcode3" />
    <result property="addtime" column="addtime" />
    </resultMap>  那么你的那个classid 在property中的值 与实体类保持一致就好,后面的colum不用多说你也该知道,就是数据库表中的列名,理所应当和你的数据库中的保持一致就好。试试吧,应该没问题。
      

  10.   

    #{classid} 改为 #{value},即可。
      

  11.   

    <if test="_parameter!=null and _parameter != ''">
       and ka_classid = #{classid}
      </if>