/**
sql截取字符串的函數。* 工具-字符串-轉換Sql查詢IN中使用的格式
* 效果:a,b==>'a','b'
java字符串替換、* @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;kk*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);jgroupCount*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();
}
文章來源: blog.51cto.com,作者:沫沫金,版權歸原作者所有,如需轉載,請聯系作者。
原文鏈接:https://blog.51cto.com/zl0828/2507761
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态