最近自己写了一个简单的拼接hql语句的类,开发小项目的时候用起来还是比较爽的。
由于公司限制了文件的上传,所以只能把代码贴在这里做个笔记。
代码
这个是工具类,需要传一个List<QueryParameter> parameterList参数,是QueryParameter实体的集合。
import java.util.List;
import com.students.xl.util.QueryParameter;
/**
* 查詢工具類
* @author louzi
*
*/
public class QueryUtil {
/**
* 將查詢參數轉換為一條完整的hql語句
* @param parameterList 參數列表
* @param hql 基礎的hql語句
* @param params 參數值的集合
* @param entityShortName 查詢實體的別名
* @return
*/
public static String ConvertParamsToHql(List<QueryParameter> parameterList, String hql, List<Object> params,
String entityShortName){
int i = params.size() + 1;
if(parameterList != null && parameterList.size() > 0){
//遍歷參數列表,添加查詢條件
for(QueryParameter param : parameterList){
if(!"".equals(param.getQueryValue()) && param.getQueryValue() != null){
switch(param.getOperator()){
case EQUAL:
hql += " and " + entityShortName + "." + param.getQueryName() + " = ?" + i;
params.add(param.getQueryValue());
i++;
break;
case BIGGER:
hql += " and " + entityShortName + "." + param.getQueryName() + " > ?" + i;
params.add(param.getQueryValue());
i++;
break;
case LESS:
hql += " and " + entityShortName + "." + param.getQueryName() + " < ?" + i;
params.add(param.getQueryValue());
i++;
break;
case NOTBIGGER:
hql += " and " + entityShortName + "." + param.getQueryName() + " <= ?" + i;
params.add(param.getQueryValue());
i++;
break;
case NOTLESS:
hql += " and " + entityShortName + "." + param.getQueryName() + " >= ?" + i;
params.add(param.getQueryValue());
i++;
break;
case NOT:
hql += " and not ( " + entityShortName + "." + param.getQueryName() + " = ?" + i
+ " )";
params.add(param.getQueryValue());
i++;
break;
case NULL:
hql += " and" + entityShortName + "." +param.getQueryName() + " is Null ";
break;
case NOTNULL:
hql += " and " + entityShortName + "." + param.getQueryName() + " is not null ";
break;
case EMPTY:
hql += " and " + entityShortName + "." + param.getQueryName() + " is empty ";
break;
case NOTEMPTY:
hql += " and " + entityShortName + "." + param.getQueryName() + " is not empty ";
break;
case IN:
Object[] objects = (Object[])param.getQueryValue();
if(objects != null & objects.length > 0){
hql += " and " + entityShortName + "." + param.getQueryName() +" in (";
for(int j = 0 ;j < objects.length; j++){
if(j == 0){
hql += "?" + i;
}else{
hql += ",?" + i;
}
params.add(objects[j]);
i++;
}
hql += ") ";
}
break;
case NOTIN:
Object[] objects2 = (Object[])param.getQueryValue();
if(objects2 != null & objects2.length > 0){
hql += " and " + entityShortName + "." + param.getQueryName() +" not in (";
for(int k = 0 ;k < objects2.length; k++){
if(k == 0){
hql += "?" + i;
}else{
hql += ",?" + i;
}
params.add(objects2[k]);
i++;
}
hql += ") ";
}
break;
case BETWEEN:
hql += " and " + entityShortName + "." + param.getQueryName() + " between ?" + i
+ " and ?" + (i+1);
params.add(param.getQueryValue());
params.add(param.getSecondQueryValue());
i += 2;
break;
case LIKE:
hql += " and " + entityShortName + "." +param.getQueryName()
+ " like '%' || ?" + i + " || '%'";
params.add(param.getQueryValue());
i++;
break;
case NOTLIKE:
hql += " and " + entityShortName + "." +param.getQueryName()
+ " not like '%' || ?" + i + " || '%'";
params.add(param.getQueryValue());
i++;
break;
case LLIKE:
hql += " and " + entityShortName + "." +param.getQueryName()
+ " like '%' || ?" + i;
params.add(param.getQueryValue());
i++;
break;
case RLIKE:
hql += " and " + entityShortName + "." +param.getQueryName()
+ " like ?" + i +"|| '%'";
params.add(param.getQueryValue());
i++;
break;
case IN_LIKE:
// 數組遍歷
Object[] objects_like = (Object[]) param.getQueryValue();
if (objects_like != null && objects_like.length > 0) {
hql += " and ( ";
for (int m = 0; m < objects_like.length; m++) {
if (m == 0) {
hql += entityShortName + "." + param.getQueryName() + " like '%' || ?"
+ i + " || '%' ";
} else {
hql += " or " + entityShortName + "." + param.getQueryName()
+ " like '%' || ?" + i + " || '%' ";
}
params.add(objects_like[m]);
i++;
}
hql += " ) ";
}
break;
case IN_RLIKE:
// 數組遍歷
Object[] objects_rlike = (Object[]) param.getQueryValue();
if (objects_rlike != null && objects_rlike.length > 0) {
hql += " and ( ";
for (int m = 0; m < objects_rlike.length; m++) {
if (m == 0) {
hql += entityShortName + "." + param.getQueryName() + " like ?"
+ i + " || '%' ";
} else {
hql += " or " + entityShortName + "." + param.getQueryName()
+ " like ?" + i + " || '%' ";
}
params.add(objects_rlike[m]);
i++;
}
hql += " ) ";
}
break;
case IN_LLIKE:
// 數組遍歷
Object[] objects_llike = (Object[]) param.getQueryValue();
if (objects_llike != null && objects_llike.length > 0) {
hql += " and ( ";
for (int m = 0; m < objects_llike.length; m++) {
if (m == 0) {
hql += entityShortName + "." + param.getQueryName() + " like || '%' ?"
+ i;
} else {
hql += " or " + entityShortName + "." + param.getQueryName()
+ " like || '%' ?" + i;
}
params.add(objects_llike[m]);
i++;
}
hql += " ) ";
}
break;
case IN_NOTLIKE:
// 數組遍歷
Object[] objects_notlike = (Object[]) param.getQueryValue();
if (objects_notlike != null && objects_notlike.length > 0) {
for (int n = 0; n < objects_notlike.length; n++) {
hql += " and " + entityShortName + "." + param.getQueryName()
+ " not like '%' || ?" + i + " || '%' ";
params.add(objects_notlike[n]);
i++;
}
}
break;
}
}
}
}
return hql;
}
}
QueryParameter实体类
import com.students.xl.util.EnumLqOperator;
import lombok.Data;
@Data
public class QueryParameter {
//查詢的條件名
private Object QueryName;
//查詢的條件值
private Object QueryValue;
//查詢操作符
private EnumLqOperator operator;
//用於between的第二個查詢值
private EnumLqOperator SecondQueryValue;
/**
* 構造函數1
* @param QueryName
* @param QueryValue
* @param operator
*/
public QueryParameter (Object QueryName,EnumLqOperator operator,Object QueryValue){
this.QueryName = QueryName;
this.QueryValue = QueryValue;
this.operator = operator;
}
/**
* 構造函數2
* @param QueryName
* @param QueryValue
* @param operator
* @param SecondQueryValue
*/
public QueryParameter (Object QueryName,EnumLqOperator operator,Object QueryValue,EnumLqOperator SecondQueryValue){
this.QueryName = QueryName;
this.QueryValue = QueryValue;
this.operator = operator;
this.SecondQueryValue = SecondQueryValue;
}
}
EnumLqOperator类 (Lq查詢操作符)
public enum EnumLqOperator {
/**
* 如 and(字段 like %parameterValue[0]% or 字段 like
* %parameterValue[1]% ... or 字段 like %parameterValue[n]%)
* INRLIKE,INLLIKE,IN_NOTLIKE,依此類推
*/
EQUAL("="), BIGGER(">"), LESS("<"), NOTBIGGER("<="), NOTLESS(">="), NOT("not"), NULL("null"), NOTNULL("not null"), EMPTY(
"empty"), NOTEMPTY("not empty"), IN("in"), NOTIN("not in"), BETWEEN("between"), LIKE("like"),NOTLIKE("not like"), LLIKE("llike"), RLIKE(
"rlike"), IN_LIKE("in like"), IN_RLIKE("in rlike"), IN_LLIKE("in llike"),IN_NOTLIKE("in not like");
private EnumLqOperator(String value) {
this.setOperatorString(value);
}
// 操作符
private String operatorString;
public String getOperatorString() {
return operatorString;
}
public void setOperatorString(String operatorString) {
this.operatorString = operatorString;
}
}
具体使用方法
- 方法
public T queryOne(List<QueryParameter> params) {
T t = null;
String hql = "from " + entityClass.getSimpleName() + " c where 1=1 ";
List<Object> list = new ArrayList<Object>();
//拼接HQL
hql = QueryUtil.ConvertParamsToHql(params, hql, list, "c");
Query query = session.createQuery(hql);
if(lists !=null && lists.size() > 0){
for(int i = 0; i < lists.size(); i++){
query.setParameter((i+1)+"", lists.get(i));
}
}
List<T> result = query.list(); //查詢的結果集合
if(result !=null && result.size() > 0){
t = result.get(0);
}
return t;
}
- 参数
String password = "123456";
String no = "001";
List<QueryParameter> paramList = new ArrayList<QueryParameter>();
paramList.add(new QueryParameter("stu_pass", EnumLqOperator.EQUAL, password));
paramList.add(new QueryParameter("sno", EnumLqOperator.EQUAL, no));
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。