問題描述:有時候進行批量操做的時候,須要攜帶條件等信息,這個時候循環的部分就須要改變一下寫法,不然會報異常,這裏直接列出解決方案數組
dao接口:app
// xml中接收參數的順序要與這裏保持一致 User selectUser(String classId, String userId);
mapper.xml:ide
<select id="selectUser" resultMap="BaseResultMap"> select * from user where class_id = #{0} and user_id = #{1} </select>
dao接口:code
// xml中接收參數的順序要與這裏保持一致 TestInfo selectAll(String classId, String[] userIds);
mapper.xml,注意foreach中collection的值"userIds"要與Dao中第二個參數的名稱一致,且順序也一致:xml
<select id="selectAll" resultMap="BaseResultMap"> select * from test_info where class_id = #{classId,jdbcType=VARCHAR} and user_id in <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>
如圖:blog
2.2.1 service接口:接口
... public TestInfo selectAll(String classId, String[] userIds) { Map<String, Object> map = new HashMap<String, Object>(); map.put("classId", classId); map.put("userIds", userIds); // 調用dao userInfoDao.selectAll(map); ... }
2.2.2 dao接口:get
TestInfo selectAll(Map<String, Object> map);
2.2.3 mapper.xml,其中的參數名稱要與map中的key一致:it
<select id="selectAll" resultMap="BaseResultMap"> select * from test_info where class_id = #{classId,jdbcType=VARCHAR} and user_id in <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>