Data Access Object (DAO) 是一般的J2EE项目中的一个常见的模块,在我们用一般的方法去实现DAO的过程中会发现在为每个pojo实现DAO的的时候会不断地区重复的写一些常用的方法,如update(),delete(),find()等。
为了解决以上所述的缺点,可以采用DAO用泛型实现的方法,把通用的方法抽出来放到基类中,以后为pojo实现DAO的时候只要继承DAO基类就可以复用这些通用方法。这样的做法即保证了代码的复用,又保证了类型的安全。
下面例子为以前一个项目中的代码片段:
代码说明:
IBaseDao 是DAO基类的接口
BaseHibernateDao 是DAO的Hibernate实现基类(实现了接口IBaseDao)
IUserDao 是具体的DAO接口,用于持久化用户数据(继承了接口IBaseDao)
UserHibernateDao 是具体的DAO的Hibernate实现,持久化用户数据(继承了BaseHibernate 并实现了接口 IUserDao)
UserHibernateDao 继承了BaseHibernateDao的所有功能,在新建一个DAO时
只要用 XxxxxHibernateDao extends BaseHibernate就可以继承BaseHibernate的所有功能。
1.DAO基类接口
Java代码
1. package
2.
3. import
4. import
5.
6. import
7.
8. /**
9. * DAO 基类接口
10. * @param <T> T pojo类型
11. * @param <ID> ID类型
12. */
13. publicinterfaceIBaseDao<T,IDextends
14. {
15.
16.
17. publicabstract
18.
19. /**
20. * 查找所有,并分页
21. * @param page 要返回的页数
22. * @param pageSize 没有记录数
23. * @return
24. */
25. publicabstractList<T> findAll(intpage,int
26.
27. publicabstractvoid
28.
29. publicabstractvoid
30.
31. /**
32. * 与findByProperty相似,当properyName == value 时把相应的记录删除
33. */
34. publicabstractvoid
35.
36. publicabstract
37.
38. /**
39. * 通过属性查找
40. * @param propertyName 属性名称
41. * @param value 属性的值
42. * @return
43. */
44. publicabstract
45.
46.
47.
48. /**
49. * 通过多个属性查找
50. * @param propertyNames 属性名称数组
51. * @param values 属性值数组
52. * @return
53. */
54. publicabstract
55.
56. /**
57. * 通过多个属性查找,并分页,
58. * 属性名称数组和属性值数组的序列要对应
59. *
60. * @param propertyNames 属性名称数组
61. * @param values 属性值数组
62. * @param page 页码
63. * @param pageSize 每页内容条数
64. * @return
65. */
66. publicList<T> findByPropertys(String[] propertyNames,Object[] values,intpage,int
67.
68. /**
69. * 通过属性查找,并分页,
70. * 属性名称数组和属性值数组的序列要对应
71. *
72. * @param propertyNames 属性名称
73. * @param values 属性值
74. * @param page 页码
75. * @param pageSize 每页内容条数
76. * @return
77. */
78. publicList<T> findByProperty(String propertyName,Object value,intpage,int
79.
80. /**
81. * 统计所有记录的总数
82. * @return 总数
83. */
84. publicint
85. /**
86. * 统计数据库中当propertyName=value时的记录总数
87. * @param propertyName
88. * @param value
89. * @return
90. */
91. publicint
92. /**
93. * 统计数据库中当多个propertyName=value时的记录总数
94. * @param propertyNames
95. * @param values
96. * @return
97. */
98. publicint
99.
100. publicabstractvoid
101.
102. publicabstract
103.
104. publicabstractvoid
105.
106.
107. /**
108. * 获得持久化对象的类型
109. * @return
110. */
111. publicabstract
112.
113. /**
114. * 查找并通过某一属性排序
115. * @param property 排序依据的顺序
116. * @param isSequence 是否顺序排序
117. */
118. publicList<T> findAndOrderByProperty(intfirstResult,intfetchSize, String propertyName,boolean
119.
120.
121. /**
122. * 查找并通过某一属性排序
123. * @param property 排序依据的顺序
124. * @param isSequence 是否顺序排序
125. */
126. publicList<T> findAllAndOrderByProperty(String propertyName,boolean
127.
128.
129. }
2.DAO的Hibernate基类
Java代码
1. package
2.
3. import
4. import
5. import
6.
7. import
8. import
9.
10. /**
11. * DAO的Hibernate基类
12. * @author pasu
13. * @param <T>
14. * pojo的类型
15. * @para <ID> id的类型
16. *
17. */
18. publicabstractclassBaseHibernateDao<T, IDextendsSerializable>extends
19. implements
20. {
21. private
22.
23. @SuppressWarnings("unchecked")
24. public
25. {
26. // 获取持久化对象的类型
27. this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
28. 0];
29. }
30.
31. public
32. {
33. return
34. }
35.
36. /**
37. * 通过id查找
38. *
39. * @param id
40. * @return
41. */
42. @SuppressWarnings("unchecked")
43. public
44. {
45. return(T)this.getHibernateTemplate().get(getPersistentClass(), id);
46. }
47.
48. publicvoid
49. {
50. this.getHibernateTemplate().save(entity);
51. }
52.
53. /**
54. * 删除
55. */
56. publicvoid
57. {
58. this.getHibernateTemplate().delete(entity);
59. }
60.
61. /**
62. * 通过属性删除
63. */
64. publicvoid
65. {
66. "delete from "
67. " as model where model." + propertyName + "= ?";
68. this.getSession().createQuery(queryString);
69. 0, value);
70. query.executeUpdate();
71. }
72.
73. /**
74. * saveOrUpdate
75. */
76. publicvoid
77. {
78. this.getHibernateTemplate().saveOrUpdate(entity);
79. }
80.
81. /**
82. * 更新
83. */
84. publicvoid
85. {
86. this.getHibernateTemplate().update(entity);
87. }
88.
89. /**
90. * 分页查找所有的记录
91. *
92. * @param page
93. * 要返回的页数
94. * @param pageSize
95. * 没有记录数
96. * @return
97. */
98. publicList<T> findAll(intpage,int
99. {
100. "from "
101. this.getSession().createQuery(queryString);
102. intfirstResult = (page - 1) * pageSize;
103. query.setFirstResult(firstResult);
104. query.setMaxResults(pageSize);
105. return
106. }
107.
108. /**
109. * 统计所有记录的总数
110. *
111. * @return 总数
112. */
113. publicint
114. {
115. "select count(*) from "
116. + getPersistentClass().getName();
117. this.getSession().createQuery(queryString);
118. List list = query.list();
119. 0);
120. return
121. }
122.
123. /**
124. * find By Example
125. *
126. * @param entity
127. * @return
128. */
129. @SuppressWarnings("unchecked")
130. public
131. {
132. returnthis.getHibernateTemplate().findByExample(entity);
133. }
134.
135. @SuppressWarnings("unchecked")
136. public
137. {
138. returnthis.getHibernateTemplate().find(
139. "from "
140. }
141.
142. /**
143. * 通过属性查找
144. *
145. * @param propertyName
146. * 属性名称
147. * @param value
148. * 属性的值
149. * @return
150. */
151. @SuppressWarnings("unchecked")
152. public
153. {
154. "from "
155. " as model where model." + propertyName + "= ?";
156. returnthis.getHibernateTemplate().find(queryString, value);
157.
158. }
159.
160. /**
161. * 通过多个属性组合查询
162. *
163. * @param propertyNames
164. * 属性名称数组
165. * @param values
166. * 对应于propertyNames的值 return 匹配的结果
167. */
168. public
169. {
170. new
171. "from "
172. " as model where ");
173. for(inti = 0; i < propertyNames.length; i++)
174. {
175. if(i != 0)
176. " and");
177. " model.");
178. strBuffer.append(propertyNames[i]);
179. "=");
180. "? ");
181. }
182. String queryString = strBuffer.toString();
183. returnthis.getHibernateTemplate().find(queryString, values);
184. }
185.
186. /**
187. * 通过属性查找并分页
188. *
189. * @param propertyName
190. * 属性名称
191. * @param value
192. * 属性值
193. * @param page
194. * 页数
195. * @param pageSize
196. * 每页显示条数
197. */
198. publicList<T> findByProperty(String propertyName, Object value,int
199. int
200. {
201. returnthis.findByPropertys(new
202. {
203. propertyName
204. new
205. {
206. value
207. }, page, pageSize);
208. }
209.
210. /**
211. * 通过多个属性组合查询
212. *
213. * @param propertyNames
214. * 属性名称数组
215. * @param values
216. * 对应于propertyNames的值
217. * @param page
218. * 页数
219. * @param pageSize
220. * 每页显示数 return 匹配的结果 return 匹配的结果
221. */
222. public
223. intpage,int
224. {
225.
226. new
227. "from "
228. " as model where ");
229. for(inti = 0; i < propertyNames.length; i++)
230. {
231. if(i != 0)
232. " and");
233. " model.");
234. strBuffer.append(propertyNames[i]);
235. "=");
236. "? ");
237. }
238. String queryString = strBuffer.toString();
239.
240. intfirstResult = (page - 1) * pageSize;
241.
242. this.getSession().createQuery(queryString);
243. query.setFirstResult(firstResult);
244. query.setMaxResults(pageSize);
245. for(inti = 0; i < values.length; i++)
246. {
247. query.setParameter(i, values[i]);
248. }
249.
250. return
251. }
252.
253. /**
254. * 通过属性统计数量
255. *
256. * @param propertyName
257. * 属性名称
258. * @param value
259. * 属性值
260. */
261. publicint
262. {
263. new
264. {
265. propertyName
266. };
267. new
268. {
269. value
270. };
271. returnthis.countByPropertys(propertyNames, values);
272. }
273.
274. /**
275. * 通过多个属性统计数量
276. *
277. * @param propertyNames
278. * 属性名称数组
279. * @param values
280. * 对应的属性值数组 return
281. */
282. publicint
283. {
284. new
285. "select count(*) from "
286. + getPersistentClass().getName());
287. " as model where ");
288. for(inti = 0; i < propertyNames.length; i++)
289. {
290. if(i != 0)
291. " and");
292. " model.");
293. strBuffer.append(propertyNames[i]);
294. "=");
295. "? ");
296. }
297.
298. String queryString = strBuffer.toString();
299. this.getSession().createQuery(queryString);
300. for(inti = 0; i < values.length; i++)
301. {
302. query.setParameter(i, values[i]);
303. }
304.
305. List list = query.list();
306. 0);
307. return
308. }
309.
310. /**
311. * 查找T并通过某一属性排序
312. *
313. * @param property
314. * 排序依据的顺序
315. * @param isSequence
316. * 是否顺序排序,false为倒序
317. */
318. publicList<T> findAndOrderByProperty(intfirstResult,int
319. boolean
320. {
321. "from "
322. " as model order by model."
323. if(isSequence ==false)
324. {
325. " DESC";
326. }
327.
328. Query queryObject = getSession().createQuery(queryString);
329. queryObject.setFirstResult(firstResult);
330. queryObject.setMaxResults(fetchSize);
331. return
332.
333. }
334.
335. /**
336. * 查找所有并通过某个属性排序
337. *
338. * @param propertyName
339. * 排序依据的属性名称
340. * @param isSequence
341. * 是否顺序排列
342. */
343. public
344. boolean
345. {
346. "from "
347. " as model order by model."
348. if(isSequence ==false)
349. {
350. " DESC";
351. }
352.
353. Query queryObject = getSession().createQuery(queryString);
354. return
355. }
356.
357. }
3.具体的DAO接口(继承IBaseDao):
Java代码
1. package
2.
3. import
4. import
5.
6. /**
7. * 用户Dao
8. *
9. * @author pasu
10. * @see com.rc.video.common.base.IBaseDao
11. * @vesion 1.0, 2008-3-2
12. */
13. publicinterfaceIUserDaoextends
14. {
15. /**
16. * 通过用户名查找用户
17. *
18. * @param userName 用户名
19. * @return TUser 用户对象,如果用户名不存在返回null
20. */
21. public
22. }
4.具体的Dao实现(Hibernate)
Java代码
1. package
2.
3. import
4.
5. import
6. import
7. import
8.
9. /**
10. * 用户Dao
11. * @author pasu
12. * @vesion 1.0, 2008-3-2
13. *
14. */
15. publicclassUserHibernateDaoextendsBaseHibernateDao<TUser,String>implements
16. {
17. // property constants
18. publicstaticfinalString USER_NAME = "userName";
19.
20. /**
21. * 通过名称查找用户
22. * @return TUser
23. */
24. public
25. {
26. super.findByProperty(USER_NAME, userName);
27. if(userList.size() != 0)
28. {
29. returnuserList.get(0);
30. }
31. else
32. {
33. returnnull;
34. }
35. }
36. }