oracle select in超過1000條報錯解決方法


本博客介紹oracle select in超過1000條數據的解決方法,java框架是采用mybatis的,這可以說是一種比較常見的錯誤:select * from A where id in(...),oracle官方函數做了限定,in里的參數只能1000個,所以超過1000個參數就會報錯,解決方法是將集合分為每個集合1000的小集合,然后用or拼起來select * from A where id in(1,2,...,1000) or id in (1001,1002,2000)...,好的,根據這個sql,下面介紹一下orm空間為mybatis的項目里怎么解決

java代碼只要獲取參數進行集合拆分就可以:

舉個例子,下面是一種方法

List<String> values = new ArrayList<String>();
String[] configSeqArray = StringUtils.split(configSeq,',');
for (String str : configSeqArray) {
		values.add(str);
}
List<Collection<String>> configSeqs = CollectionUtil.splitCollection(values, 1000);

復制公司同事寫的集合拆分的方法

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class CollectionUtils {
	public static List<Collection<String>> splitCollection(Collection<String>values , int size) {
		List<Collection<String>> result = new ArrayList<Collection<String>>();
		if(values.size() <= size ){
			result.add(values);
		}else{
				int count =0;
				Collection<String> subCollection= null;
				for(String s:c){
					if(subCollection == null){
						subColletion = new ArrayList<String>();
						result.add(subColletion);
					}
					subCollection.add(s);
					count++;
					if(count == size){
						count =0;
						subCollectiion = null;
					}
				}
		}
	}
}

Mybatis的代碼:

select *  from t_config_related
		<where> 
			<if test="configSeqs != null and configSeqs.size()>0 and configSeqs.get(0).size()>0">
			<foreach collection="configSeqs" item="seqs" open="seq in"
								 separator="or seq in">
				<foreach collection="seqs" item="seq" open="(" close=")" separator=",">
						#{seq}
				</foreach>
			</foreach>
			</if>
		</where>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM