[b]IGenericDao Class[/b]
[b]JpaGenericDao Class[/b]
[b]
具体实现类 MagazineDaoImpl 实现起来非常简单 [/b]
public interface IGenericDao<T, ID extends Object> {
public T load(ID id) throws DataAccessException;
public T get(ID id) throws DataAccessException;
public boolean contains(T t) throws DataAccessException;
public void refresh(T t, LockMode lockMode) throws DataAccessException;
public void refresh(T t) throws DataAccessException;
public void save(T t) throws DataAccessException;
public void saveOrUpdate(T t) throws DataAccessException;
public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException;
public void update(T t, LockMode lockMode) throws DataAccessException;
public void update(T t) throws DataAccessException;
// public void delete(T t, LockMode lockMode) throws DataAccessException;
public void delete(ID t) throws DataAccessException;
public void deleteAll(Collection<T> entities) throws DataAccessException;
public List<T> find(String queryString, Object value) throws DataAccessException;
public List<T> find(String queryString, Object[] values) throws DataAccessException;
public List<T> find(String queryString) throws DataAccessException;
public List<T> list() throws DataAccessException;
// public List<T> findByNamedQuery(String queryName) throws DataAccessException;
//
// public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException;
public List<T> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
// public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,
// final int startIndex);
//
// public PaginationSupport findPageByQuery(final String hql, final String countHql, final int pageSize,
// final int startIndex);
}
[b]JpaGenericDao Class[/b]
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;
import org.hibernate.LockMode;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.support.JpaDaoSupport;
@SuppressWarnings("unchecked")
public class JPAGenericDao<T, ID extends Object> extends JpaDaoSupport implements IGenericDao<T, ID> {
protected Class<T> entityClass;
public JPAGenericDao() {
}
protected Class getEntityClass() {
if (entityClass == null) {
entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
logger.debug("T class = " + entityClass.getName());
}
return entityClass;
}
public boolean contains(T t) throws DataAccessException {
// TODO Auto-generated method stub
return this.getJpaTemplate().contains(t);
}
public void delete(ID id) throws DataAccessException {
// TODO Auto-generated method stub
// 使用Query删除对象
this.getJpaTemplate().remove(this.getJpaTemplate().find(getEntityClass(), id));
}
public void deleteAll(Collection<T> entities) throws DataAccessException {
// TODO Auto-generated method stub
}
public List<T> find(String queryString, Object value)
throws DataAccessException {
// TODO Auto-generated method stub
return this.getJpaTemplate().find(queryString, value);
}
public List<T> find(String queryString, Object[] values)
throws DataAccessException {
// TODO Auto-generated method stub
List<T> find = (List<T>)this.getJpaTemplate().find(queryString, values);
return find;
}
public List<T> find(String queryString) throws DataAccessException {
// TODO Auto-generated method stub
List<T> find = (List<T>)this.getJpaTemplate().find(queryString);
return find;
}
public T get(Object id) throws DataAccessException {
// TODO Auto-generated method stub
return null;
}
public List<T> list() throws DataAccessException {
// TODO Auto-generated method stub
return this.getJpaTemplate().getEntityManager().createQuery("select all from " + this.getEntityClass().getSimpleName() + " all ").getResultList();
}
public T load(Object id) throws DataAccessException {
// TODO Auto-generated method stub
return (T)this.getJpaTemplate().find(this.getEntityClass(), id);
}
public void refresh(T t, LockMode lockMode) throws DataAccessException {
// TODO Auto-generated method stub
}
public void refresh(T t) throws DataAccessException {
// TODO Auto-generated method stub
this.getJpaTemplate().refresh(t);
}
public void save(T t) throws DataAccessException {
// TODO Auto-generated method stub
this.getJpaTemplate().persist(t);
}
public void saveOrUpdate(T t) throws DataAccessException {
// TODO Auto-generated method stub
}
public void saveOrUpdateAll(Collection entities) throws DataAccessException {
// TODO Auto-generated method stub
}
public void update(T t, LockMode lockMode) throws DataAccessException {
// TODO Auto-generated method stub
}
public void update(T t) throws DataAccessException {
// TODO Auto-generated method stub
this.getJpaTemplate().merge(t);
}
public List<T> findByNamedQuery(String queryName, Object... values)
throws DataAccessException {
// TODO Auto-generated method stub
return (List<T>)this.getJpaTemplate().findByNamedQuery(queryName, values);
}
}
[b]
具体实现类 MagazineDaoImpl 实现起来非常简单 [/b]
public class MagazineDaoImpl extends JPAGenericDao<Magazine,Magazine.MagazineId> implements MagazineDao