Hiberna
teDaoSuppor
t支持。具体代码如下。
IGenericDAO接口,这个接口与前一个帖子是一致的:
- public interface IGenericDAO〈T, ID extends Serializable〉 {
-
-
- T findById(ID id, boolean lock);
-
-
- List〈T〉 findAll();
-
-
- List〈T〉 findByExample(T exampleInstance);
-
-
- T makePersistent(T entity);
-
-
- void makeTransient(T entity);
- }
public interface IGenericDAO〈T, ID extends Serializable〉 {
// 通过ID检索对应的实体对象
T findById(ID id, boolean lock);
// 列出所有的实体对象
List〈T〉 findAll();
// 通过Example方法检索实体对象
List〈T〉 findByExample(T exampleInstance);
// 产生持久化一个实体对象
T makePersistent(T entity);
// 产生一个游离对象
void makeTransient(T entity);
}
下面是针对IGenericDAO的实现,该类继承自Spring的Hiberna
teDaoSuppor
t,代码如下:
- public abstract class GenericDAOHibernate〈T, ID extends Serializable, DAOImpl extends IGenericDAO〈T, ID〉〉
- extends HibernateDaoSupport implements IGenericDAO〈T, ID〉 {
- private Class〈T〉 persistentClass;
-
- public GenericDAOHibernate() {
- this.persistentClass = (Class〈T〉) ((ParameterizedType) getClass()
- .getGenericSuperclass()).getActualTypeArguments()[0];
- }
-
- public Class〈T〉 getPersistentClass() {
- return persistentClass;
- }
-
- @SuppressWarnings("unchecked")
- public T findById(ID id) {
- return (T) getHibernateTemplate().load(getPersistentClass(), id);
- }
-
- @SuppressWarnings("unchecked")
- public T findById(ID id, boolean lock) {
- T entity;
- if (lock)
- entity = (T) getHibernateTemplate().load(getPersistentClass(), id,
- LockMode.UPGRADE);
- else
- entity = findById(id);
-
- return entity;
- }
-
- public List〈T〉 findAll() {
- return findByCriteria();
- }
-
- @SuppressWarnings("unchecked")
- public List〈T〉 findByExample(T exampleInstance) {
- DetachedCriteria detachedCrit = DetachedCriteria
- .forClass(getPersistentClass());
- Example example = Example.create(exampleInstance);
- detachedCrit.add(example);
- return getHibernateTemplate().findByCriteria(detachedCrit);
- }
-
- @SuppressWarnings("unchecked")
- public List〈T〉 findByExample(T exampleInstance, String[] excludeProperty) {
- DetachedCriteria detachedCrit = DetachedCriteria
- .forClass(getPersistentClass());
- Example example = Example.create(exampleInstance);
- for (String exclude : excludeProperty) {
- example.excludeProperty(exclude);
- }
- detachedCrit.add(example);
- return getHibernateTemplate().findByCriteria(detachedCrit);
- }
-
- public T makePersistent(T entity) {
- getHibernateTemplate().saveOrUpdate(entity);
- return entity;
- }
-
- public void makeTransient(T entity) {
- getHibernateTemplate().delete(entity);
- }
-
- @SuppressWarnings("unchecked")
- protected List〈T〉 findByCriteria(Criterion... criterion) {
- DetachedCriteria detachedCrit = DetachedCriteria
- .forClass(getPersistentClass());
- for (Criterion c : criterion) {
- detachedCrit.add(c);
- }
- return getHibernateTemplate().findByCriteria(detachedCrit);
- }
- }
public abstract class GenericDAOHibernate〈T, ID extends Serializable, DAOImpl extends IGenericDAO〈T, ID〉〉
extends HibernateDaoSupport implements IGenericDAO〈T, ID〉 {
private Class〈T〉 persistentClass;
public GenericDAOHibernate() {
this.persistentClass = (Class〈T〉) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class〈T〉 getPersistentClass() {
return persistentClass;
}
@SuppressWarnings("unchecked")
public T findById(ID id) {
return (T) getHibernateTemplate().load(getPersistentClass(), id);
}
@SuppressWarnings("unchecked")
public T findById(ID id, boolean lock) {
T entity;
if (lock)
entity = (T) getHibernateTemplate().load(getPersistentClass(), id,
LockMode.UPGRADE);
else
entity = findById(id);
return entity;
}
public List〈T〉 findAll() {
return findByCriteria();
}
@SuppressWarnings("unchecked")
public List〈T〉 findByExample(T exampleInstance) {
DetachedCriteria detachedCrit = DetachedCriteria
.forClass(getPersistentClass());
Example example = Example.create(exampleInstance);
detachedCrit.add(example);
return getHibernateTemplate().findByCriteria(detachedCrit);
}
@SuppressWarnings("unchecked")
public List〈T〉 findByExample(T exampleInstance, String[] excludeProperty) {
DetachedCriteria detachedCrit = DetachedCriteria
.forClass(getPersistentClass());
Example example = Example.create(exampleInstance);
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
detachedCrit.add(example);
return getHibernateTemplate().findByCriteria(detachedCrit);
}
public T makePersistent(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
return entity;
}
public void makeTransient(T entity) {
getHibernateTemplate().delete(entity);
}
@SuppressWarnings("unchecked")
protected List〈T〉 findByCriteria(Criterion... criterion) {
DetachedCriteria detachedCrit = DetachedCriteria
.forClass(getPersistentClass());
for (Criterion c : criterion) {
detachedCrit.add(c);
}
return getHibernateTemplate().findByCriteria(detachedCrit);
}
}
实际上这个GenericDAOHiberna
te类还是比较简单的,主要通过Spring的ge
tHiberna
te
Templa
te()方法来实现通用的CRUD操作。
在上一个帖子中有朋友问到,使用了范型的DAO类后,如何通过Spring来注入这个DAO类,实际上,注入DAO类与不使用范型是一样的,比如说对于上一个帖子的UserDAOHiberna
te的注入:
- "userDao"
- class="org.rainlife.dao.hibernate.UserDaoHibernate"
- autowire="byName"/>
te.UserDaoHibernate"
autowire="byName"/>
这样就完成了对userDAO的注入。
阅读(1415) | 评论(0) | 转发(0) |