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

使用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.),令人困惑。

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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐