基于 Open Jpa 的 GenericDao 的实现

[b]IGenericDao Class[/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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值