目前正在做SSH2整合項目,寫了一個DAO基類,發上來大家提提意見,看能不能在改進一下。
1 /**
2 * @(#)IBaseDAO.java 2009-9-14 下午03:05:59
3 * Copyright 2009 Bobby_Guo, Inc. All rights reserved
4 */
5 package cn.commonframework.util;
6
7 import java.io.Serializable;
8 import java.util.List;
9
10 import org.hibernate.criterion.Criterion;
11
12 /**
13 * @description:公用DAO接口,包含基本的增、刪、改、查操作.
14 * @author :Bobby_Guo <br>
15 * @version :1.0 <br>
16 * @date :2009-9-14 下午03:12:53 <br>
17 * @param <T>
18 */
19 public interface IBaseDAO<T> {
20 /**
21 * 保存一個實體對象
22 * @param t
23 */
24 public void save(T t);
25 /**
26 * 更新一個實體對象
27 * @param t
28 */
29 public void update(T t);
30 /**
31 * 批量更新
32 * @param hql
33 * @param o
34 * @return
35 */
36 public int batchUpdate(String hql,Object
o);
37 /**
38 * 刪除一個實體對象
39 * @param t
40 */
41 public void delete(T t);
42 /**
43 * 根據主鍵查找實體對象
44 * @param id
45 * @return
46 */
47 public T findById(Serializable id);
48 /**
49 * 查找所有實體對象
50 * @return
51 */
52 public List<T> getAll();
53 /**
54 * HQL查詢
55 * @param hql
56 * @return
57 */
58 public List<T> getAllByHql(String hql);
59 /**
60 * QBC查詢
61 * @param criterion
62 * @return
63 */
64 public List<T> getAllByCriteria(Criterion
criterion);
65 /**
66 * QBE查詢
67 * @return
68 */
69 public List<T> getAllByExample(T t,boolean enableLike,String
properties);
70 /**
71 * 默認的QBE查詢
72 * @param t
73 * @return
74 */
75 public List<T> getAllByExample(T t);
76 }
77
下面是BaseDAO類:

BaseDAO.java
1 /**
2 * @(#)BaseDAO.java 2009-9-14 下午03:26:46
3 * Copyright 2009 Bobby_Guo, Inc. All rights reserved
4 */
5 package cn.commonframework.util;
6
7 import java.io.Serializable;
8 import java.util.List;
9 import org.hibernate.criterion.Criterion;
10 import org.hibernate.criterion.DetachedCriteria;
11 import org.hibernate.criterion.Example;
12 import org.hibernate.criterion.MatchMode;
13 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
14
15 /**
16 * DAO基類,實現公用DAO接口,提供基本的數據庫操作。采用泛型設計,增強代碼的復用性。
17 * @author :Bobby_Guo <br>
18 * @version :1.0 <br>
19 * @date :2009-9-14 下午03:26:46 <br>
20 */
21 public class BaseDAO<T> extends HibernateDaoSupport implements IBaseDAO<T> {
22
23 /**
24 * 聲明一個實體類
25 */
26 private Class<T> entityClass;
27 /**
28 * 無參構造方法
29 */
30 public BaseDAO(){
31
32 }
33 /**
34 * 構造方法 初始化實體類
35 * @param entityClass
36 */
37 public BaseDAO(Class<T> entityClass){
38 this.entityClass = entityClass;
39 }
40 /**
41 * 刪除一個實體對象。
42 */
43 public void delete(T t) {
44 this.getHibernateTemplate().delete(t);
45
46 }
47 /**
48 * 根據主鍵檢索實體對象
49 */
50 @SuppressWarnings("unchecked")
51 public T findById(Serializable id) {
52 return (T) this.getHibernateTemplate().load(entityClass, id);
53 }
54 /**
55 * 查找所有記錄。
56 */
57 @SuppressWarnings("unchecked")
58 public List<T> getAll() {
59 DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);
60
61 return this.getHibernateTemplate().findByCriteria(criteria);
62 }
63 /**
64 * 保存實體對象
65 */
66 public void save(T t) {
67
68 this.getHibernateTemplate().save(t);
69
70
71 }
72 /**
73 * 更新實體對象
74 */
75 public void update(T t) {
76 this.getHibernateTemplate().update(t);
77
78 }
79 /**
80 * 批量更新
81 */
82 public int batchUpdate(String hql,Object
o){
83
84 return this.getHibernateTemplate().bulkUpdate(hql, o);
85 }
86 /**
87 * QBC查詢
88 */
89 @SuppressWarnings("unchecked")
90 public List<T> getAllByCriteria(Criterion
criterion) {
91 DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);
92 for(Criterion c : criterion){
93 criteria.add(c);
94 }
95 return this.getHibernateTemplate().findByCriteria(criteria);
96 }
97 /**
98 * QBE查詢 enableLike表示是否為模糊查詢 properties為模版對象中要除去的屬性名稱
99 */
100 public List<T> getAllByExample(T t, boolean enableLike,
101 String
properties) {
102 Example example = null;
103 if(enableLike){
104 example = Example.create(t).ignoreCase().enableLike(MatchMode.ANYWHERE).excludeZeroes();
105 }else{
106 example = Example.create(t);
107 }
108 for(String s : properties){
109 example.excludeProperty(s);
110 }
111 return this.getAllByCriteria(example);
112 }
113 /**
114 * HQL查詢
115 */
116 @SuppressWarnings("unchecked")
117 public List<T> getAllByHql(String hql) {
118
119 return this.getHibernateTemplate().find(hql);
120 }
121 /**
122 * QBE查詢 精確查詢
123 */
124 @SuppressWarnings("unchecked")
125 public List<T> getAllByExample(T t){
126 return this.getHibernateTemplate().findByExample(t);
127 }
128
129 }
130
posted on 2009-09-28 10:28
bobby 閱讀(1594)
評論(4) 編輯 收藏 所屬分類:
BaseUtil