Mybatis中未使用@Param注解导致错误,如何有效解决?

2026-04-30 07:462阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计572个文字,预计阅读时间需要3分钟。

Mybatis中未使用@Param注解导致错误,如何有效解决?

我的IDEA版本是2017.3.4,低版本可能不支持此配置。IDEA高版本会自动补充知识:解决Mybatis传递多个参数的问题及MyBatis报错Parameter '270' not found。可用参数有[arg1, arg0, param1]。关于使用Myba...

Mybatis中未使用@Param注解导致错误,如何有效解决?

我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会

补充知识: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)

以上这篇mybatis不加@Parm注解报错的解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

标签:解决方案

本文共计572个文字,预计阅读时间需要3分钟。

Mybatis中未使用@Param注解导致错误,如何有效解决?

我的IDEA版本是2017.3.4,低版本可能不支持此配置。IDEA高版本会自动补充知识:解决Mybatis传递多个参数的问题及MyBatis报错Parameter '270' not found。可用参数有[arg1, arg0, param1]。关于使用Myba...

Mybatis中未使用@Param注解导致错误,如何有效解决?

我的idea版本2017.3.4,低版本貌似不会加上这个配置,idea高版本会

补充知识: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)

以上这篇mybatis不加@Parm注解报错的解决方案就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

标签:解决方案