? ? ? 近期在研究jqgrid,在里面涉及到了单字段查询。里面用到的查询方式有很多,比如等于、不等于、包含等等,都需要在后台实现。个人感觉这个后台的实现能够用到很多地方,所以在此保留,以供后用。请勿见笑。
?
public static String getOperation(String sField, String sOper,String sValue){ if(sOper==null || sOper.trim().length() ==0) return ""; String result = ""; if(sOper.trim().equals("eq")) //等于 result = sField + "='" + sValue +"' "; else if(sOper.trim().equals("ne")) //不等于 result = sField + " != '"+ sValue+"' "; else if(sOper.trim().equals("lt")) //小于 result = sField + " < '"+ sValue+"' "; else if(sOper.trim().equals("le")) //小于等于 result = sField + " <= '"+ sValue+"' "; else if(sOper.trim().equals("gt")) //大于 result = sField + " > '"+ sValue+"' "; else if(sOper.trim().equals("ge")) //大于等于 result = sField + " >= '"+ sValue+"' "; else if(sOper.trim().equals("bw")) //以...开始 result = sField + " LIKE '"+ sValue+"%' "; else if(sOper.trim().equals("bn")) //不以...开始 result = sField + " NOT LIKE '"+ sValue+"%' "; else if(sOper.trim().equals("cn")){ //包括 String[] sv = sValue.split(","); String svString = ""; for(int i = 0 ;i<sv.length; i ++){ svString += "%"+sv[i]+"%"; } result = sField + " LIKE '" +svString+"'"; } else if(sOper.trim().equals("nc")){//不包含 String[] sv = sValue.split(","); String svString = ""; for(int i = 0 ;i<sv.length; i ++){ svString += "%"+sv[i]+"%"; } result = sField + " NOT LIKE '" +svString + "'"; } else if(sOper.trim().equals("nu")) //is null result = sField + " IS NULL "; else if(sOper.trim().equals("nn")) //IS NOT NULL result = sField + " IS NOT NULL "; else if(sOper.trim().equals("ew")) // 以... 结束 result = sField + " LIKE '%"+ sValue+"' "; else if(sOper.trim().equals("en")) result = sField + " NOT LIKE '%" +sValue+"' "; return result; }?