fc77bcba64238a34b1a7a8052c3eb6de07f4b6fd.svn-base
10.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.espeed.dao.impl;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.espeed.dao.HibernateBaseDAO;
import com.espeed.pojo.YxyReadingCount;
import com.espeed.pojo.YxySendMailMasterBase;
import com.espeed.pojo.YxyUserAddress;
import com.espeed.tool.DBUtil;
import com.espeed.vo.PageBean;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: 通用dao操作接口实现类
* 开始时间: 2013.09.22
* 程 序 员: 谢勇
* 最后修改: 2014-02-26
* 备 注: 实现通用的增删改查操作,被其他dao所继承使用
*/
public abstract class HibernateBaseDAOImpl<T extends Serializable ,Pk extends Serializable> extends HibernateDaoSupport
implements HibernateBaseDAO<T, Pk> {
private Connection conn;
private PreparedStatement pst;
private ResultSet rs;
/**增加记录 (返回新增加记录的主键)*/
public int add(T o) throws Exception {
return (Integer) super.getHibernateTemplate().save(o);
}
/**增加记录(无返回值)*/
public void adddate(T o) throws Exception {
super.getHibernateTemplate().save(o);
}
/**修改记录*/
public void update(T o) throws Exception {
super.getHibernateTemplate().update(o);
}
/**删除记录*/
public void del(T o) throws Exception {
super.getHibernateTemplate().delete(o);
}
/**添加或更新*/
public void saveOrUpdate(T o) throws Exception {
super.getHibernateTemplate().saveOrUpdate(o);
}
/**根据ID获取一条数据*/
@SuppressWarnings("unchecked")
public T get(Class<T> t,Pk pk) throws Exception {
return (T) super.getHibernateTemplate().get(t, pk);
}
/**根据ID获取一条数据*/
@SuppressWarnings("unchecked")
public T load(Class<T> t,Pk pk) throws Exception {
return (T) super.getHibernateTemplate().load(t, pk);
}
/**根据hql进行条件查询*/
@SuppressWarnings("unchecked")
public List<T> getAll(String hql) throws Exception {
return super.getHibernateTemplate().find(hql);
}
/**条件查询*/
@SuppressWarnings("unchecked")
public List<T> getAll(String whereHql, Object... params) throws Exception {
return super.getHibernateTemplate().find(whereHql,params);
}
/**
* ------------------------------分页查询-----------------------------------------
*/
/** 根据指定的HQL(多条件)进行查询*/
public List<T> findByPage(String hql, String hqlCount, PageBean pb)
throws Exception {
return this.findByPage(hql, hqlCount, pb, new Object[0]);
}
/** 根据指定的HQL(多条件)进行查询*/
@SuppressWarnings("unchecked")
public List<T> findByPage(final String hql,final String hqlCount,final PageBean pb,final Object... values)
throws Exception {
return (List<T>) super.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int pageSize = pb.getPageSize();
int currentPage = pb.getCurrentPage();
Query queryCount = session.createQuery(hqlCount);
Query query = session.createQuery(hql);
for(int i = 0;i<values.length;i++){
queryCount.setParameter(i, values[i]);
query.setParameter(i, values[i]);
}
//获取总记录数
Integer totalRecord = Integer.valueOf(queryCount.list().get(0).toString());
pb.setTotalRecord(totalRecord);
//获取总页数
Integer totalPage = (totalRecord + pageSize - 1)/pageSize;
pb.setTotalPage(totalPage);
query.setFirstResult((currentPage - 1)*pageSize);
query.setMaxResults(pageSize);
List list = query.list();
return list;
}
});
}
/** 根据指定的HQL(多条件)进行查询特殊(所有点读)*/
@SuppressWarnings("unchecked")
public List<T> findByPages(final String hql,final String hqlCount,final PageBean pb,final Object... values)
throws Exception {
return (List<T>) super.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int pageSize = pb.getPageSize();
int currentPage = pb.getCurrentPage();
Query queryCount = session.createQuery(hqlCount);
Query query = session.createQuery(hql);
for(int i = 0;i<values.length;i++){
queryCount.setParameter(i, values[i]);
query.setParameter(i, values[i]);
}
//获取总记录数
Integer totalRecord = queryCount.list().size();
pb.setTotalRecord(totalRecord);
//获取总页数
Integer totalPage = (totalRecord + pageSize - 1)/pageSize;
pb.setTotalPage(totalPage);
query.setFirstResult((currentPage - 1)*pageSize);
query.setMaxResults(pageSize);
List list = query.list();
return query.list();
}
});
}
/**根据条件查询总记录*/
public int count(String hql) throws Exception {
List result=super.getHibernateTemplate().find(hql);
if(result.size()>0&&result.get(0)!=null&&!result.get(0).equals("null")){
return Integer.valueOf(result.get(0).toString());
}else{
return 0;
}
//return Integer.valueOf(super.getHibernateTemplate().find(hql).get(0).toString());
}
/**根据条件和参数查询总记录*/
public int count(String hql, Object... params) throws Exception {
return Integer.valueOf(super.getHibernateTemplate().find(hql,params).get(0).toString());
}
/**根据ID通过jdbc删除数据*/
public void delByID(String table,String StrID,int id)throws Exception{
conn = DBUtil.getConnection();
pst = conn.prepareStatement("delete from "+table+" where "+ StrID+"=?");
pst.setInt(1, id);
pst.execute();
DBUtil.close(rs, pst, conn);
}
/**sql插入返回插入的id*/
public int insertTable(String sql)throws Exception{
conn = DBUtil.getConnection();
pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
int resultID=0;
pst.executeUpdate();
rs=pst.getGeneratedKeys();
if(rs.next()){
resultID=rs.getInt(1);
}
DBUtil.close(rs, pst, conn);
return resultID;
}
/**HQL语句修改删除记录*/
public int updateorDelByHql(String hql){
Query query = null;
int result=0;
Session session =null;
try {
session=super.getHibernateTemplate().getSessionFactory().openSession();
query = session.createQuery(hql);
result = query.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(session!=null){
session.clear();
session.close();
}
}
return result;
}
/**查询设定的记录条数据*/
public List<T> findBySet(String hql,int num){
Session session = null;
Query query=null;
try {
session=super.getHibernateTemplate().getSessionFactory().openSession();
query = session.createQuery(hql);
query.setMaxResults(num);
return query.list();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(session!=null){
session.clear();
session.close();
}
}
return null;
}
/**批量插入*/
public void addPi(List<T> o)throws Exception{
Session session =null;
if(o!=null){
session=super.getHibernateTemplate().getSessionFactory().openSession();
session.beginTransaction();
for(int i=0;i<o.size();i++){
session.saveOrUpdate(o.get(i));
if(i%10==0){
session.flush();
session.clear();
}
}
session.getTransaction().commit();
}
session.close();
}
/**sql语句查询*/
public List<Object> findBySql(String sql){
Session session=null;
try {
session = super.getHibernateTemplate().getSessionFactory().openSession();
return session.createSQLQuery(sql).list();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(session!=null){
session.clear();
session.close();
}
}
return null;
}
/**批量更新(统计表使用)*/
public void updatePi(List<YxyReadingCount> o){
if(o!=null){
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
String sql="";
for(int i=0;i<o.size();i++)
{
sql="update YxyReadingCount set reading_count_person="+o.get(i).getReading_count_person()+",reading_count_tol="+o.get(i).getReading_count_tol() +" where reading_count_mailuid='"+o.get(i).getReading_count_mailuid()+"'";
Query query = session.createQuery(sql);
query.executeUpdate();
//session.update(o.get(i));
}
session.beginTransaction().commit();
} catch (Exception e)
{
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
session.flush();
session.clear();
session.close();
}
}
}
/**批量更新(邮件基本信息)*/
public void updatePiBase(List<YxySendMailMasterBase> o){
if(o!=null){
Session session =super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
String sql="";
for(int i=0;i<o.size();i++)
{
sql="update YxySendMailMasterBase set reading_tol="+o.get(i).getReading_tol()+" where mail_uid='"+o.get(i).getMail_uid()+"'";
Query query = session.createQuery(sql);
query.executeUpdate();
}
session.beginTransaction().commit();
} catch (Exception e)
{
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
session.flush();
session.clear();
session.close();
}
}
}
/**批量更新(用户地址)*/
public void updatePiAddress(List<YxyUserAddress> o){
if(o!=null){
Session session =super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
String Hql="";
for(int i=0;i<o.size();i++)
{
Hql="update YxyUserAddress set user_send_time='"+o.get(i).getUser_send_time()+"',user_send_num="+o.get(i).getUser_send_num()+" where user_addr_id="+o.get(i).getUser_addr_id();
Query query = session.createQuery(Hql);
query.executeUpdate();
}
session.beginTransaction().commit();
} catch (Exception e)
{
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
session.flush();
session.clear();
session.close();
}
}
}
}