java hql多条件查询_Hibernate结合JPA编写通用泛型多条件查询
項(xiàng)目中使用Hibernate和JPA對(duì)數(shù)據(jù)庫(kù)對(duì)象進(jìn)行實(shí)例化,但是生成的方法不支持多條件查詢。而如果針對(duì)每一個(gè)數(shù)據(jù)庫(kù)對(duì)象進(jìn)行多條件查詢編碼,則會(huì)變得很麻煩,而且一旦以后發(fā)生表結(jié)構(gòu)發(fā)生變化,這些方法可能還需要進(jìn)行重新編碼。所以考慮編寫一個(gè)方法可以對(duì)數(shù)據(jù)庫(kù)對(duì)象進(jìn)行多條件查詢,并返回泛型對(duì)象,這樣就可以方便使用。具體實(shí)現(xiàn)思路如下:
第一步:編寫數(shù)據(jù)庫(kù)查詢參數(shù)對(duì)象,此部分包含兩個(gè),一個(gè)是查詢實(shí)體名稱(QueryCondition.java),一個(gè)是數(shù)據(jù)庫(kù)查詢條件對(duì)象(QueryParameter.java)。
數(shù)據(jù)庫(kù)查詢條件對(duì)象(QueryParameter.java)包含三個(gè)參數(shù),分別為參數(shù)名、參數(shù)值、查詢條件表達(dá)式
package?com.imstudio.jpa;
public?class?QueryParameter?{
public?enum?QueryOperateType?{
Equal,?CharIn
}
public?String?ParameterName;
public?Object?ParameterValue;
public?QueryOperateType?ParameterType;
public?QueryParameter()?{
}
public?QueryParameter(String?parameterName,?Object?parameterValue,
QueryOperateType?parameterType)?{
this.ParameterName?=?parameterName;
this.ParameterValue?=?parameterValue;
this.ParameterType?=?parameterType;
}
public?String?getParameterName()?{
return?ParameterName;
}
public?QueryOperateType?getParameterType()?{
return?ParameterType;
}
public?Object?getParameterValue()?{
return?ParameterValue;
}
public?void?setParameterName(String?parameterName)?{
ParameterName?=?parameterName;
}
public?void?setParameterType(QueryOperateType?parameterType)?{
this.ParameterType?=?parameterType;
}
public?void?setParameterValue(Object?parameterValue)?{
ParameterValue?=?parameterValue;
}
}
查詢對(duì)象實(shí)體(QueryCondition.java)包含查詢實(shí)體名稱以及查詢參數(shù)對(duì)象
package?com.imstudio.jpa;
import?java.util.ArrayList;
import?java.util.List;
public?class?QueryCondition?{
public?String?ModelName;
public?List?Parameters?=?new?ArrayList();
public?QueryCondition()?{
}
public?QueryCondition(String?modelName)?{
this.ModelName?=?modelName;
}
public?QueryCondition(String?modelName,?List?parameters)?{
this.ModelName?=?modelName;
this.Parameters?=?parameters;
}
public?void?add(QueryParameter?queryParameter)?{
this.Parameters.add(queryParameter);
}
public?String?getModelName()?{
return?ModelName;
}
public?List?getParameters()?{
return?Parameters;
}
public?void?setModelName(String?modelName)?{
ModelName?=?modelName;
}
public?void?setParameters(List?parameters)?{
Parameters?=?parameters;
}
}
在完成上述兩個(gè)實(shí)體對(duì)象之后就可以具體查詢方法的編寫了,在查詢中使用到一個(gè)變量querySymbols,下述編碼是從配置文件web.xml中獲取,這里主要是為了在使用不同數(shù)據(jù)庫(kù)的時(shí)候查詢關(guān)鍵字標(biāo)示符的修改。同時(shí)為了增加查詢方法的通用性,查詢返回?cái)?shù)據(jù)這里定義為泛型。
package?com.imstudio.jpa;import?java.util.ArrayList;
import?java.util.List;
import?java.util.logging.Level;
import?javax.persistence.EntityManager;
import?javax.persistence.Query;
import?org.apache.struts2.ServletActionContext;
import?com.imstudio.jpa.QueryParameter.QueryOperateType;
public?class?QueryDataAction?{
public?String?errorCode;
public?QueryDataAction()?{
}
public?QueryDataAction(String?errorCode)?{
this.errorCode?=?errorCode;
}
public?String?getErrorCode()?{
return?errorCode;
};
@SuppressWarnings("unchecked")
public??List?queryByPropertys(QueryCondition?queryCondition)?{
String?querySymbols?=?ServletActionContext.getServletContext()
.getInitParameter("QuerySymbols");
StringBuffer?sqlBuffer?=?new?StringBuffer();
sqlBuffer.append("select?model?\n");
sqlBuffer.append("from?"?+?queryCondition.ModelName?+?"?as?model?\n");
boolean?first?=?true;
for?(int?pi?=?0;?pi?
if?(queryCondition.Parameters.get(pi).getParameterName()?!=?null)?{????????????????????????????????if?(first)?{
sqlBuffer.append("where?");
first?=?false;
}?else?{
sqlBuffer.append("and?");
}
if?(queryCondition.Parameters.get(pi).getParameterType()?==?QueryOperateType.Equal)?{
sqlBuffer.append("model."
+?queryCondition.Parameters.get(pi)
.getParameterName()????????????????????????????+?"?=?"
+?querySymbols????????????????????????????+?queryCondition.Parameters.get(pi)
.getParameterName()?+?"?\n");
}?else?if?(queryCondition.Parameters.get(pi).getParameterType()?==?QueryOperateType.CharIn)?{
sqlBuffer.append("InStr(model."
+?queryCondition.Parameters.get(pi)
.getParameterName()????????????????????????????+?"?,?"
+?querySymbols????????????????????????????+?queryCondition.Parameters.get(pi)
.getParameterName()?+?"?)?>?0?\n");
}
}
}
List?list?=?new?ArrayList();
try?{
EntityManagerHelper.log(sqlBuffer.toString(),?Level.INFO,?null);
EntityManager?emEntityManager?=?EntityManagerHelper
.getEntityManager();
Query?queryObject?=?emEntityManager.createQuery(sqlBuffer
.toString());
for?(int?li?=?0;?li?
queryObject.setParameter(queryCondition.Parameters.get(li)
.getParameterName(),?queryCondition.Parameters.get(li)
.getParameterValue());
}
list?=?queryObject.getResultList();
emEntityManager.close();
}?catch?(RuntimeException?re)?{
errorCode?+=?"CM000006";
EntityManagerHelper.log("queryByPropertys?error",?Level.SEVERE,?re);
throw?re;
}????????return?list;
}
public?void?setErrorCode(String?errorCode)?{
this.errorCode?=?errorCode;
}
}
總結(jié)
以上是生活随笔為你收集整理的java hql多条件查询_Hibernate结合JPA编写通用泛型多条件查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 明星片酬1000万要交多少税 到手只有5
- 下一篇: 什么是股票投资 概述目的和特点介绍