首先聲明我不是很喜歡用foreach,所以我的代碼中很少出現foreach。不廢話了,上代碼:
in的用法:
我的id是Long類型的
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) { Map<String, Object> map = new HashMap<String, Object>(); if(ids.size()!=0) { StringBuilder sbd = new StringBuilder(); for(Long cateIds:ids){ sbd.append(cateIds+","); } String idStr = sbd.toString(); idStr = idStr.substring(0,idStr.length()-1); map.put("ids", idStr); }
實體類.xml
select * from xxx where
<if test="ids != null"> FIND_IN_SET(id,#{ids}) </if>
not in的用法
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) { Map<String, Object> map = new HashMap<String, Object>(); if(ids.size()!=0) { StringBuilder sbd = new StringBuilder(); for(Long cateIds:ids){ sbd.append(cateIds+","); } String idStr = sbd.toString(); idStr = idStr.substring(0,idStr.length()-1); map.put("notids", idStr); }
實體類.xml
select * from xxx where
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids}) </if>