There is no getter for property named 'departId' in 'class java.lang.Integer'异常

Posted by Jesse' Notes on November 23, 2018

今天遇到的报错 There is no getter for property named 'departId' in 'class java.lang.Integer'

代码重现如下: mapper接口:

1
2
3
4
5
6
7
8
public interface DistributeMapper {
    List<DistributePO> queryOtherDistribute(Integer departId);

    List<DistributePO> queryDepartDistribut(Integer departId);

    List<DistributePO> queryWorkNum(Integer departId);
}

一糊涂将int写成了integer mapper的SQL语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   <select id="queryWorkNum" resultMap="BaseResultMap">
        with RECURSIVE dept as (
        select d.id as super_id,d.department_name as super_name,d.id as sub_id,cast(d.department_name as varchar(100))
        as name,d.department_name,1 as dept_rank
        from hello_pivot.t_department_info d
        -- where d.data_status=1 and department_parent_id=0 -- 全部
        where d.data_status=1
        <choose>
            <when test="departId!= -1">
                and id=#{departId} -- 某个部门
            </when>
            <otherwise>
                and department_parent_id = 0
            </otherwise>
        </choose>
        ...
        ...

    </select>

“Integer departId” 加上 when test=”departId!= -1” 导致了报错。 原因是: 在mybatis中,采用ONGL解析参数,所以会自动采用对象树的形式获取传入的变量值。这里传入参数的类型为 integer 获取departId参数的值时,其实获取的就是Integer的departId属性值。所以这样的写法是不对的。 解决办法:参数前面加@Parm(“departId”) 注解

1
2
3
public interface DistributeMapper {
    List<DistributePO> queryXX(@Param("departId")Integer departId);
}

或者

1
2
3
 <if test="_parameter != null">
            and id = #{departId} 
        </if>