key words : hibernate spring dao
come from :
http://lpacec.javaeye.com/blog/46220 1. package infoweb.dao;
2.
3. import java.util.List;
4. import java.util.Iterator;
5.
6. import infoweb.pojo.Info;
7.
8. import net.sf.hibernate.HibernateException;
9. import net.sf.hibernate.Query;
10. import net.sf.hibernate.Session;
11.
12. import org.springframework.orm.hibernate.HibernateCallback;
13. import org.springframework.orm.hibernate.support.HibernateDaoSupport;
14.
15. /** *//**
16. *
Title:
17. *
Description:
18. *
Copyright: Copyright (c) 2004
19. *
Company:
20. * @author 段洪杰
21. * @version 1.0
22. */
23.
24. public class InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {
25. /** *//**
26. * 构造函数
27. */
28. public InfoDAOImpl() {
29. super();
30. }
31.
32. /** *//**
33. * 增加记录
34. * @param info Info
35. */
36. public void setInfo(Info info) throws Exception {
37. getHibernateTemplate().save(info);
38. }
39.
40. /** *//**
41. * 通过ID取得记录
42. * @param id String
43. * @return Info
44. */
45. public Info getInfoById(String id) throws Exception {
46. Info info = (Info) getHibernateTemplate().load(Info.class, id);
47. return info;
48. }
49.
50. /** *//**
51. * 修改记录
52. * @param Info info
53. */
54. public void modifyInfo(Info info) throws Exception {
55. getHibernateTemplate().update(info);
56. }
57.
58. /** *//**
59. * 删除记录
60. * @param Info info
61. */
62. public void removeInfo(Info info) throws Exception {
63. getHibernateTemplate().delete(info);
64. }
65.
66. ////////////////////////////////////////////////////////
67. ///// ///
68. /////以下部份不带审核功能 ///
69. ///// ///
70. ////////////////////////////////////////////////////////
71.
72. /** *//**
73. * 取记录总数
74. * @return int
75. */
76. public int getInfosCount() throws Exception {
77. int count = 0;
78. String queryString = "select count(*) from Info";
79. count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
80. intValue();
81. return count;
82. }
83.
84. /** *//**
85. * 取所有记录集合
86. * @return Iterator
87. */
88. public Iterator getAllInfos() throws Exception {
89. Iterator iterator = null;
90. String queryString = " select info from Info as info order by info.id desc";
91. List list = getHibernateTemplate().find(queryString);
92. iterator = list.iterator();
93. return iterator;
94. }
95.
96. /** *//**
97. * 取记录集合
98. * @return Iterator
99. * @param int position, int length
100. */
101. public Iterator getInfos(int position, int length) throws Exception {
102. Iterator iterator = null;
103. String queryString = " select info from Info as info order by info.id desc";
104. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
105. //设置游标的起始点
106. query.setFirstResult(position);
107. //设置游标的长度
108. query.setMaxResults(length);
109. //记录生成
110. List list = query.list();
111. //把查询到的结果放入迭代器
112. iterator = list.iterator();
113. return iterator;
114. }
115.
116. /** *//**
117. * 取第一条记录
118. * @throws Exception
119. * @return Station
120. */
121. public Info getFirstInfo() throws Exception {
122. Iterator iterator = null;
123. Info info = null;
124. String queryString = "select info from Info as info order by info.id desc";
125. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
126. //记录生成
127. List list = query.list();
128. //把查询到的结果放入迭代器
129. iterator = list.iterator();
130. if (iterator.hasNext()) {
131. info = (Info) iterator.next();
132. }
133. return info;
134. }
135.
136. /** *//**
137. * 取最后一条记录
138. * @throws Exception
139. * @return Station
140. */
141. public Info getLastInfo() throws Exception {
142. Iterator iterator = null;
143. Info info = null;
144. String queryString = "select info from Info as info order by info.id asc";
145. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
146. //记录生成
147. List list = query.list();
148. //把查询到的结果放入迭代器
149. iterator = list.iterator();
150. if (iterator.hasNext()) {
151. info = (Info) iterator.next();
152. }
153. return info;
154.
155. }
156.
157. ////////////////////////////////////////////////////////
158. ///// ///
159. ///// 以下部份表中要有特定字段才能正确运行 个人和企业 ///
160. ///// ///
161. ////////////////////////////////////////////////////////
162.
163. /** *//**
164. * 取符合条件记录总数, [表中要有 isperson 字段]
165. * @return int
166. * @param int isPerson
167. */
168.
169. public int getInfosCountByIsperson(int isPerson) throws Exception {
170. int count = 0;
171. String queryString =
172. "select count(*) from Info as info where info.isperson =" + isPerson;
173. count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
174. intValue();
175. return count;
176. }
177.
178. /** *//**
179. * 取所有符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
180. * @return Iterator
181. * @param int isPerson
182. */
183.
184. public Iterator getAllInfosByIsperson(int isPerson) throws Exception {
185. Iterator iterator = null;
186. String queryString = " select info from Info as info where info.isperson =" +
187. isPerson + " order by info.id desc";
188. List list = getHibernateTemplate().find(queryString);
189. //把查询到的结果放入迭代器
190. iterator = list.iterator();
191. return iterator;
192. }
193.
194. /** *//**
195. * 取符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
196. * @return Iterator
197. * @param int isPerson,int position, int length
198. */
199.
200. public Iterator getInfosByIsperson(int isPerson, int position, int length) throws
201. Exception {
202. Iterator iterator = null;
203. String queryString = " select info from Info as info where info.isperson =" +
204. isPerson + " order by info.id desc";
205. //创建查询
206. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
207. //设置游标的起始点
208. query.setFirstResult(position);
209. //设置游标的长度
210. query.setMaxResults(length);
211. //记录生成
212. List list = query.list();
213. //把查询到的结果放入迭代器
214. iterator = list.iterator();
215. return iterator;
216. }
217.
218. ////////////////////////////////////////////////////////
219. ///// ///
220. ///// 以下部份表中要有特定字段才能正确运行 查询部份 ///
221. ///// ///
222. ///////////////////////////////////////////////////////
223. /** *//**
224. * 取符合条件记录总数, 模糊查询条件.[表中要有 title 字段]
225. * @return int
226. * @param String text
227. */
228. public int getInfosCount(String text) throws Exception {
229. int count = 0;
230. count = ((Integer) getHibernateTemplate().iterate(
231. "select count(*) from Info as info where info.title like '%" + text +
232. "%'").next()).intValue();
233. return count;
234. }
235.
236. /** *//**
237. * 取所有符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
238. * @return Iterator
239. * @param String text
240. */
241.
242. public Iterator getAllInfos(String text) throws Exception {
243. Iterator iterator = null;
244. String queryString =
245. " select info from Info as info where info.title like '%" + text +
246. "%' order by info.id desc";
247. //创建查询
248. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
249. //记录生成
250. List list = query.list();
251. //把查询到的结果放入迭代器
252. iterator = list.iterator();
253. return iterator;
254. }
255.
256. /** *//**
257. * 取符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
258. * @return Iterator
259. * @param String text,int position, int length
260. */
261. public Iterator getInfos(String text, int position, int length) throws
262. Exception {
263. Iterator iterator = null;
264. String queryString =
265. " select info from Info as info where info.title like '%" + text +
266. "%' order by info.id desc";
267.
268. //创建查询
269. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
270. //设置游标的起始点
271. query.setFirstResult(position);
272. //设置游标的长度
273. query.setMaxResults(length);
274. //记录生成
275. List list = query.list();
276. //把查询到的结果放入迭代器
277. iterator = list.iterator();
278. return iterator;
279. }
280.
281. ////////////////////////////////////////////////////////
282. ///// ///
283. ///// 以下部份表中要有特定字段才能正确运行 注册相关 ///
284. ///// ///
285. ////////////////////////////////////////////////////////
286.
287. /** *//**
288. * 取符合条件记录总数.[ 表中要有 registername 字段]
289. * @return int
290. * @param String text
291. */
292. public int getInfosCountByRegisterName(String registerName) throws Exception {
293. int count = 0;
294. count = ((Integer) getHibernateTemplate().iterate(
295. "select count(*) from Info as info where info.registername = '" +
296. registerName + "'").next()).intValue();
297. return count;
298. }
299.
300. /** *//**
301. * 通过注册名取得一条记录,如有多条,只取第一条.[表中要有 registername字段]
302. * @param registername String
303. * @return Info
304. */
305. public Info getInfoByRegisterName(String registerName) throws Exception {
306. Iterator iterator = null;
307. Info info = null;
308. String queryString =
309. " select info from Info as info where info.registername='" +
310. registerName + "' order by info.id desc";
311. //创建查询
312. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
313. //记录生成
314. List list = query.list();
315. //把查询到的结果放入迭代器
316. iterator = list.iterator();
317. if (iterator.hasNext()) {
318. info = (Info) iterator.next();
319. }
320. return info;
321. }
322.
323. /** *//**
324. * 通过注册名取得所有记录集合.[表中要有 registername字段]
325. * @param registername String
326. * @return Iterator
327. */
328. public Iterator getAllInfosByRegisterName(String registerName) throws
329. Exception {
330. Iterator iterator = null;
331. String queryString =
332. " select info from Info as info where info.registername='" +
333. registerName + "' order by info.id desc";
334. //创建查询
335. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
336. //记录生成
337. List list = query.list();
338. //把查询到的结果放入迭代器
339. iterator = list.iterator();
340. return iterator;
341. }
342.
343. /** *//**
344. * 通过注册名取得记录列表.[表中要有 registername字段]
345. * @param registername String
346. * @return Iterator
347. */
348. public Iterator getInfosByRegisterName(String registerName, int position,
349. int length) throws Exception {
350. Iterator iterator = null;
351. String queryString =
352. " select info from Info as info where info.registername='" +
353. registerName + "' order by info.id desc";
354. //创建查询
355. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
356. //设置游标的起始点
357. query.setFirstResult(position);
358. //设置游标的长度
359. query.setMaxResults(length);
360. //记录生成
361. List list = query.list();
362. //把查询到的结果放入迭代器
363. iterator = list.iterator();
364. return iterator;
365. }
366.
367. ////////////////////////////////////////////////////////
368. ///// ///
369. ///// 以下部份表中要有特定字段才能正确运行 树型版块 ///
370. ///// ///
371. ////////////////////////////////////////////////////////
372.
373. /** *//**
374. * 取记录总数.[ 表中要有 board_id 字段]
375. * @return int
376. * @param String boardId
377. */
378. public int getInfosCountByBoard(String boardId) throws Exception {
379. int count = 0;
380.
381. count = ((Integer) getHibernateTemplate().iterate(
382. "select count(*) from Info as info where info.boardId = '" + boardId +
383. "'").next()).intValue();
384.
385. return count;
386. }
387.
388. /** *//**
389. * 通过版块名取得所有记录集合.[表中要有 board_id字段]
390. * @param BoardId String
391. * @return Iterator
392. */
393. public Iterator getAllInfosByBoard(String boardId) throws Exception {
394. Iterator iterator = null;
395. String queryString = " select info from Info as info where info.boardId='" +
396. boardId + "' order by info.id desc";
397. //创建查询
398. Query query = getHibernateTemplate().createQuery(getSession(), queryString);
399. //记录生成
400. List list = query.list();
401. //把查询到的结果放入迭代器
402. iterator = list.iterator();
403. return iterator;