接口中参数有多个的情况下,一般有两种方法进行配置。第一种是封装到一个类中,第二种是直接在接口形式参数前面写注解。
这里说下第二种容易出错的地方。
拼接字符串的时候常常需要用到where,foreach等语句
然而:
①接口中有方法:
List<User> selectByIdAndUsername(@Param("id1") int id,@Param("username1") String username);
<select id="selectByIdAndUsername" resultType="user">
select * from user
<where>
<if test="id1!=0">
and id=#{id1}
</if>
<if test="username1!=null and username1!=''">
and username like '%${username1}%'
</if>
</where>
</select>
这里我特意将param里面的参数名称后面加了个1以作区别。
②接口中有方法:
List<User> selectByIds(@Param("ids1") List<Integer> ids );
<select id="selectByIds" resultType="user">
select * from user where id in
<foreach collection="ids1" open="(" close=")" separator="," item="item">
#{item}
</foreach>
</select>
此处特别要注意,即使是list,也算是多个参数的,一定要写注解,或者封装到其他类底下去也可以。通常不用写注解或者不用封装的情况,只有一种,那就是只传一个参数!