标签:rto none i++ spl join split bin turn mat
/**
* 工具-字符串-转换Sql查询IN中使用的格式
* 效果:a,b==>‘a‘,‘b‘
* @param str
* @return
*/
public String strToDbin(String str){
return String.format("‘%s‘", StringUtils.join(str.split(","),"‘,‘"));
}
/**
* 工具-字符串-转换为Sql查询分组IN格式
* 解决:Sql中in超过1000个报错问题
* @param field 列名,例如:ID
* @param strIns 待拆分的字符串 a,b,c
* @return field in(‘a‘) and field in(‘b‘) and field in(‘c‘)
*/
public String strToSqlGroupIn(String field,String strIns) {
int groupNum = 1;
String groupInArr = new String();
StringBuffer buffer=new StringBuffer();
if(StringUtils.isNoneBlank(strIns)){
String[] array=strIns.split(",");
//数组总长度
int len=array.length;
//分组数
int groupCount=len/groupNum;
for(int k=0;k<groupCount;k++){
groupInArr=new String();
for(int i=(k*groupNum);i<(k*groupNum+groupNum);i++){
if(i>k*groupNum){
groupInArr+=",";
}
groupInArr+=array[i].trim();
}
if(k>0){
buffer.append(" and ");
}
buffer.append(String.format(" %s in(%s)",field,strToDbin(groupInArr)));
}
if(len%groupNum != 0){
//未整除
groupInArr=new String();
//处理最后一组数据
for(int j=(groupCount*groupNum);j<len;j++){
if(j>groupCount*groupNum){
groupInArr+=",";
}
groupInArr+=array[j].trim();
}
if(buffer.length()>0){
buffer.append(" and ");
}
buffer.append(String.format(" %s in(%s)",field,strToDbin(groupInArr)));
}
}
return buffer.toString();
}
标签:rto none i++ spl join split bin turn mat
原文地址:https://blog.51cto.com/zl0828/2507761