Mybatis傳多個參數的問題 及MyBatis報錯 Parameter '0' not found. Available parameters are [arg1, arg0, param1 問題


對於使用Mybatis ,傳多個參數,我們可以使用對象封裝外,還可以直接傳遞參數

對象的封裝,例如查詢對象條件basequery對象

<select id="getProductByProductQuery" parameterType="com.niulande.product.query.BaseQuery" resultMap="BaseResultMap">

    select
    <include refid="Base_Column_List" />
    from pd_product
    <include refid="whereSql"/>
  </select>
  <sql id= "whereSql" >
    <where>
      <if test="gameCode != null and gameCode != ''" >
        and game_type_coding = #{gameCode}
      </if>
      <if test="goodsTypeId != null">
        and goods_type_id = #{goodsTypeId}
      </if>
      <if test="accId != null">
        and account_id = #{accId}
      </if>
      <if test="delFlag != null">
        and del_flag = #{delFlag}
      </if>
    </where>
    limit #{start},#{rows}
  </sql>
</mapper>

 

直接傳遞參數

例如:

mapper方法

selectByGameIdAndGoodsTypeId(Long gameTypeId, Long goodsTypeId);

對應的xml文件方法:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from pd_game_goods_type_mid
  where game_type_id = #{gameTypeId} AND goods_type_id = #{goodsTypeId}
</select>

 

 第一:在select標簽后就不再使用parameterType,因為這個標簽只能指定一個參數,而兩個參數及以上的,則不用再使用

第二:在sql語句里面以上的寫法是錯誤的(為了演示執行報錯)

會報錯

Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2] 注意這里使用的mybatis的版本號 在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}

0是指參數的索引,從0開始。第一個參數是0,第二個參數是1,依次類推

以下正確的寫法:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from pd_game_goods_type_mid
  where game_type_id = #{arg0} AND goods_type_id = #{arg1}
</select>

 

 第三種:

<select id="selectByGameIdAndGoodsTypeId" resultMap="BaseResultMap">
  select
  <include refid="Base_Column_List" />
  from pd_game_goods_type_mid
  where game_type_id = #{gameTypeId} AND goods_type_id = #{goodsTypeId}
</select>

 

剛剛說這樣的會報錯。解決辦法,更改mapper方法

加上@Param注解

selectByGameIdAndGoodsTypeId(@Param("gameTypeId")Long gameTypeId, @Param("goodsTypeId") Long goodsTypeId)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM