程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> server2005-在Struts中這段查詢怎麼解釋

server2005-在Struts中這段查詢怎麼解釋

編輯:編程綜合問答
在Struts中這段查詢怎麼解釋

try {
String queryString = "select count(model) from Client model";
if (map != null) {
boolean bFirst = true;
Object[] obj = null;
for (Object o : map.keySet()) {
if (bFirst) {
queryString += " where ";
bFirst = false;
} else {
queryString += " and ";
}
obj = (Object[]) map.get(o);
if ("1".equals(obj[1].toString())) {
// 模糊查詢
queryString += " model." + o + " like :_" + o;
} else {
// 准確查詢
queryString += " model." + o + "=:_" + o;
}
}
}
Query query = getEntityManager().createQuery(queryString);

最佳回答:


 try {
String queryString = "select count(model) from Client model";
if (map != null) {//map中是條件
boolean bFirst = true;
Object[] obj = null;
for (Object o : map.keySet()) {
if (bFirst) {//如果是第一個條件,則需要拼接where,如果不是第一個,則是拼接and
queryString += " where ";
bFirst = false;
} else {
queryString += " and ";
}
obj = (Object[]) map.get(o);
if ("1".equals(obj[1].toString())) {//如果是模糊查詢是like,精確查詢時等號
// 模糊查詢
queryString += " model." + o + " like :_" + o;
} else {
// 准確查詢
queryString += " model." + o + "=:_" + o;
}
}
}

也就是比如
map.put("field1",new String[]{"a","1"})
map.put("field2",new String[]{"b","0"})
則,原來SQL是
select count(model) from Client model
在field1的時候需要拼接where
select count(model) from Client model where
然後是1,則是模糊查詢,拼接成
select count(model) from Client model where  model.field1 like:_field1
到field2的時候,不是第一個條件,拼接and
select count(model) from Client model where  model.field1 like:_field1 and
然後是0,精確查詢
select count(model) from Client model where  model.field1 like:_field1 and model.field2 =:_field2
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved