-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHQLHelper.java
More file actions
132 lines (115 loc) · 3.1 KB
/
HQLHelper.java
File metadata and controls
132 lines (115 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cn.itcast.oa.util;
import java.util.ArrayList;
import java.util.List;
import cn.itcast.oa.base.DaoSupport;
import cn.itcast.oa.domain.PageBean;
import com.opensymphony.xwork2.ActionContext;
/**
* 辅助拼接hql语句的工具类
* @author Administrator
*/
public class HQLHelper {
private String fromClause; // from字句
private String whereClause = ""; // whereClause字句
private String orderByClause = ""; // order by字句
private List<Object> parameters = new ArrayList<Object>();// 参数列表
/**
* 生成form子句
* @param clazz
* @param alias
* 别名
*/
public HQLHelper(Class clazz, String alias) {
fromClause = "from " + clazz.getSimpleName() + " " + alias;
}
/**
* 拼接where子句
* @param args
* @param condition
*/
public HQLHelper addWhereCondition(String condition, Object... args) {
if (whereClause.length() == 0) { //第一次拼接whereClause为空字符串
whereClause = " where " + condition;
} else {
whereClause += " and " + condition;
}
//处理参数
if (args != null && args.length > 0) {
for (Object arg : args) {
parameters.add(arg);
}
}
return this;
}
public HQLHelper addWhereCondition(boolean append ,String condition, Object... args) {
if (append) {
if (whereClause.length() == 0) {
whereClause = " where " + condition;
} else {
whereClause += " and " + condition;
}
if (args != null && args.length > 0) {
for (Object arg : args) {
parameters.add(arg);
}
}
}
return this;
}
/**
* 拼接order by子句
* @param propertyName
* @param asc
* true表示升序,false表示降序
*/
public HQLHelper addOrderByProperty(String propertyName, boolean asc) {
if (orderByClause.length() == 0) { //第一次拼接orderByClause为空字符串
orderByClause = " order by " + propertyName
+ (asc ? " asc" : " desc");
} else {
orderByClause += ", " + propertyName + (asc ? " asc" : " desc");
}
return this;
}
public HQLHelper addOrderByProperty(boolean append ,String propertyName, boolean asc) {
if (append) {
if (orderByClause.length() == 0) {
orderByClause = " order by " + propertyName
+ (asc ? " asc" : " desc");
} else {
orderByClause += ", " + propertyName + (asc ? " asc" : " desc");
}
}
return this;
}
/**
* 获取查询记录列表的的hql语句
* @return
*/
public String getQueryListHql() {
return fromClause + whereClause + orderByClause;
}
/**
* 获取查询总记录的的hql语句(没有order by语句)
* @return
*/
public String getCountHql() {
return "select count(*) " + fromClause + whereClause;
}
/**
* 获取参数列表
* @return
*/
public List<Object> getParameters() {
return parameters;
}
/**
* 准备PageBean对象到struts2的栈顶
* @param service
* @param pageNumber
*/
public void preparePageBean(DaoSupport<?> service , int pageNumber){
PageBean pageBean = service.getPageBean(pageNumber, this);// pageNumber在BaseAction中定义
ActionContext.getContext().getValueStack().push(pageBean); //放到栈顶,方便拿pageBean中的属性
}
}