MyBatis提供了以下几种传入List的方法:
public interface UserMapper {
List<User> getUserList(List<String> ids);
}
<select id="getUserList" parameterType="java.util.List">
SELECT * FROM users WHERE id IN
<foreach item="item" collection="list" separator="," open="(" close=")">
#{item}
</foreach>
</select>
public interface UserMapper {
List<User> getUserList(String[] ids);
}
<select id="getUserList" parameterType="java.lang.String[]">
SELECT * FROM users WHERE id IN
<foreach item="item" collection="array" separator="," open="(" close=")">
#{item}
</foreach>
</select>
public class IdListWrapper {
private List<String> ids;
// getter and setter
}
public interface UserMapper {
List<User> getUserList(IdListWrapper idListWrapper);
}
<select id="getUserList" parameterType="com.example.IdListWrapper">
SELECT * FROM users WHERE id IN
<foreach item="item" collection="ids" separator="," open="(" close=")">
#{item}
</foreach>
</select>
这些方法都可以实现传入List的功能,根据具体的需求选择适合的方法即可。