Jfinal適用於條件查詢的動態SQL語句生成工具
阿新 • • 發佈:2019-01-20
use null tps 過濾 where value col acea ret
條件查詢是可能有為空字段,拼接SQL語句時候要屏蔽掉這些字段.
package cn.pangpython.utils; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; /** * @author pangPython * sql工具類 */ public class SQLUtils { //根據請求參數動態生成sql,過濾空值 //適用於條件查詢 public static String DynamicSQL(Map<String,Object> map){ String sql= " where "; //遍歷map把其中value為空的刪除 Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry mapentry = (Entry) it.next(); if(mapentry.getValue()!=null){ sql = sql + mapentry.getKey() + " = " + mapentry.getValue()+" and "; } }if(sql.trim().endsWith("and")){ sql = sql.substring(0, sql.lastIndexOf("and")); } if(sql.equals(" where ")){ return ""; } //因為用到表的別名,需要替換 sql = sql.replaceAll("user", "u"); return sql; } }
參考原文:https://blog.csdn.net/u012995856/article/details/52968684
Jfinal適用於條件查詢的動態SQL語句生成工具