2e2ea7d297144988c859aff24fdd881cf78384ba.svn-base
5.8 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
package com.espeed.yxy.dao.impl;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.espeed.yxy.dao.HibernateBaseDAO;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: hbernate常用方法(营销邮)
* 开始时间: 2013.11.27
* 程 序 员: 谢勇
* 最后修改:
* 备 注: 如需修改请通知程序员
*/
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 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);
}
/*
* -------------------------------查询总记录数-----------------------------------------
*/
/**根据条件查询总记录*/
public int count(String hql) throws Exception {
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());
}
/**hql语句修改删除记录*/
public int updateorDelByHql(String hql){
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
Query query = session.createQuery(hql);
return query.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally{
if(session!=null){
session.clear();
session.close();
}
}
}
/**sql语句修改删除记录*/
public int updateorDelBySql(String sql)throws Exception{
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
Query query = session.createSQLQuery(sql);
return query.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally{
if(session!=null){
session.clear();
session.close();
}
}
}
/**查询设定的记录条数据*/
public List<T> findBySet(String hql,int num)throws Exception{
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
Query query = session.createQuery(hql);
query.setMaxResults(num);
return query.list();
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
if(session!=null){
session.clear();
session.close();
}
}
}
/**批量插入*/
public void addPi(List<T> o){
if(o!=null){
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
for(int i=0;i<o.size();i++){
session.saveOrUpdate(o.get(i));
}
session.beginTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
session.flush();
session.clear();
session.close();
}
}
}
/**批量更新*/
public void updatePi(List<T> o) throws Exception{
if(o!=null){
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
for(int i=0;i<o.size();i++){
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 saveandupdatePi(List<T> o) throws Exception{
if(o!=null){
Session session = super.getHibernateTemplate().getSessionFactory().openSession();
try {
session.beginTransaction();
for(int i=0;i<o.size();i++){
session.saveOrUpdate(o.get(i));
}
session.beginTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
session.flush();
session.clear();
session.close();
}
}
}
/**sql语句查询*/
public List 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;
}
}