当ids数量成千上万的时候,如果还要使用in方法进行查询,ids可以先分组得到idList再循环idList查询;
code:
/** * 将id串分隔成若干份 * @param vids * @param len * @return */ public static List<String> splitSqlParamsToList(String ids, int len){ List<String> vidList = new ArrayList<>(); StringBuffer buffer = new StringBuffer(); String[] split = ids.split(","); int length = split.length; // 分隔vids串,单个大小len长度 String value = ""; for (int i = 0; i < length; i++) { value = split[i]; if(isNullOrEmpty(value)){ continue; } // (i + 1) % len != 0 if(isNullOrEmpty(buffer.toString())){ buffer.append(value); }else{ buffer.append("," + value); } if(buffer.toString().split(",").length % len != 0){ if(i != (length - 1)){ // 不是最后一位 continue; } } vidList.add(buffer.toString()); buffer = new StringBuffer(); } return vidList; }
test:
String vids = "1,2,3,4,5,6,7,8,,22,,11,,,10,12"; List<String> list = splitSqlParamsToList(vids, 6); for (int i = 0; i < list.size(); i++) { System.out.println("第" + i + "组:" + list.get(i)); }
result:
第0组:1,2,3,4,5,6 第1组:7,8,22,11,10,12
来源:https://www.cnblogs.com/huakaiyoushi/p/12550314.html