public static String whererIn(String fieldName,List<String> listGroupIds) {
String sqlWhere=" ";
StringBuffer sb = new StringBuffer();
if(!listGroupIds.equals("")&& listGroupIds!=null){
for(int i=0;i<listGroupIds.size();i++){
sb.append("'" + listGroupIds.get(i) + "'");
if(i != listGroupIds.size() -1){
sb.append(",");
}
}
sqlWhere+=" and "+fieldName+" in ("+sb.toString()+")";
}
return sqlWhere;
}
2:利用For Each循环的写法:
public static String whererIn2(String fieldName, List<String> listGroupIds){
String whatYouNeedGroupId = "";
for(String id : listGroupIds){
whatYouNeedGroupId += "'" + id + "'" + ",";
}
whatYouNeedGroupId = whatYouNeedGroupId.substring(0, whatYouNeedGroupId.length()-1);
String sqlWhere=" and "+fieldName+" in ("+whatYouNeedGroupId.toString()+")";
return sqlWhere;
}
最后写一个main函数测试:
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("cyz");
list.add("zwb");
System.out.println(whererIn("s.groupId",list));
System.out.println(whererIn2("s.groupId",list));