struts2+jpa+spring 泛型版小结

  struts 2+jpa+spring 的,如果用上范型的话,还是能省不少事的哦.把以前的小结之.

 

首先搞个DAO的基础接口

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

public interface IBaseDAO<T> {

 public T find(Class<T> clazz, Serializable id);

 public void persist(T t);

 public void save(T t);

 public void delete(T t);

 public Query createQuery(String jpql);

 public int getTotalCount(String jpql);

 public List<T> find(String jpql);

 public List<T> find(String jpql, Object param);

 public List<T> find(String jpql, Object... param);

 public void saveOrUpdate(T t);
}

 

 dao层实现:
  

public class BaseDAOImpl<T extends BaseBean> implements IBaseDAO<T> {

 @PersistenceContext
 protected EntityManager em;

 public Query createQuery(String jpql) {
  return em.createQuery(jpql);
 }

 public void delete(T t) {
  em.remove(t);
 }

 public T find(Class<T> clazz, Serializable id) {
  return em.getReference(clazz, id);
 }

 @SuppressWarnings("unchecked")
 public List<T> find(String jpql, Object param) {
  return em.createQuery(jpql).setParameter(1, param).getResultList();
 }

 @SuppressWarnings("unchecked")
 public List<T> find(String jpql, Object[] param) {
  Query query = em.createQuery(jpql);
  for (int i = 1; i <= param.length; i++) {
   query.setParameter(i, param[i - 1]);
  }
  return query.getResultList();
 }

 @SuppressWarnings("unchecked")
 public List<T> find(String jpql) {
  return em.createQuery(jpql).getResultList();
 }

 @SuppressWarnings("unchecked")
 public int getTotalCount(String jpql) {
  return ((Long) em.createQuery(jpql).getSingleResult()).intValue();
 }

 public void persist(T t) {
  em.persist(t);
 }

 public void save(T t) {
  em.merge(t);
 }

 public void saveOrUpdate(T t) {
  em.merge(t);
 }

 public EntityManager getEntityManager() {
  return em;
 }

 @PersistenceContext
 public void setEntityManager(EntityManager entityManager) {
  this.em = entityManager;
 }

同样地,将CRUD的基本服务搞成一个baseservice
  

import java.io.Serializable;
import java.util.List;

import com.fengmanfei.blog.exception.BusinessException;

public interface IBaseService<T> {

 public void create(T t) throws BusinessException;

 public void save(T t) throws BusinessException;

 public void delete(T t) throws BusinessException;

 public T find(Class<T> clazz, Serializable id);

 public int getTotalCount(String jpql);

 public List<T> find(String jpql);

 public List<T> find(String jpql, Object param);

 public List<T> find(String jpql, Object... param);

}

公共service的实现
  

@Transactional
public class BaseServiceImpl<T extends BaseBean> implements IBaseService<T> {

 protected Log log = LogFactory.getLog(this.getClass());

 protected IBaseDAO<T> baseDAO;

 public void create(T t) throws BusinessException {
  baseDAO.persist(t);
 }

 public void delete(T t) throws BusinessException {
  t.setDeleted(true);
  baseDAO.save(t);
 }

 public void save(T t) throws BusinessException {
  baseDAO.save(t);
 }

 public T find(Class<T> clazz, Serializable id) {
  T t = baseDAO.find(clazz, id);
  if (t.isDeleted()) {
   return null;
  }
  return t;
 }

 public List<T> find(String jpql, Object param) {
  return baseDAO.find(jpql, param);
 }

 public List<T> find(String jpql, Object... param) {
  return baseDAO.find(jpql, param);
 }

 public List<T> find(String jpql) {
  return baseDAO.find(jpql);
 }

 public int getTotalCount(String jpql) {
  return (Integer) baseDAO.createQuery(jpql).getSingleResult();
 }

 public IBaseDAO<T> getBaseDAO() {
  return baseDAO;
 }

 public void setBaseDAO(IBaseDAO<T> baseDAO) {
  this.baseDAO = baseDAO;
 }

}

 

再来看具体的功能,比如person的部分,
首先是person的接口:
 

import com.fengmanfei.blog.entity.Person;

public interface IPersonService<T extends Person> extends IBaseService<T> {

 public T findPersonByAccount(String login);

 public T findPersonByAccountAndPassword(String login, String password);
}

 

person的实现:
  

public class PersonServiceImpl<T extends Person> extends BaseServiceImpl<T>
  implements IPersonService<T> {

 public void create(T person) throws BusinessException {

  log.info("");

  if (findPersonByAccount(person.getAccount()) != null) {
   throw new BusinessException("帐号 \"" + person.getAccount()
     + "\" 已经存在. ");
  }

  this.baseDAO.persist(person);
 }

 @SuppressWarnings("unchecked")
 public T findPersonByAccount(String account) {

  log.info("");

  List<T> personList = this.baseDAO.find(" SELECT p FROM Person p "
    + " WHERE p.account = lower(?) AND p.deleted = false ", account
    .toLowerCase().trim());

  if (personList.size() > 0) {
   return personList.get(0);
  }

  return null;
 }

 @SuppressWarnings("unchecked")
 public T findPersonByAccountAndPassword(String login, String password) {

  log.info("");

  List<T> personList = this.baseDAO.find(" SELECT p FROM Person p "
    + " WHERE p.account = lower(?) "
    + " AND p.password = ? and p.deleted = false ", new String[] {
    login.toLowerCase().trim(), password });

  if (personList.size() > 0) {
   return personList.get(0);
  }

  return null;
 }

 

你可能感兴趣的:(struts2)