mybatis传参异常: Parameter 'XXXXXX' not found. Available parameters are [0, 1, param1, param2]解决方案
传入多个值时,mybatis会自动将这些值转成类似map形式的值,以0,1,2…的序列为keyName。
这里我们需要先简单了解一下mybatis的传值机制。Mybatis现在可以使用的parameterType有基本类型和Java负责类型。
基本数据类型:包含int,String,Date等。基本数据类型作为传参,只能传入一个。通过#{参数名}获取。
复杂数据类型:包含Java实体类、Map。通过#{属性名}或#{map的keyName}获取。
四种方式:
- 顺序传递参数
- 注解@Param传递参数
- 使用Map集合传递参数
- 使用JavaBean实体类传递参数
dao代码编写方式:
int saveImages(@Param(value="groupId") Long groupId,
@Param(value="nType") int nType,
@Param(value="imagesUrls") Set<String> imagesUrls);
sql接收参数:
<insert id="saveImages" >
insert into glsa_image (
type,
image_group_id,
url,
valid,
gmt_create)
values
<foreach collection="imagesUrls" item="url" separator=",">
(#{nType,jdbcType=INTEGER},
#{groupId,jdbcType=BIGINT},
#{url,jdbcType=VARCHAR},
1,
now())
</foreach>
</insert>