1. 程式人生 > >Mybatis 中遍歷map 引數中的 list 和 array 屬性

Mybatis 中遍歷map 引數中的 list 和 array 屬性

問題

在專案有中遇到批量刪除操作時,需要根據兩個屬性去刪除資料,其中一個是型別:type, 另一個是ids:陣列形式的id陣列。由於在官方文件中只是簡單的介紹foreach的用法,套用之後進行批量刪除操作:提示遍歷map中的array 屬性是無法獲取值。

解決方案

通過重新閱讀mybatis 3 官方文件, 查閱CSDN iteye等網站資料。

程式碼

controller層

/**
 *[根據附件的型別 type 和 物件ids批量刪除附件資訊]
 */
@RequestMapping("/deleteProjectInterimByIds.do")
public void
deleteProjectInterimByIds(HttpServletResponse response, @RequestParam(value = "ids", required=true)Long[] ids, @RequestParam(value="type",required=true)Integer type) { Map<String, Object> paraMap = new HashMap<String, Object>(); paraMap.put("type", type); paraMap.put("ids"
, ids); int i = nterimAttService.deleteAttachmentByObjIdsAndType(paraMap); System.out.println(i);

dao層

@Override
public int deleteAttachmentByObjIdsAndType(Map<String, Object> paraMap) {
    return this.getSqlSession().delete(NAME_SPACE +"batchDeleteAttByIds", paraMap);
    }

mapper.xml

<–1.取map中的key 為type的值
2.取map中的key 為ids 的值;Ids 在map中是以陣列的形式存在 的,直接標記取出就可以,採用#{des}的方式會出現錯誤;–>


<delete id="batchDeleteAttByIds" parameterType="map">
    delete from project_attachments
    where attachment_type = #{type} and object_id in 
    <foreach collection="ids" open="(" close=")" separator="," item="id">
        #{id}
    </foreach> 
  </delete>