这个原因在于Oracle语法限制,in内数量必须小于1000,故采取满1000分割用OR连接,下面是解决方案:
/** * Example: List sqhlist=[''aa''"bb""cc""dd""ee""ff""gg"'] ; * Test.getSqlStrByList(sqhList,3,"SHENQINGH")= "SHENQING IN * ('aa','bb','cc') OR SHENQINGH IN ('dd','ee','ff') OR SHENQINGH IN ('g * * 把超过1000的in条件集合拆分成数量splitNum的多组sql的in 集合。 * * @param sqhList * in条件的List * @param splitNum * 拆分的间隔数目,例如: 1000 * @param columnName * SQL中引用的字段名例如: Z.SHENQINGH * @return **/ private static String getSqlStrByList(List sqhList, int splitNum, String columnName) { if (splitNum > 1000) // 因为数据库的列表sql限制,不能超过1000. return null; StringBuffer ids = new StringBuffer(""); if (sqhList != null) { ids.append(" ").append(columnName).append(" IN ( "); for (int i = 0; i < sqhList.size(); i++) { ids.append("'").append(sqhList.get(i) + "'"); if ((i + 1) % splitNum == 0 && (i + 1) < sqhList.size()) { ids.deleteCharAt(ids.length() - 1); ids.append(" ) OR ").append(columnName).append(" IN ("); } } ids.deleteCharAt(ids.length() - 1); ids.append(" )"); } return ids.toString(); } public static void main(String[] args) { List list = new ArrayList(); int id = 2; for(int i=0;i<1020;i++){ list.add("'"+id+"'"); } String ss = getSqlStrByList(list, 1000,"test"); System.out.println(ss); }
时间: 2024-11-12 03:14:48