Mybatis传入多类型参数
接口定义如下
public List<User> selectUsersByIds(List<Integer> ids, String userName)
Mybatis的xml文件如下
<select id="selectUsersByIds" resultType="User">
select * from user where id in
<foreach collection="param1" item="item" open="(" separator="," close=")">
#{item}
</foreach>
and name = #{param2}
</select>
mybatis会自动将多个不同类型的参数改成param1,param2…
当然也可以使用Mybatis注解明确指明传入参数名称
接口定义如下
public List<User> selectUsersByIds(@Param("ids")List<Integer> ids, @Param("user")User user)
Mybatis的xml文件如下
<select id="selectUserInIDs" resultType="User">
select * from user
<if test="null != ids">
where id in
<foreach collection="ids" item="item" open="(" separator="," close=")">
#{item}
</foreach>
and name = #{user.name}
</if>
</select>