复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319package com.fjk.commonbasedao; import java.util.List; import java.util.Map; import com.fjk.util.QueryCriteria; /** * 通用操作接口 * * @author * */ public interface CommonDao { /** * 保存对象 * * @param * @return */ public <T> Integer save(T entity); /** * 删除对象 * * @param * @return */ public <T> Integer delete(T entity); /** * 更新对象 * * @param * @return */ public <T> Integer update(T entity); /** * 保存、更新对象 * * @param * @return */ public <T> Integer saveOrUpdate(T entity); /** * 通过id来查找某一个对像 * * @param * @param * @return */ public <T> T findById(Class<T> entityClass, String id); /** * 通过HQL语句或者实体类类名查找某类对象 * * @param * @param * @return */ public <T> List<T> findByHql(Class<T> entityClass, String HQL); /** * 多条件查询 目前仅限于单表条件查询 * * @param * @return * entity封装了查询条件 */ public <T> List<T> findByObj(T entity); /** * 返回单个Object * @param CriteriaOne * @param CriteriaTow * @return */ public Object findOneObject(Map<String,Object>parameter ,String HQL); /** * 查询数据返回一个集合 * @param CriteriaOne * @param CriteriaTow * @return */ public <T> List<T> findMany(Map<String,Object>parameter,String HQL); /** * 返回Integer * @param CriteriaOne * @param CriteriaTow * @return */ public Integer findManyInteger(Map<String,Object>parameter,String HQL); /** * 通过QueryCriteria封装的查询条件查询的 * @param criteria * @return List<T> */ public <T> List<T> findManyByQueryCriteria(Map<String,Object>parameter,String HQL); } 实现类 package com.fjk.commonbasedaoimpl; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Component; import com.fjk.commonbasedao.CommonDao; import com.fjk.util.QueryCriteria; /** * 通用操作实现类 * * @author * */ @Component("commoDao") public class CommonDaoImpl implements CommonDao { @Resource(name = "sessionFactory") private SessionFactory getMySessionFactory; private Session getSession() { return getMySessionFactory.openSession(); } /** * 添加 */ public <T> Integer save(T entity) { try { this.getSession().save(entity); return 1; } catch (Exception e) { e.printStackTrace(); return -1; } } /** * 删除 */ public <T> Integer delete(T entity) { try { this.getSession().delete(entity); return 1; } catch (Exception e) { e.printStackTrace(); return -1; } } /** * 更新 */ public <T> Integer update(T entity) { try { this.getSession().update(entity); return 1; } catch (Exception e) { e.printStackTrace(); return -1; } } /** * 保存或者更新 */ public <T> Integer saveOrUpdate(T entity) { try { this.getSession().saveOrUpdate(entity); return 1; } catch (Exception e) { e.printStackTrace(); return -1; } } /** * 根据Id查询某个对象 */ @SuppressWarnings("unchecked") public <T> T findById(Class<T> entityClass, String id) { try { return (T) this.getSession().get(entityClass, id); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根据Bean的全类名查询数据或者HQL语句查询数据据 */ @SuppressWarnings("unchecked") public <T> List<T> findByHql(Class<T> entityClass, String hql) { try { if (hql == null || hql.equals("")) { return (List<T>) this.getSession() .createQuery("from " + entityClass.getName()).list(); } return (List<T>) this.getSession().createQuery(hql).list(); } catch (DataAccessException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根据根据条件查询数据 */ @SuppressWarnings("unchecked") public <T> List<T> findByObj(T entity) { // 保存查询条件中的属性名称 List<Object> fieldName = new ArrayList<Object>(); // 保存查询添加中的属性对应值 List<Object> fieldValue = new ArrayList<Object>(); try { Class clazz = entity.getClass(); // 获得对应Class对象 Field[] fields = entity.getClass().getDeclaredFields();// 获得对应Class所声明的所有字段 // 遍历所有字段属性 for (Field field : fields) { // 获得对应get方法 Method getMethod = null; try { // 获取不到对应get方法则跳到下个属性 getMethod = new PropertyDescriptor(field.getName(), clazz) .getReadMethod(); } catch (IntrospectionException e) { e.getMessage(); continue; } Object val = getMethod.invoke(entity, null); if (val != null) { fieldName.add(field.getName()); fieldValue.add(val); } } // 根据获取到的查询条件拼接查询语句 StringBuffer hql = new StringBuffer(); hql.append("from ").append(clazz.getName()).append(" a where 1=1 "); for (int i = 0; i < fieldName.size(); i++) { String name = fieldName.get(i).toString(); String val = fieldValue.get(i).toString(); hql.append(" and a.").append(name).append("=") .append("'" + val + "'"); System.out.println(name + "t" + val); } System.out.println(hql); return this.getSession().createQuery(hql.toString()).list(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 返回单个查询结果对象 */ @Override public Object findOneObject(Map<String, Object> parameter, String HQL) { Query query=this.getSession().createQuery(HQL); String[] pamarsName = query.getNamedParameters(); for (int i = 0; i < pamarsName.length; i++) { query.setParameter(pamarsName[i], parameter.get(pamarsName[i])); } return query.uniqueResult(); } /** * 返回多个查询结果对象 */ @SuppressWarnings("unchecked") @Override public <T> List<T> findMany(Map<String, Object> parameter, String HQL) { Query query=this.getSession().createQuery(HQL); String[] pamarsName = query.getNamedParameters();//获取参数列表 for (int i = 0; i < pamarsName.length; i++) { query.setParameter(pamarsName[i], parameter.get(pamarsName[i])); } return query.list(); } @Override public Integer findManyInteger(Map<String, Object> parameter, String HQL) { // TODO Auto-generated method stub return null; } /** * 多条件查询 */ @Override public <T> List<T> findManyByQueryCriteria(Map<String, Object> parameter,String HQL) { // TODO Auto-generated method stub return null; } }
Hibernate通用的增删改查代码
最后
以上就是现实河马最近收集整理的关于Hibernate通用的曾删改查代码的全部内容,更多相关Hibernate通用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复