package scut.job.dao.hibernate.support;
002
003 import org.springframework.orm.hibernate3.HibernateCallback;
004 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
005 import org.hibernate.Session;
006 import org.hibernate.Query;
007 import org.hibernate.HibernateException;
008
009 import scut.job.util.Assert;
010
011 import java.sql.SQLException;
012 import java.util.List;
013
014 public class MyHibernateDaoSupport extends HibernateDaoSupport {
015
016 protected int MAX_BATCH_ROW = 20;
017
018 // 批量更新.返回更新影响的行数
019 public <T> int batchUpdate(final T[] array) {
020 int affectedRow = (Integer) getHibernateTemplate().execute(
021 new HibernateCallback() {
022 public Object doInHibernate(Session session)
023 throws HibernateException, SQLException {
024 for (int i = 0; i < array.length; ++i) {
025 session.update(array[i]);
026 if (i % MAX_BATCH_ROW == 0) {
027 session.flush();
028 session.clear();
029 }
030 }
031 session.flush();
032 session.clear();
033 return array.length;
034 }
035 });
036 return affectedRow;
037 }
038
039 // 批量保存.返回持久化影响的行数
040 public <T> int batchSave(final T[] array) {
041 int affectedRow = (Integer) getHibernateTemplate().execute(
042 new HibernateCallback() {
043 public Object doInHibernate(Session session)
044 throws HibernateException, SQLException {
045 for (int i = 0; i < array.length; ++i) {
046 session.save(array[i]);
047 if (i % MAX_BATCH_ROW == 0) {
048 session.flush();
049 session.clear();
050 }
051 }
052 session.flush();
053 session.clear();
054 return array.length;
055 }
056 });
057 return affectedRow;
058 }
059
060 // 批量删除.返回删除影响的行数
061 public <T> int batchDelete(final T[] array) {
062 int affectedRow = (Integer) getHibernateTemplate().execute(
063 new HibernateCallback() {
064 public Object doInHibernate(Session session)
065 throws HibernateException, SQLException {
066 for (int i = 0; i < array.length; ++i) {
067 session.delete(array[i]);
068 if (i % MAX_BATCH_ROW == 0) {
069 session.flush();
070 session.clear();
071 }
072 }
073 session.flush();
074 session.clear();
075 return array.length;
076 }
077 });
078 return affectedRow;
079 }
080
081 // 按主键批量删除.返回删除影响的行数
082 public <T> int batchDelete(final Class<T> entityClass, final Integer[] Ids) {
083 int affectedRow = (Integer) getHibernateTemplate().execute(
084 new HibernateCallback() {
085 public Object doInHibernate(Session session)
086 throws HibernateException, SQLException {
087 String hql = "delete from " + entityClass.getName()
088 + " a where a.id=?";
089 int count = 0;
090 for (int i = 0; i < Ids.length; ++i) {
091 Query delete = session.createQuery(hql)
092 .setParameter(0, Ids[i]);
093 count += delete.executeUpdate();
094 if (i % MAX_BATCH_ROW == 0) {
095 session.flush();
096 session.clear();
097 }
098 }
099 session.flush();
100 session.clear();
101 return count;
102 }
103 });
104 return affectedRow;
105 }
106
107 // 按主键删除.返回删除影响的行数
108 public <T> int delete(final Class<T> entityClass, final Integer id) {
109 int affectedRow = (Integer) getHibernateTemplate().execute(
110 new HibernateCallback() {
111 public Object doInHibernate(Session session)
112 throws HibernateException, SQLException {
113 String hql = "delete from " + entityClass.getName()
114 + " a where a.id=?";
115 return session.createQuery(hql).setParameter(0, id)
116 .executeUpdate();
117 }
118 });
119 return affectedRow;
120 }
121
122 // ------------------------------------------------------------------
123 // 统计方法
124 // ------------------------------------------------------------------
125
126 // hql,不要参数
127 public int count(final String hql) {
128 return count(hql, null);
129 }
130
131 // hql,一个参数
132 public int count(final String hql, final Object value) {
133 return count(hql, new Object[]{ value });
134 }
135
136 // hql,多个参数
137 public int count(final String hql, final Object[] values) {
138 int rowCount = (Integer) getHibernateTemplate().execute(
139 new HibernateCallback() {
140 public Object doInHibernate(Session session)
141 throws HibernateException, SQLException {
142 String hql2 = hql;
143 //截断order by
144 int endIndex = hql2.lastIndexOf("order by");
145 if (endIndex > 0){
146 hql2 = hql2.substring(0, endIndex);
147 }
148 Query query = session.createQuery(hql2);
149 if (values != null){
150 for (int i = 0; i < values.length; ++i) {
151 query.setParameter(i, values[i]);
152 }
153 }
154 return Integer.parseInt(query.list().get(0).toString());
155 }
156 });
157 return rowCount;
158 }
159
160 // ------------------------------------------------------------------
161 // 分页查询
162 // ------------------------------------------------------------------
163
164 // 查询一个类的所有记录
165 public <T> QueryResutl<T> findAll(final Class<T> entityClass,
166 final int offset, final int pageSize) {
167 final String hql = "from " + entityClass.getName();
168 List<T> list = findByPage(hql, offset, pageSize);
169 String hql2 = "select count(*) " + hql;
170 int totalRow = count(hql2);
171 return new QueryResutl<T>(list, totalRow);
172 }
173
174 // 查询的hql语句要以from开头
175 public <T> QueryResutl<T> queryAsFrom(final Class<T> entityClass,
176 final String hqlFrom, final int offset, final int pageSize) {
177 List<T> list = findByPage(hqlFrom, offset, pageSize);
178 String hql2 = "select count(*)" + hqlFrom;
179 int totalRow = count(hql2);
180 return new QueryResutl<T>(list, totalRow);
181 }
182
183 // 查询的hql可以是select开头或from开头
184 public <T> QueryResutl<T> query(final Class<T> entityClass,
185 final String hql, final int offset, final int pageSize) {
186 Assert.hasText(hql, "[Assertion failed] - this hql must have text; it must not be null, empty, or blank");
187 if (hql.trim().startsWith("from")) {
188 return queryAsFrom(entityClass, hql, offset, pageSize);
189 } else {
190 List<T> list = findByPage(hql, offset, pageSize);
191 int begin = hql.indexOf("from");
192 int end = hql.length();
193 String hql2 = "select count(*) from " + hql.substring(begin, end);
194 int totalRow = count(hql2);
195 return new QueryResutl<T>(list, totalRow);
196 }
197 }
198
199 // hql,不需要参数
200 public <T> List<T> findByPage(final String hql, final int offset,
201 final int pageSize) {
202 return findByPage(hql, null, offset, pageSize);
203 }
204
205 // hql,一个参数
206 public <T> List<T> findByPage(final String hql, final Object value,
207 final int offset, final int pageSize) {
208 return findByPage(hql, new Object[] { value }, offset, pageSize);
209 }
210
211 // hql,多个参数
212 @SuppressWarnings("unchecked")
213 public <T> List<T> findByPage(final String hql, final Object[] values,
214 final int offset, final int pageSize) {
215 // 通过一个HibernateCallback对象来执行查询
216 List<T> list = getHibernateTemplate().executeFind(
217 new HibernateCallback() {
218 // 实现HibernateCallback接口必须实现的方法
219 public Object doInHibernate(Session session)
220 throws HibernateException, SQLException {
221 // 执行Hibernate分页查询
222 Query query = session.createQuery(hql);
223 if (values != null) {
224 // 为hql语句传入参数
225 for (int i = 0; i < values.length; i++) {
226 query.setParameter(i, values[i]);
227 }
228 }
229 List<T> result = query.setFirstResult(offset)
230 .setMaxResults(pageSize).list();
231 return result;
232 }
233 });
234 return list;
235 }
236 }
DAO的一些便利方法
最新推荐文章于 2021-06-06 23:30:23 发布