在使用MyBatis操作list in時,可以通過以下技巧實現:
<select id="selectByIds" parameterType="java.util.List" resultType="com.example.User">
SELECT * FROM user WHERE id IN
<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</select>
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("ids", listOfIds);
userMapper.selectByIds(paramMap);
<select id="selectByIds" parameterType="java.util.Map" resultType="com.example.User">
SELECT * FROM user WHERE id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>
通過以上技巧,可以方便地在MyBatis中操作list in,實現根據list中的多個id查詢對應的數據。