• 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,或者对象。
  • 相关阅读:
    jquery
    模板库
    Luogu P1902 刺杀大使
    8.20模拟赛再次观光记
    Luogu P1122 最大子树和
    Luogu P1470 最长前缀 Longest Prefix
    8.18爆炸记
    Luogu P1388 算式
    Luogu P1103 书本整理
    8.17
  • 原文地址:https://www.cnblogs.com/qitian1/p/6462864.html
Copyright © 2020-2023  润新知