1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 处理Oracle SQL语句IN子句中WHERE id IN (1, 2, ..., 1000, 1001),
* 如果子句中超过1000项就会报错。
* 这主要是Oracle考虑性能问题做的限制。
* 如果要解决此问题,可以用 WHERE id IN (1, 2, ..., 1000) OR id IN (1001, ...)
* @author hoojo
* @createDate 2012-8-31 下午02:36:03
* @param ids IN语句中的集合对象
* @param count IN语句中出现的条件个数
* @param field IN语句对应的数据库查询字段
* @return field IN (...) OR field IN (...)字符串
*/
private String getOracleSQLIn(List<?> ids, int count, String field) {
count = Math.min(count, 1000);
int len = ids.size();
int size = len % count;
if (size == 0) {
size = len / count;
} else {
size = (len / count) + 1;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < size; i++) {
int fromIndex = i * count;
int toIndex = Math.min(fromIndex + count, len);
String productId = StringUtils.defaultIfEmpty(StringUtils.join(ids.subList(fromIndex, toIndex), "','"), "");
if (i != 0) {
builder.append(" or ");
}
builder.append(field).append(" in ('").append(productId).append("')");
}
return StringUtils.defaultIfEmpty(builder.toString(), field + " in ('')");
}
Post
Cancel
Java中Oracle WHERE IN查询的项超过1000条的解决方案
This post is licensed under
CC BY 4.0
by the author.