zoukankan      html  css  js  c++  java
  • Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。

    Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。


    错误
    List<Post> listPage(Integer categoryId);
    在测试时报错:There is no getter for property named 'categoryId' in 'class java.lang.Integer'


     问题分析:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.value值,引起报错。
     解决方法:  List<Post> listPage(@Param("categoryId")Integer categoryId); 说明参数值。
     
     sql语句
    <select id="listPage" resultMap="PostResultMap">
    select id,title,summary,create_time from p2p_post
    where status=0 
    <if test="categoryId != null">
     and category_id=#{categoryId}
    </if>
    order by id
    desc
    </select>

     最让人郁闷的是,以前在只有1个参数的时候,都是不用@Param注解的,一般只有在多个参数的时候,才需要用。
     为什么这次,只有1个参数,也必须用@Params注解呢?
     -----------------------
     第2天早上,问了下boss,感觉还是有道理的。
     
     正解一:
     @Select("select * from ..")
     List<Post> listPage(Integer categoryId);
     
     正解二:
      List<Post> listPage(@Param("categoryId")Integer categoryId);
      <select>..</select>
      
     正解三:
       List<Post> listPage(Integer categoryId);
      <select id="listPage" parameterType="java.lang.Integer" resultMap="PostResultMap">
      
      </select>
     
     昨天遇到的那个问题,问题关键就是:xml文件中的select映射语句,默认参数类型是map,从map里取属性,所以总是找不到。
     或者是当作对象类型吧。
     因此,用@Param注解或手动指定参数类型。
     
     理论上是这样,没有去一一校验。
     
     另外需要说明,多个参数,必须使用@Param,或者用Map,或者对象。
  • 相关阅读:
    osg::BlendFunc来设置透明度
    LCA(Tarjan)
    CODEVS1073 家族 (并查集)
    CODEVS1533 互斥的数(哈希表)
    2014-12-4
    BZOJ2661 连连看 (费用流)
    2014-11-30
    JAVA语法基础作业——动手动脑以及课后实验性问题
    课后作业01——相加
    再读大道至简第二章
  • 原文地址:https://www.cnblogs.com/qitian1/p/6462864.html
Copyright © 2011-2022 走看看