1 package com.cy.utils;
2
3 import java.io.Serializable;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.Query;
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.Transaction;
14 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
15 import org.hibernate.cfg.Configuration;
16 import org.hibernate.service.ServiceRegistry;
17
18 /**
19 * Hibernate辅助类
20 *
21 * @author renchang
22 *
23 */
24 public class HibernateUtils {
25
26 private static Configuration config; // 配置对象
27 private static ServiceRegistry ser; // 服务
28 private static SessionFactory sf; // session工厂
29 private static Session session; // session
30 private static Transaction transaction; // 事务
31
32 /**
33 * 1.读取配置到Configuration 2.注册服务 3.获得SessionFactory
34 */
35 static {
36 config = new Configuration().configure();
37 ser = new StandardServiceRegistryBuilder().applySettings(
38 config.getProperties()).build();
39 sf = config.buildSessionFactory(ser);
40 }
41
42 /**
43 * 获得Session
44 *
45 * @return session对象
46 */
47 public static Session getSession() {
48 return session = sf.openSession();
49 }
50
51 /**
52 * 获得事务对象
53 *
54 * @return Transaction对象
55 */
56 public static Transaction getTransaction() {
57 return transaction = session.beginTransaction();
58 }
59
60 /**
61 * 关闭资源
62 */
63 public static void close() {
64 try {
65 session.close();
66 } catch (HibernateException e) {
67 e.printStackTrace();
68 }
69 }
70
71 /**
72 * 添加新数据
73 *
74 * @param obj
75 * 待添加的对象
76 */
77 public static void save(Object obj) {
78 try {
79 getSession();
80 getTransaction();
81 session.save(obj);
82 transaction.commit();
83 } catch (Exception e) {
84 e.printStackTrace();
85 transaction.rollback();
86 } finally {
87 close();
88 }
89 }
90
91 /**
92 * 修改数据
93 *
94 * @param obj
95 * 待修改的对象
96 */
97 public static void update(Object obj) {
98 try {
99 getSession();
100 getTransaction();
101 session.update(obj);
102 transaction.commit();
103 } catch (Exception e) {
104 e.printStackTrace();
105 transaction.rollback();
106 } finally {
107 close();
108 }
109 }
110
111 /**
112 * 添删除数据
113 *
114 * @param obj
115 * 待删除的对象
116 */
117 public static void delete(Object obj) {
118 try {
119 getSession();
120 getTransaction();
121 session.delete(obj);
122 transaction.commit();
123 } catch (Exception e) {
124 e.printStackTrace();
125 transaction.rollback();
126 } finally {
127 close();
128 }
129 }
130
131 /**
132 * 按条件插叙数据
133 *
134 * @param hql
135 * 插叙的hql语句
136 * @param whereMap
137 * 插叙条件的键值对
138 * @return 满足条件的对象集合
139 */
140 public static List<?> find(String hql, Map<String, Object> whereMap) {
141 List<?> objList = null;
142 try {
143 getSession();
144 getTransaction();
145 getTransaction();
146 hql = setHql(hql, whereMap);
147 Query query = session.createQuery(hql);
148 objList = query.list();
149 transaction.commit();
150 } catch (Exception e) {
151 e.printStackTrace();
152 transaction.rollback();
153 } finally {
154 close();
155 }
156 return objList;
157 }
158
159 /**
160 * 根据id获得对象信息
161 *
162 * @param cls
163 * 对象z
164 * @param id
165 * id
166 * @return 对象
167 */
168 public static Object getObjById(Class<?> cls, Serializable id) {
169 Object obj = null;
170 try {
171 getSession();
172 getTransaction();
173 obj = session.get(cls, id);
174 transaction.commit();
175 } catch (Exception e) {
176 e.printStackTrace();
177 transaction.rollback();
178 } finally {
179 close();
180 }
181 return obj;
182 }
183
184 /**
185 * 分页
186 *
187 * @param hql
188 * 分页语句
189 * @param pageSize
190 * 每页大小
191 * @param pageNo
192 * 当前页数
193 * @param whereMap
194 * 分页条件
195 * @return 满足条件的数据
196 */
197 public static List<?> paging(String hql, int pageSize, int pageNo,
198 Map<String, Object> whereMap) {
199 List<?> objList = null;
200 try {
201 getSession();
202 getTransaction();
203 getTransaction();
204 hql = setHql(hql, whereMap);
205 Query query = session.createQuery(hql);
206 int beginRow = (pageNo - 1) * pageSize;
207 query.setFirstResult(beginRow);
208 query.setMaxResults(pageSize);
209 objList = query.list();
210 transaction.commit();
211 } catch (Exception e) {
212 e.printStackTrace();
213 transaction.rollback();
214 } finally {
215 close();
216 }
217 return objList;
218 }
219
220 /**
221 * 获得条件下总条数
222 *
223 * @param hql
224 * hql语句
225 * @param whereMap
226 * 统计的总条数
227 * @return 总条数
228 */
229 public static int getTotalRows(String hql, Map<String, Object> whereMap) {
230 int totalRows = 0;
231 try {
232 getSession();
233 getTransaction();
234 hql = setHql(hql, whereMap);
235 Query query = session.createQuery(hql);
236 totalRows = (new Integer(query.uniqueResult().toString()))
237 .intValue();
238 } catch (Exception e) {
239 e.printStackTrace();
240 } finally {
241 close();
242 }
243 return totalRows;
244 }
245
246 /**
247 * 拼接hql语句
248 *
249 * @param hql
250 * 原始hql语句
251 * @param whereMap
252 * 拼接的条件
253 * @return 拼接后的hql
254 */
255 private static String setHql(String hql, Map<String, Object> whereMap) {
256 StringBuffer sbHql = new StringBuffer(hql);
257 if (whereMap != null) {
258 Set<String> set = whereMap.keySet();
259 Iterator<String> it = set.iterator();
260 while (it.hasNext()) {
261 String key = it.next();
262 sbHql.append(" AND " + key + " LIKE '%" + whereMap.get(key)
263 + "%'");
264 }
265 }
266 return sbHql.toString();
267 }
268 }