001 public abstractclass BaseDAO {
002 protectedSession getSession() {
003 returnMySessionFactory.getSession();
004 }
005
006 protectedvoid closeSession() {
007 MySessionFactory.closeSession();
008 }
009
010 protectedObject getByID(Class cls, Serializable id) {
011 Session session = getSession();
012
013 Object obj = session.get(cls, id);
014
015 closeSession();
016 returnobj;
017 }
018
019 protectedList getByPro(String className, String property, String value,
020 String orderHQL,boolean cache) {
021 Session session = getSession();
022
023 StringBuffer hql =new StringBuffer("from ");
024 hql.append(className);
025 hql.append(" where ");
026 hql.append(property);
027 hql.append("=:value ");
028 hql.append(orderHQL);
029
030 Query query = session.createQuery(hql.toString()).setString("value",
031 value);
032
033 query.setCacheable(cache);
034
035 List result = query.list();
036
037 closeSession();
038
039 hql = null;
040
041 returnresult;
042 }
043
044 protectedList getByPro(String className, String property, String value,
045 intpageNO, int pageSize, String orderHQL, boolean cache) {
046 Session session = getSession();
047
048 StringBuffer hql =new StringBuffer("from ");
049 hql.append(className);
050 hql.append(" where ");
051 hql.append(property);
052 hql.append("=:proValues ");
053 hql.append(orderHQL);
054
055 intFirstIndex = pageSize * (pageNO - 1);
056
057 Query query = session.createQuery(hql.toString()).setString(
058 "proValues", value).setFirstResult(FirstIndex).setMaxResults(
059 pageSize);
060
061 query.setCacheable(cache);
062
063 List all = query.list();
064
065 closeSession();
066
067 hql = null;
068
069 returnall;
070 }
071
072 protectedList getByPro(String className, String property, intvalue,
073 String orderHQL,boolean cache) {
074 Session session = getSession();
075
076 StringBuffer hql =new StringBuffer("from ");
077 hql.append(className);
078 hql.append(" where ");
079 hql.append(property);
080 hql.append("=:value ");
081 hql.append(orderHQL);
082
083 Query query = session.createQuery(hql.toString()).setInteger("value",
084 value);
085
086 query.setCacheable(cache);
087
088 List result = query.list();
089
090 closeSession();
091
092 hql = null;
093
094 returnresult;
095 }
096
097 protectedList getByPro(String className, String property, intvalue,
098 intpageNO, int pageSize, String orderHQL, boolean cache) {
099 Session session = getSession();
100
101 StringBuffer hql =new StringBuffer("from ");
102 hql.append(className);
103 hql.append(" where ");
104 hql.append(property);
105 hql.append("=:proValues ");
106 hql.append(orderHQL);
107
108 intFirstIndex = pageSize * (pageNO - 1);
109
110 Query query = session.createQuery(hql.toString()).setInteger(
111 "proValues", value).setFirstResult(FirstIndex).setMaxResults(
112 pageSize);
113
114 query.setCacheable(cache);
115
116 List all = query.list();
117
118 closeSession();
119
120 hql = null;
121
122 returnall;
123 }
124
125 protectedList getByLike(String className, String property,
126 String likeValue, String orderHQL,boolean cache) {
127 Session session = getSession();
128
129 StringBuffer hql =new StringBuffer("from ");
130 hql.append(className);
131 hql.append(" where ");
132 hql.append(property);
133 hql.append(" like '");
134 hql.append(likeValue).append("%' ");
135 hql.append(orderHQL);
136
137 Query query = session.createQuery(hql.toString());
138
139 query.setCacheable(cache);
140
141 List result = query.list();
142
143 closeSession();
144
145 hql = null;
146
147 returnresult;
148 }
149
150 protectedList getByLike(String className, String property,
151 String likeValue, String orderHQL,int pageNO, intpageSize,
152 booleancache) {
153 Session session = getSession();
154
155 StringBuffer hql =new StringBuffer("from ");
156 hql.append(className);
157 hql.append(" where ");
158 hql.append(property);
159 hql.append(" like '");
160 hql.append(likeValue).append("%' ");
161 hql.append(orderHQL);
162
163 intFirstIndex = pageSize * (pageNO - 1);
164
165 Query query = session.createQuery(hql.toString()).setFirstResult(
166 FirstIndex).setMaxResults(pageSize);
167
168 query.setCacheable(cache);
169
170 List result = query.list();
171
172 closeSession();
173
174 hql = null;
175
176 returnresult;
177 }
178
179 protectedboolean add(Object obj) {
180 Session session = getSession();
181
182 Transaction tx =null;
183 try{
184 tx = session.beginTransaction();
185
186 session.save(obj);
187
188 tx.commit();
189 returntrue;
190 } catch(Exception e) {
191 if(tx != null)
192 tx.rollback();
193 returnfalse;
194 } finally{
195 closeSession();
196 }
197
198 }
199
200 protectedboolean batchUpdate(String sql) {
201 Session session = getSession();
202 Transaction tx =null;
203 try{
204 tx = session.beginTransaction();
205
206 session.createSQLQuery(sql).executeUpdate();
207
208 tx.commit();
209 returntrue;
210 } catch(Exception e) {
211 if(tx != null) {
212 tx.rollback();
213 }
214 System.out.println("批量操作失败!");
215 returnfalse;
216 } finally{
217 closeSession();
218 }
219
220 }
221
222 protectedboolean update(Object obj) {
223 Session session = getSession();
224
225 Transaction tx =null;
226 try{
227 tx = session.beginTransaction();
228
229 session.update(obj);
230
231 tx.commit();
232 returntrue;
233 } catch(Exception e) {
234 if(tx != null)
235 tx.rollback();
236 returnfalse;
237 } finally{
238 closeSession();
239 }
240
241 }
242
243 protectedboolean delete(Object obj) {
244 Session session = getSession();
245
246 Transaction tx =null;
247 try{
248 tx = session.beginTransaction();
249
250 session.delete(obj);
251
252 tx.commit();
253 returntrue;
254 } catch(Exception e) {
255 if(tx != null)
256 tx.rollback();
257 returnfalse;
258 } finally{
259 closeSession();
260 }
261
262 }
263
264 protectedboolean delete(String className, String key, String value) {
265 Session session = getSession();
266
267 Transaction tx =null;
268
269 StringBuffer hql =new StringBuffer("delete ");
270 hql.append(className).append(" where ").append(key).append("=").append(
271 value);
272 try{
273 tx = session.beginTransaction();
274
275 session.createQuery(hql.toString()).executeUpdate();
276
277 tx.commit();
278 returntrue;
279 } catch(Exception e) {
280 if(tx != null)
281 tx.rollback();
282 returnfalse;
283 } finally{
284 closeSession();
285 }
286
287 }
288
289 protectedint getResult(String className, String function, String key) {
290 Session session = getSession();
291
292 StringBuffer hql =new StringBuffer("select ");
293 hql.append(function);
294 hql.append("(");
295 hql.append(key);
296 hql.append(") from ");
297 hql.append(className);
298
299 Object result = session.createQuery(hql.toString()).uniqueResult();
300
301 closeSession();
302
303 hql = null;
304
305 returnresult == null ? 0 : Integer.parseInt(result.toString());
306 }
307
308 protectedint getResult(String className, String function, String key,
309 String property, String value) {
310 Session session = getSession();
311
312 StringBuffer hql =new StringBuffer("select ");
313 hql.append(function);
314 hql.append("(");
315 hql.append(key);
316 hql.append(") from ");
317 hql.append(className);
318 hql.append(" where ");
319 hql.append(property);
320 hql.append(" =:proValue");
321
322 Object result = session.createQuery(hql.toString()).setString(
323 "proValue", value).uniqueResult();
324
325 closeSession();
326
327 hql = null;
328
329 returnresult == null ? 0 : Integer.parseInt(result.toString());
330 }
331
332 protectedint getResult(String className, String function, String key,
333 String property,int value) {
334 Session session = getSession();
335
336 StringBuffer hql =new StringBuffer("select ");
337 hql.append(function);
338 hql.append("(");
339 hql.append(key);
340 hql.append(") from ");
341 hql.append(className);
342 hql.append(" where ");
343 hql.append(property);
344 hql.append(" =:proValue");
345
346 Object result = session.createQuery(hql.toString()).setInteger(
347 "proValue", value).uniqueResult();
348
349 closeSession();
350
351 hql = null;
352
353 returnresult == null ? 0 : Integer.parseInt(result.toString());
354 }
355
356 protectedint getResultSQL(String sql) {
357 Session session = getSession();
358
359 Object result = session.createSQLQuery(sql).uniqueResult();
360
361 closeSession();
362
363 sql = null;
364
365 returnresult == null ? 0 : Integer.parseInt(result.toString());
366 }
367
368 protectedint getResultHQL(String hql) {
369 Session session = getSession();
370
371 Object result = session.createQuery(hql).uniqueResult();
372
373 closeSession();
374
375 hql = null;
376
377 returnresult == null ? 0 : Integer.parseInt(result.toString());
378 }
379
380 protectedList getAll(String className, String orderHQL, booleancache) {
381 Session session = getSession();
382
383 StringBuffer hql =new StringBuffer("from ");
384 hql.append(className);
385 hql.append(" ");
386 hql.append(orderHQL);
387
388 Query query = session.createQuery(hql.toString());
389
390 query.setCacheable(cache);
391
392 List all = query.list();
393
394 closeSession();
395
396 hql = null;
397
398 returnall;
399 }
400
401 protectedList getAll(String className, String orderHQL, intpageNO,
402 intpageSize, boolean cache) {
403 Session session = getSession();
404
405 StringBuffer hql =new StringBuffer("from ");
406 hql.append(className);
407 hql.append(" ");
408 hql.append(orderHQL);
409
410 intFirstIndex = pageSize * (pageNO - 1);
411
412 Query query = session.createQuery(hql.toString()).setFirstResult(
413 FirstIndex).setMaxResults(pageSize);
414
415 query.setCacheable(cache);
416
417 List all = query.list();
418
419 closeSession();
420
421 hql = null;
422
423 returnall;
424 }
425
426 protectedList getAllHQL(String hql) {
427 Session session = getSession();
428
429 List all = session.createQuery(hql).list();
430
431 closeSession();
432
433 hql = null;
434
435 returnall;
436 }
437
438 protectedList getAllHQL(String hql, booleancache) {
439 Session session = getSession();
440
441 Query query = session.createQuery(hql);
442
443 query.setCacheable(cache);
444
445 List all = query.list();
446
447 closeSession();
448
449 hql = null;
450
451 returnall;
452 }
453
454 protectedList getAllHql(String hql, intpageNO, int pageSize) {
455 Session session = getSession();
456
457 intFirstIndex = pageSize * (pageNO - 1);
458
459 Query query = session.createQuery(hql).setFirstResult(FirstIndex)
460 .setMaxResults(pageSize);
461
462 List all = query.list();
463
464 closeSession();
465
466 hql = null;
467
468 returnall;
469 }
470
471 protectedList getAllHql(String hql, intpageNO, int pageSize, boolean cache) {
472 Session session = getSession();
473
474 intFirstIndex = pageSize * (pageNO - 1);
475
476 Query query = session.createQuery(hql).setFirstResult(FirstIndex)
477 .setMaxResults(pageSize);
478
479 query.setCacheable(cache);
480
481 List all = query.list();
482
483 closeSession();
484
485 hql = null;
486
487 returnall;
488 }
489
490 protectedList getAllSQL(String sql) {
491 Session session = getSession();
492
493 List all = session.createSQLQuery(sql).list();
494
495 closeSession();
496
497 sql = null;
498
499 returnall;
500 }
501
502 protectedList getAllSQL(String sql, intpageNO, int pageSize) {
503 Session session = getSession();
504
505 intFirstIndex = pageSize * (pageNO - 1);
506
507 Query query = session.createSQLQuery(sql).setFirstResult(FirstIndex)
508 .setMaxResults(pageSize);
509
510 List all = query.list();
511
512 closeSession();
513
514 sql = null;
515
516 returnall;
517 }
518 }