1 package com.cnblogs.yjmyzz.dao.impl;
2
3 import com.cnblogs.yjmyzz.dao.BaseDAO;
4
5 import java.io.Serializable;
6 import java.util.List;
7
8 import org.hibernate.Query;
9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Component;
13 import org.springframework.stereotype.Repository;
14
15 @Component("baseDAO")
16 @SuppressWarnings("all")
17 public class BaseDAOImpl<T> implements BaseDAO<T> {
18
19 private SessionFactory sessionFactory;
20
21 public SessionFactory getSessionFactory() {
22 return sessionFactory;
23 }
24
25 @Autowired
26 public void setSessionFactory(SessionFactory sessionFactory) {
27 this.sessionFactory = sessionFactory;
28 }
29
30 private Session getCurrentSession() {
31 return sessionFactory.getCurrentSession();
32 }
33
34 public Serializable save(T o) {
35 return this.getCurrentSession().save(o);
36 }
37
38 public void delete(T o) {
39 this.getCurrentSession().delete(o);
40 }
41
42 public void update(T o) {
43 this.getCurrentSession().update(o);
44 }
45
46 public void saveOrUpdate(T o) {
47 this.getCurrentSession().saveOrUpdate(o);
48 }
49
50 public List<T> find(String hql) {
51 return this.getCurrentSession().createQuery(hql).list();
52 }
53
54 public List<T> find(String hql, Object[] param) {
55 Query q = this.getCurrentSession().createQuery(hql);
56 if (param != null && param.length > 0) {
57 for (int i = 0; i < param.length; i++) {
58 q.setParameter(i, param[i]);
59 }
60 }
61 return q.list();
62 }
63
64 public List<T> find(String hql, List<Object> param) {
65 Query q = this.getCurrentSession().createQuery(hql);
66 if (param != null && param.size() > 0) {
67 for (int i = 0; i < param.size(); i++) {
68 q.setParameter(i, param.get(i));
69 }
70 }
71 return q.list();
72 }
73
74 public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
75 if (page == null || page < 1) {
76 page = 1;
77 }
78 if (rows == null || rows < 1) {
79 rows = 10;
80 }
81 Query q = this.getCurrentSession().createQuery(hql);
82 if (param != null && param.length > 0) {
83 for (int i = 0; i < param.length; i++) {
84 q.setParameter(i, param[i]);
85 }
86 }
87 return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
88 }
89
90 public List<T> find(String hql, List<Object> param, Integer page,
91 Integer rows) {
92 if (page == null || page < 1) {
93 page = 1;
94 }
95 if (rows == null || rows < 1) {
96 rows = 10;
97 }
98 Query q = this.getCurrentSession().createQuery(hql);
99 if (param != null && param.size() > 0) {
100 for (int i = 0; i < param.size(); i++) {
101 q.setParameter(i, param.get(i));
102 }
103 }
104 return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
105 }
106
107 public T get(Class<T> c, Serializable id) {
108 return (T) this.getCurrentSession().get(c, id);
109 }
110
111 public T get(String hql, Object[] param) {
112 List<T> l = this.find(hql, param);
113 if (l != null && l.size() > 0) {
114 return l.get(0);
115 } else {
116 return null;
117 }
118 }
119
120 public T get(String hql, List<Object> param) {
121 List<T> l = this.find(hql, param);
122 if (l != null && l.size() > 0) {
123 return l.get(0);
124 } else {
125 return null;
126 }
127 }
128
129 public Long count(String hql) {
130 return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
131 }
132
133 public Long count(String hql, Object[] param) {
134 Query q = this.getCurrentSession().createQuery(hql);
135 if (param != null && param.length > 0) {
136 for (int i = 0; i < param.length; i++) {
137 q.setParameter(i, param[i]);
138 }
139 }
140 return (Long) q.uniqueResult();
141 }
142
143 public Long count(String hql, List<Object> param) {
144 Query q = this.getCurrentSession().createQuery(hql);
145 if (param != null && param.size() > 0) {
146 for (int i = 0; i < param.size(); i++) {
147 q.setParameter(i, param.get(i));
148 }
149 }
150 return (Long) q.uniqueResult();
151 }
152
153 public Integer executeHql(String hql) {
154 return this.getCurrentSession().createQuery(hql).executeUpdate();
155 }
156
157 public Integer executeHql(String hql, Object[] param) {
158 Query q = this.getCurrentSession().createQuery(hql);
159 if (param != null && param.length > 0) {
160 for (int i = 0; i < param.length; i++) {
161 q.setParameter(i, param[i]);
162 }
163 }
164 return q.executeUpdate();
165 }
166
167 public Integer executeHql(String hql, List<Object> param) {
168 Query q = this.getCurrentSession().createQuery(hql);
169 if (param != null && param.size() > 0) {
170 for (int i = 0; i < param.size(); i++) {
171 q.setParameter(i, param.get(i));
172 }
173 }
174 return q.executeUpdate();
175 }
176
177 }