提交 | 用户 | age
|
826b66
|
1 |
package com.hx.mybatisTool; |
C |
2 |
|
|
3 |
import com.hx.exception.TipsException; |
|
4 |
import com.hx.util.SimpleTool; |
|
5 |
import com.hx.util.StringUtils; |
|
6 |
|
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
/** |
|
10 |
* mybatis 自定义处理sql语句 |
|
11 |
* @author chenjiahe |
|
12 |
* @Data: 2020-06-08 |
|
13 |
*/ |
|
14 |
public class SqlSentence { |
|
15 |
|
|
16 |
/**执行语句*/ |
|
17 |
private String sqlSentence; |
|
18 |
|
|
19 |
/**新增存在查询判断*/ |
|
20 |
private String whereExist; |
|
21 |
|
|
22 |
private Map<String,Object> m; |
|
23 |
/**排序*/ |
|
24 |
private String orderBy; |
|
25 |
/**开始页数*/ |
|
26 |
private Integer startPage = 0; |
|
27 |
/**每页数量*/ |
|
28 |
private Integer pageNum = 0; |
|
29 |
/**条数(当前类使用)*/ |
|
30 |
private Integer pageSize; |
|
31 |
|
|
32 |
////////////////////////////////////////////////////////////// |
|
33 |
|
|
34 |
/********************mother****************************/ |
|
35 |
|
|
36 |
public SqlSentence() |
|
37 |
{ |
|
38 |
|
|
39 |
} |
|
40 |
|
|
41 |
public SqlSentence(Class c, String where, Map<String, Object> values) |
|
42 |
{ |
|
43 |
if(StringUtils.isEmpty(where)) |
|
44 |
{ |
|
45 |
throw new TipsException("sql is null"); |
|
46 |
} |
|
47 |
|
|
48 |
if(values == null) |
|
49 |
{ |
|
50 |
throw new TipsException("values is null"); |
|
51 |
} |
|
52 |
|
|
53 |
sqlSentence = "select * from " + c.getSimpleName()+ " where " + where; |
|
54 |
m = values; |
|
55 |
} |
|
56 |
|
|
57 |
public SqlSentence(String where, Map<String, Object> values) |
|
58 |
{ |
|
59 |
if(StringUtils.isEmpty(where)) |
|
60 |
{ |
|
61 |
throw new TipsException("sql is null"); |
|
62 |
} |
|
63 |
|
|
64 |
if(values == null) |
|
65 |
{ |
|
66 |
throw new TipsException("values is null"); |
|
67 |
} |
|
68 |
|
|
69 |
sqlSentence = where; |
|
70 |
m = values; |
|
71 |
} |
|
72 |
|
|
73 |
|
|
74 |
/** |
|
75 |
* sql整条语句 |
|
76 |
* @param sql 如:select * from user Where name = #{m.userName} order by age desc |
|
77 |
* @param values 存放的值如:values.put("userName","ChenJiaHe") |
|
78 |
*/ |
|
79 |
public void sqlSentence(String sql,Map<String,Object> values) { |
|
80 |
if(StringUtils.isEmpty(sql)) |
|
81 |
{ |
|
82 |
throw new TipsException("sql is null"); |
|
83 |
} |
|
84 |
|
|
85 |
if(values == null) |
|
86 |
{ |
|
87 |
throw new TipsException("values is null"); |
|
88 |
} |
|
89 |
|
|
90 |
sqlSentence = sql; |
|
91 |
m = values; |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* sql整条语句-分页 |
|
96 |
* @param sql 如:select * from user Where name = #{m.userName} order by age desc |
|
97 |
* @param values 存放的值如:values.put("userName","ChenJiaHe") |
|
98 |
* @param pageNum 当前页数 |
|
99 |
* @param pageSize 当前条数 |
|
100 |
*/ |
|
101 |
public void sqlSentencePage(String sql,Map<String,Object> values,int pageNum,int pageSize) { |
|
102 |
if(StringUtils.isEmpty(sql)) { |
|
103 |
throw new TipsException("sql is null"); |
|
104 |
} |
|
105 |
|
|
106 |
if(values == null) { |
|
107 |
throw new TipsException("values is null"); |
|
108 |
} |
|
109 |
if(pageNum < 1){ |
|
110 |
pageNum = 1; |
|
111 |
} |
|
112 |
if(pageSize < 1){ |
|
113 |
pageSize = 1; |
|
114 |
} |
|
115 |
this.pageNum = pageNum; |
|
116 |
this.pageSize = pageSize; |
|
117 |
|
|
118 |
//算起点 |
|
119 |
int pageStart = (pageNum-1)*pageSize; |
|
120 |
sql = sql+" LIMIT "+pageStart+","+pageSize; |
|
121 |
sqlSentence = sql; |
|
122 |
m = values; |
|
123 |
} |
|
124 |
|
|
125 |
/**分页组装信息 |
|
126 |
* @param data 装载信息 |
|
127 |
* @param totalNum 总页数 |
|
128 |
* @return 装载信息 |
|
129 |
*/ |
|
130 |
public Map<String,Object> pageAssembleData(Map<String,Object> data,int totalNum){ |
|
131 |
data.put("pageNum",pageNum); |
|
132 |
data.put("pageSize",pageSize); |
|
133 |
data.put("total",totalNum); |
|
134 |
int pages; |
|
135 |
if(pageSize > 0){ |
|
136 |
if(totalNum%pageSize > 0){ |
|
137 |
pages = totalNum/pageSize+1; |
|
138 |
}else{ |
|
139 |
pages = totalNum/pageSize; |
|
140 |
} |
|
141 |
}else{ |
|
142 |
pages = 0; |
|
143 |
} |
|
144 |
data.put("pages",pages); |
|
145 |
if(pages <= pageNum){ |
|
146 |
data.put("isLastPage",true); |
|
147 |
}else{ |
|
148 |
data.put("isLastPage",false); |
|
149 |
} |
|
150 |
return data; |
|
151 |
} |
|
152 |
|
|
153 |
|
|
154 |
/** |
|
155 |
* 查询的语句 |
|
156 |
* @param sql 如:id = #{m.userId} order by age DESC |
|
157 |
* @param values 存放的值如:values.put("userId","123456") |
|
158 |
*/ |
|
159 |
public void sqlWhere(String sql,Map<String,Object> values) { |
|
160 |
if(!SimpleTool.checkNotNull(values)){ |
|
161 |
throw new TipsException("values is null"); |
|
162 |
} |
|
163 |
if(!SimpleTool.checkNotNull(sql)) { |
|
164 |
sql = "1=1"; |
|
165 |
} |
|
166 |
sqlSentence = sql; |
|
167 |
m = values; |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* 更新语句的语句 |
|
172 |
* @param sql 如:name = #{m.name},age = ? WHERE id = #{m.id} |
|
173 |
* @param values 存放的值 |
|
174 |
*/ |
|
175 |
public void sqlUpdate(String sql,Map<String,Object> values) { |
|
176 |
if(!SimpleTool.checkNotNull(values)){ |
|
177 |
throw new TipsException("values is null"); |
|
178 |
} |
|
179 |
m = values; |
|
180 |
sqlSentence = sql; |
|
181 |
} |
|
182 |
|
|
183 |
|
|
184 |
/************************************************************************/ |
|
185 |
|
|
186 |
public Map<String, Object> getM() { |
|
187 |
return m; |
|
188 |
} |
|
189 |
|
|
190 |
public void setM(Map<String, Object> m) { |
|
191 |
this.m = m; |
|
192 |
} |
|
193 |
|
|
194 |
public String getSqlSentence() { |
|
195 |
return sqlSentence; |
|
196 |
} |
|
197 |
|
|
198 |
public void setSqlSentence(String sqlSentence) { |
|
199 |
this.sqlSentence = sqlSentence; |
|
200 |
} |
|
201 |
|
|
202 |
public String getOrderBy() { |
|
203 |
return orderBy; |
|
204 |
} |
|
205 |
|
|
206 |
public void setOrderBy(String orderBy) { |
|
207 |
this.orderBy = orderBy; |
|
208 |
} |
|
209 |
|
|
210 |
public Integer getStartPage() { |
|
211 |
return startPage; |
|
212 |
} |
|
213 |
|
|
214 |
public void setStartPage(Integer startPage) { |
|
215 |
this.startPage = startPage; |
|
216 |
} |
|
217 |
|
|
218 |
public Integer getPageNum() { |
|
219 |
return pageNum; |
|
220 |
} |
|
221 |
|
|
222 |
public void setPageNum(Integer pageNum) { |
|
223 |
this.pageNum = pageNum; |
|
224 |
} |
|
225 |
|
|
226 |
public Integer getStartIndex() |
|
227 |
{ |
|
228 |
return (startPage - 1) * pageNum; |
|
229 |
} |
|
230 |
|
|
231 |
public String getWhereExist() { |
|
232 |
return whereExist; |
|
233 |
} |
|
234 |
|
|
235 |
public void setWhereExist(String whereExist) { |
|
236 |
if(StringUtils.isNull(whereExist)){ |
|
237 |
whereExist = null; |
|
238 |
} |
|
239 |
this.whereExist = whereExist; |
|
240 |
} |
|
241 |
|
|
242 |
public Integer getPageSize() { |
|
243 |
return pageSize; |
|
244 |
} |
|
245 |
|
|
246 |
public void setPageSize(Integer pageSize) { |
|
247 |
this.pageSize = pageSize; |
|
248 |
} |
|
249 |
} |
|
250 |
|
|
251 |
|