2121import static zuo .biao .apijson .JSONObject .KEY_HAVING ;
2222import static zuo .biao .apijson .JSONObject .KEY_ID ;
2323import static zuo .biao .apijson .JSONObject .KEY_ID_IN ;
24+ import static zuo .biao .apijson .JSONObject .KEY_USER_ID ;
25+ import static zuo .biao .apijson .JSONObject .KEY_USER_ID_IN ;
2426import static zuo .biao .apijson .JSONObject .KEY_ORDER ;
2527import static zuo .biao .apijson .JSONObject .KEY_ROLE ;
2628import static zuo .biao .apijson .JSONObject .KEY_SCHEMA ;
3941import java .util .LinkedHashMap ;
4042import java .util .List ;
4143import java .util .Map ;
44+ import java .util .Map .Entry ;
4245import java .util .Set ;
4346
4447import com .alibaba .fastjson .JSON ;
@@ -511,16 +514,35 @@ public String getWhereString() throws Exception {
511514 * @throws Exception
512515 */
513516 public static String getWhereString (RequestMethod method , Map <String , Object > where , boolean verifyName ) throws Exception {
514- Set <String > set = where == null ? null : where . keySet ();
515- if (set == null || set . isEmpty () ) {
517+ Map <String , Object > where2 = where == null || where . isEmpty () ? null : new LinkedHashMap < String , Object > ();
518+ if (where2 == null ) {
516519 return "" ;
517520 }
518- String whereString = "" ;
521+
522+ //强制排序,把id,id{},userId,userId{}放最前面,保证安全、优化性能
523+ Object id = where .remove (KEY_ID );
524+ Object idIn = where .remove (KEY_ID_IN );
525+ Object userId = where .remove (KEY_USER_ID );
526+ Object userIdIn = where .remove (KEY_USER_ID_IN );
527+
528+ where2 .put (KEY_ID , id );
529+ where2 .put (KEY_ID_IN , idIn );
530+ where2 .put (KEY_USER_ID , userId );
531+ where2 .put (KEY_USER_ID_IN , userIdIn );
532+ where2 .putAll (where );
533+
534+
535+ Set <Entry <String , Object >> set = where2 .entrySet ();
536+
519537 boolean isFirst = true ;
520-
521538 String condition ;
522- for (String key : set ) {
523- condition = getWhereItem (key , where .get (key ), method , verifyName );
539+ String whereString = "" ;
540+
541+ for (Entry <String , Object > entry : set ) {
542+ if (entry == null ) {
543+ continue ;
544+ }
545+ condition = getWhereItem (entry .getKey (), entry .getValue (), method , verifyName );
524546
525547 if (StringUtil .isEmpty (condition , true )) {//避免SQL条件连接错误
526548 continue ;
@@ -530,6 +552,12 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
530552
531553 isFirst = false ;
532554 }
555+
556+ //还原where,后续可能用到
557+ where .put (KEY_ID , id );
558+ where .put (KEY_ID_IN , idIn );
559+ where .put (KEY_USER_ID , userId );
560+ where .put (KEY_USER_ID_IN , userIdIn );
533561
534562 String s = whereString .isEmpty () ? "" : " WHERE " + whereString ;
535563
@@ -1041,8 +1069,7 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
10411069 }
10421070 AbstractSQLConfig config = callback .getSQLConfig (method , table );
10431071
1044- boolean isEmpty = request .isEmpty ();
1045- if (isEmpty ) { // User:{} 这种空内容在查询时也有效
1072+ if (request .isEmpty ()) { // User:{} 这种空内容在查询时也有效
10461073 return config ; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
10471074 }
10481075
0 commit comments