0
点赞
收藏
分享

微信扫一扫

mybatis中找不到字段问题org.apache.ibatis.binding.BindingException: Parameter ‘id‘ not found


如果参数只有一个对象,则无须显式写对象名;否则需要。

使用EasyCodeMybatisCodeHelper生成代码,调用其中的queryAllByLimit时报错了,说找不到参数“id"。

List<Power> queryAllByLimit(Power power, @Param("pageable") Pageable pageable);

<!--查询指定行数据-->
 <select id="queryAllByLimit" resultMap="BaseResultMap">
     select
     ID, MASTER_ID, NAME, CODE, ISBACKUP,NOTE
     from power
     <where>
         <if test="id != null">
             and ID = #{id}
         </if>
         <if test="masterId != null">
             and MASTER_ID = #{masterId}
         </if>
         <if test="name != null and name != ''">
             and NAME like concat('%',#{name},'%')
         </if>
         <if test="code != null and code != ''">
             and CODE like concat('%',#{code},'%')
         </if>
         <if test="note != null and note != ''">
             and NOTE like concat('%',#{note},'%')
         </if>
         <if test="isbackup != null">
             and ISBACKUP = #{isbackup}
         </if>
     </where>
     limit #{pageable.offset}, #{pageable.pageSize}
 </select>

后来没办法,将mapper/*.xml里的改为,参数前面加上对象名称:

<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="BaseResultMap">
    select
    ID, MASTER_ID, NAME, CODE, ISBACKUP,NOTE
    from power
    <where>
        <if test="power.id != null">
            and ID = #{power.id}
        </if>
        <if test="power.masterId != null">
            and MASTER_ID = #{power.masterId}
        </if>
        <if test="power.name != null and power.name != ''">
            and NAME like concat('%',#{power.name},'%')
        </if>
        <if test="power.code != null and power.code != ''">
            and CODE like concat('%',#{power.code},'%')
        </if>
        <if test="power.note != null and power.note != ''">
            and NOTE like concat('%',#{power.note},'%')
        </if>
        <if test="power.isbackup != null">
            and ISBACKUP = #{power.isbackup}
        </if>
    </where>
    limit #{pageable.offset}, #{pageable.pageSize}
</select>

考察dao中的语句:

List<Power> queryAllByLimit(Power power, @Param("pageable") Pageable pageable);

有两个参数,power和pageable。EasyCodeMybatisCodeHelper生成的代码中,只为分页加了对象p前缀(pageable.),却没有为字段加上对象前缀(power.),令人困惑。

不过,如果只有一个参数,就无须加前缀。


举报

相关推荐

0 条评论