-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaDao-Hibernate3.template
executable file
·92 lines (79 loc) · 2.92 KB
/
JavaDao-Hibernate3.template
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
package dao.impl;
import dao.BaseDao;
import org.hibernate.*;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import java.io.Serializable;
import java.util.List;
/**
* @Created with Atom
* @author @author@
* @time @now@
* @description
* BaseDaoHibernate3工具类
*
**/
public class BaseDaoHibernate3<T> extends HibernateDaoSupport
implements BaseDao<T> {
public T get(Class<T> entityClazz, Serializable id) {
return(getHibernateTemplate().get(entityClazz, id));
}
public Serializable save(T entity) {
return(getHibernateTemplate().save(entity));
}
public void update(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
}
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
public void delete(Class<T> entityClazz, Serializable id) {
delete(get(entityClazz, id));
}
@Override
@SuppressWarnings ("unchecked")
public List<T> findAll(Class<T> entityClazz) {
return((List<T> )getHibernateTemplate().find("select en from "
+ entityClazz.getSimpleName() + " en"));
}
@Override
@SuppressWarnings ("unchecked")
public long findCount(Class<T> entityClazz) {
List<Long> list = (List<Long> )getHibernateTemplate().find(
"select count(*) from " + entityClazz.getSimpleName() + " en");
return(list.get(0));
}
@SuppressWarnings ("unchecked")
protected List<T> findByPage(final String hql,
final int pageNo, final int pageSize) {
List<T> list = getHibernateTemplate()
.execute(new HibernateCallback<List<T> >() {
public List<T> doInHibernate(Session session) {
List<T> result = session.createQuery(hql)
.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
return(result);
}
});
return(list);
}
@SuppressWarnings ("unchecked")
protected List<T> findByPage(final String hql, final int pageNo,
final int pageSize, final Object ... params) {
List<T> list = getHibernateTemplate()
.execute(new HibernateCallback<List<T> >() {
public List<T> doInHibernate(Session session) {
Query query = session.createQuery(hql);
for (int i = 0, len = params.length; i < len; i++) {
query.setParameter(i + "", params[i]);
}
List<T> result = query.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
return(result);
}
});
return(list);
}
}