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的功能,根據具體的需求選擇適合的方法即可。