mybatis中,单个参数在xml中使用if标签导致There is no getter for property named 'type' in 'class java.lang.Integer'的错误

在dao层声明了一个单参数的方法

List<ADVO> listNoExpire(Integer type);

我想在mybaits中的xml中进行判断,如果type为null则查询所有,如果有值则进行where查询

然后我一开始的xml是这么写的

<select id="listNoExpire" resultType="com.lqc.admin.ad.ADVO" parameterType="java.lang.Integer">
  select
  <include refid="Base_Column_List" />
  from t_ad ad
  where ad.expiretime >=now()
  <if test="type != null">
    ad.type = #{type,jdbcType=INTEGER}
  </if>
  order by ad.sort
</select>

结果会一直报:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'type' in 'class java.lang.Integer'

的错误。后来查询资料之后解决办法是:
mybaits中,使用单参数的情况下,xml中引用这个参数可以直接使用#{name},这里的name可以使用任意名字,不过我习惯性使用接口中命名的名字。并且在#{}中最好把参数类型也指定了。如:

#{type,jdbcType=INTEGER}

但是在if标签中,如果要引用参数,需要使用_parameter来代替。要不就会出现上头的报错。

最终xml的写法是:

<select id="listNoExpire" resultType="com.lqc.admin.ad.ADVO" parameterType="java.lang.Integer">
  select
  <include refid="Base_Column_List" />
  from t_ad ad
  where ad.expiretime >=now()
  <if test="_parameter != null">
    and ad.type = #{type,jdbcType=INTEGER}
  </if>
  order by ad.sort
</select>

觉得内容还不错?打赏个钢镚鼓励鼓励!!👍