JavaEE 3层架构的的 DAO, Service, Web 简单封装

时间:2022-02-18 20:56:23

由于大多数项目都是使用开源的 SSH 三大框架来做. 所以对DAO, Service 层进行了简单的封装. 封装的方法非常简单, 就是将 Hibernate 的 CURD 等常用方法封装起来. 免得每一个 DAO 内都需要再写这些方法. Service 相同.

实现的逻辑大体是

GeneralDAO(Interface). AbstractGeneralDAOImpl(Abstract class),  GeneralService(Interface), AbstractGeneralServiceImpl(Abstract class).

具体

IDAO extends GeneralDAO, 

IDAOImpl extends AbstractGeneralDAOImpl,

IService extends GeneralService,

IServiceImpl extends AbstractGeneralServiceImpl

 

实现代码如下.

GeneralDAO<T, ID extends Serializable>

   1:  import java.io.Serializable;
   2:  import java.util.Collection;
   3:  import java.util.List;
   4:   
   5:  import PagingVO;
   6:   
   7:  /**
   8:   * General DAO
   9:   * 
  10:   * @author Hacker-TTAO
  11:   * @version 0.1.0
  12:   */
  13:  public interface GeneralDAO<T, ID extends Serializable> {
  14:   
  15:      T findByID(ID id);
  16:   
  17:      List<T> findAll();
  18:   
  19:      PagingVO getPagingData(int currentPage, int pageSize);
  20:      
  21:      T save(T entity);
  22:      
  23:      void save(T... entity);
  24:   
  25:      T update(T entity);
  26:      
  27:      void update(Collection<T> entity);
  28:   
  29:      void saveOrUpdate(T entity);
  30:   
  31:      void saveOrUpdate(Collection<T> entities);
  32:   
  33:      void delete(T entity);
  34:      
  35:      void delete(ID id);
  36:      
  37:      void delete(Collection<T> entities);
  38:   
  39:      void marge(T eneity);
  40:      
  41:      void marge(T... eneity);
  42:      
  43:      int getCount();
  44:  }

 

AbstractGeneralDAOImpl<T, ID extends Serializable>

   1:  import java.io.Serializable;
   2:  import java.lang.reflect.ParameterizedType;
   3:   
   4:  import org.apache.commons.logging.Log;
   5:  import org.apache.commons.logging.LogFactory;
   6:   
   7:  /**
   8:   * Abstract General DAO Implement
   9:   * 
  10:   * @author Hacker-TTAO
  11:   * @version 0.1.0
  12:   */
  13:  public abstract class AbstractGeneralDAOImpl<T, ID extends Serializable> implements GeneralDAO<T, ID> {
  14:   
  15:      private final Log log = LogFactory.getLog(this.getClass());
  16:   
  17:      protected final Class<T> persistenceClass;
  18:   
  19:      protected final Class<T> pkClass;
  20:   
  21:      protected AbstractGeneralDAOImpl() {
  22:          this.persistenceClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  23:          this.pkClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
  24:   
  25:          if (log.isInfoEnabled()) {
  26:              log.info("解析泛型参数 T : " + getPersistenceClass().getName());
  27:          }
  28:      }
  29:   
  30:      protected final Class<T> getPersistenceClass() {
  31:          return persistenceClass;
  32:      }
  33:   
  34:      protected Class<T> getPkClass() {
  35:          return pkClass;
  36:      }
  37:  }

 

基于 Spring 封装的 Hibernate DAO 抽象实现

AbstractSpringDAO<T, ID extends Serializable>

   1:  import java.io.Serializable;
   2:  import java.sql.SQLException;
   3:  import java.util.Collection;
   4:  import java.util.List;
   5:   
   6:  import org.hibernate.Criteria;
   7:  import org.hibernate.HibernateException;
   8:  import org.hibernate.Session;
   9:  import org.hibernate.criterion.Projections;
  10:  import org.springframework.orm.hibernate3.HibernateCallback;
  11:  import org.springframework.orm.hibernate3.HibernateTemplate;
  12:   
  13:  /**
  14:   * 基于 Spring Hibernate 的DAO
  15:   * 
  16:   * @author Hacker-TTAO
  17:   * 
  18:   * @param <T>
  19:   * @param <ID>
  20:   */
  21:  public abstract class AbstractSpringDAO<T, ID extends Serializable> extends AbstractGeneralDAOImpl<T, ID> {
  22:   
  23:      private HibernateTemplate hibernateTemplate;
  24:   
  25:      public HibernateTemplate getHibernateTemplate() {
  26:          return hibernateTemplate;
  27:      }
  28:   
  29:      public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  30:          this.hibernateTemplate = hibernateTemplate;
  31:      }
  32:   
  33:      public List<T> findAll() {
  34:          return getHibernateTemplate().loadAll(getPersistenceClass());
  35:      }
  36:   
  37:      public T findByID(ID id) {
  38:          return (T) getHibernateTemplate().get(getPersistenceClass(), id);
  39:      }
  40:   
  41:      public void delete(T entity) {
  42:          getHibernateTemplate().delete(entity);
  43:      }
  44:   
  45:      public void delete(Collection<T> entities) {
  46:          getHibernateTemplate().deleteAll(entities);
  47:      }
  48:   
  49:      public void marge(T eneity) {
  50:          getHibernateTemplate().merge(eneity);
  51:      }
  52:   
  53:      public void marge(T... eneity) {
  54:      }
  55:   
  56:      public T save(T entity) {
  57:          getHibernateTemplate().save(entity);
  58:          return entity;
  59:      }
  60:   
  61:      public void save(T... entity) {
  62:      }
  63:   
  64:      public void saveOrUpdate(T entity) {
  65:          getHibernateTemplate().saveOrUpdate(entity);
  66:      }
  67:   
  68:      public void saveOrUpdate(Collection<T> entities) {
  69:          getHibernateTemplate().saveOrUpdate(entities);
  70:      }
  71:   
  72:      public T update(T entity) {
  73:          getHibernateTemplate().update(entity);
  74:          return entity;
  75:      }
  76:   
  77:      public void update(Collection<T> entity) {
  78:      }
  79:   
  80:      public int getCount() {
  81:          Object obj = getHibernateTemplate().execute(new HibernateCallback() {
  82:              public Object doInHibernate(Session session) throws HibernateException, SQLException {
  83:                  Criteria c = session.createCriteria(getPersistenceClass());
  84:                  c.setProjection(Projections.rowCount());
  85:                  return c.uniqueResult();
  86:              }
  87:          });
  88:          return Integer.parseInt(obj.toString());
  89:      }
  90:   
  91:      public void delete(ID id) {
  92:          T entity = findByID(id);
  93:          delete(entity);
  94:      }
  95:  }

具体 DAO 实现.

XtyhbDAO

   1:  import Xtyhb;
   2:   
   3:  /**
   4:   * @author Hacker-TTAO
   5:   * @version 1.0
   6:   * Create Date : Nov 11, 2009 1:34:16 PM
   7:   * Change List:
   8:   */
   9:  public interface XtyhbDAO extends GeneralDAO<Xtyhb, Integer>  {
  10:      Xtyhb findByNameAndPwd(String username, String password);
  11:  }

 

XtyhbDAOImpl

   1:  import java.util.List;
   2:   
   3:  /**
   4:   * 
   5:   * @author Hacker-TTAO
   6:   * @version 0.0.1 
   7:   * Create Date : Nov 11, 2009 1:35:10 PM 
   8:   * Change List:
   9:   */
  10:  @SuppressWarnings("unchecked")
  11:  public class XtyhbDAOImpl extends AbstractSpringDAO<Xtyhb, Integer> implements XtyhbDAO {
  12:   
  13:      public Xtyhb findByNameAndPwd(String username, String password) {
  14:          // implement
  15:          return null;
  16:      }
  17:   
  18:      public PagingVO getPagingData(int currentPage, int pageSize) {
  19:          return null;
  20:      }
  21:  }

 

至此. DAO 层封装完毕. 下面进行 Service 层封装

GeneralService<T, ID extends Serializable, DAO extends GeneralDAO<T, ID>>

   1:  import java.io.Serializable;
   2:  import java.util.Collection;
   3:  import java.util.List;
   4:   
   5:  /**
   6:   * 
   7:   * @author Hacker-TTAO
   8:   *
   9:   * @param <T>
  10:   * @param <ID>
  11:   * @param <DAO>
  12:   */
  13:  public interface GeneralService<T, ID extends Serializable, DAO extends GeneralDAO<T, ID>> {
  14:      T findByID(ID id);
  15:   
  16:      List<T> findAll();
  17:   
  18:      T save(T entity);
  19:   
  20:      void save(T... entity);
  21:   
  22:      T update(T entity);
  23:   
  24:      void update(Collection<T> entity);
  25:   
  26:      void saveOrUpdate(T entity);
  27:   
  28:      void saveOrUpdate(Collection<T> entities);
  29:   
  30:      void delete(T entity);
  31:   
  32:      void delete(Collection<T> entities);
  33:      
  34:      void delete(ID id);
  35:   
  36:      void marge(T eneity);
  37:   
  38:      void marge(T... eneity);
  39:  }

 

AbstractServiceImpl<T, ID extends Serializable, DAO extends GeneralDAO<T, ID>>

   1:  import java.io.Serializable;
   2:  import java.util.Collection;
   3:  import java.util.List;
   4:   
   5:  import org.apache.commons.logging.Log;
   6:  import org.apache.commons.logging.LogFactory;
   7:   
   8:  /**
   9:   * 
  10:   * @author Hacker-TTAO
  11:   *
  12:   * @param <T>
  13:   * @param <ID>
  14:   * @param <DAO>
  15:   */
  16:  public abstract class AbstractServiceImpl<T, ID extends Serializable, DAO extends GeneralDAO<T, ID>> implements GeneralService<T, ID, DAO> {
  17:   
  18:      private DAO dao;
  19:   
  20:      private static final Log log = LogFactory.getLog(AbstractServiceImpl.class);
  21:   
  22:      public List<T> findAll() {
  23:          List<T> result = getDao().findAll();
  24:          log.info("findAll execute success");
  25:          return result;
  26:      }
  27:   
  28:      public T findByID(ID id) {
  29:          T result = getDao().findByID(id);
  30:          log.info("findById execute success");
  31:          return result;
  32:      }
  33:   
  34:      public void delete(Collection<T> entities) {
  35:          getDao().update(entities);
  36:      }
  37:   
  38:      public void delete(T entity) {
  39:          getDao().delete(entity);
  40:      }
  41:   
  42:      public void delete(ID id) {
  43:          getDao().delete(id);
  44:      }
  45:   
  46:      public void marge(T... eneity) {
  47:      }
  48:   
  49:      public void marge(T eneity) {
  50:          getDao().marge(eneity);
  51:   
  52:      }
  53:   
  54:      public void save(T... entity) {
  55:          getDao().save(entity);
  56:      }
  57:   
  58:      public T save(T entity) {
  59:          return getDao().save(entity);
  60:      }
  61:   
  62:      public void saveOrUpdate(Collection<T> entities) {
  63:          getDao().saveOrUpdate(entities);
  64:      }
  65:   
  66:      public void saveOrUpdate(T entity) {
  67:          getDao().saveOrUpdate(entity);
  68:      }
  69:   
  70:      public void update(Collection<T> entity) {
  71:          getDao().update(entity);
  72:      }
  73:   
  74:      public T update(T entity) {
  75:          return getDao().update(entity);
  76:      }
  77:   
  78:      public DAO getDao() {
  79:          return dao;
  80:      }
  81:   
  82:      public void setDao(DAO dao) {
  83:          this.dao = dao;
  84:      }
  85:   
  86:  }

 

XtyhbSerivce

   1:  /**
   2:   * 
   3:   * @author Hacker-TTAO
   4:   * @version 0.0.1
   5:   * Create Date : Nov 11, 2009 1:36:20 PM
   6:   * Change List:
   7:   */
   8:  public interface XtyhbService extends GeneralService<Xtyhb, Integer, XtyhbDAO> {
   9:      Xtyhb findByNameAndPwd(Xtyhb xtyhb);
  10:  }

 

 

XtyhbServiceImpl

   1:  /**
   2:   * 
   3:   * @author Hacker-TTAO
   4:   * @version 1.0
   5:   * Create Date : Nov 11, 2009 1:36:54 PM 
   6:   * Change List:
   7:   */
   8:  public class XtyhbServiceImpl extends AbstractServiceImpl<Xtyhb, Integer, XtyhbDAO> implements XtyhbService {
   9:   
  10:      public Xtyhb findByNameAndPwd(Xtyhb xtyhb) {
  11:          return getDao().findByNameAndPwd(xtyhb.getYhmc(), xtyhb.getMm());
  12:      }
  13:  }

 

Service 层封装完毕. Web 层调用 Service

   1:  public class LoginAction {
   2:      /*----------------- ACTION RESULT CONSTANT FILED -------------------------*/
   3:      public static final String FAILURE = "failure";
   4:   
   5:      /*---------------------- Page | Action Parameters ------------------------*/
   6:      private Xtyhb xtyhb;
   7:      private String message;
   8:   
   9:      /*--------------------- Service Refrence Object --------------------------*/
  10:      private XtyhbService xtyhbService;
  11:   
  12:      /*------------------------ GETTER AND SETTER  ----------------------------*/
  13:   
  14:      public String getMessage() {
  15:          return message;
  16:      }
  17:   
  18:      public void setMessage(String message) {
  19:          this.message = message;
  20:      }
  21:   
  22:      public Xtyhb getXtyhb() {
  23:          return xtyhb;
  24:      }
  25:   
  26:      public void setXtyhb(Xtyhb xtyhb) {
  27:          this.xtyhb = xtyhb;
  28:      }
  29:   
  30:      public XtyhbService getXtyhbService() {
  31:          return xtyhbService;
  32:      }
  33:   
  34:      public void setXtyhbService(XtyhbService xtyhbService) {
  35:          this.xtyhbService = xtyhbService;
  36:      }
  37:   
  38:      public String login() {
  39:          xtyhb.setMm(MD5Encrypt.getMD5(xtyhb.getMm()));
  40:          Xtyhb loginUser = getXtyhbService().findByNameAndPwd(xtyhb);
  41:   
  42:          if (null == loginUser) {
  43:              message = "没有此用户";
  44:              return FAILURE;
  45:          }
  46:   
  47:          if (XtyhbStateConst.DISABLE == loginUser.getYhzt()) {
  48:              message = "此用户被禁用";
  49:              return FAILURE;
  50:          }
  51:   
  52:          // if user is admin
  53:          if (ADMIN_USER.equals(loginUser.getYhmc())) {
  54:              ActionContextUtil.setAttr2Session("menuTree", ADMIN_FUNCTION);
  55:          } else {
  56:              List<Xtflb> xtflbs = getXtflbService().findByXtyhb(loginUser);
  57:              ActionContextUtil.setAttr2Session("xtflbs", xtflbs);
  58:              ActionContextUtil.setAttr2Session("menuTree", CreateMenuTreeUtil.createMenuTree(xtflbs));
  59:          }
  60:   
  61:          ActionContextUtil.setAttr2Session("user", loginUser);
  62:          return Action.SUCCESS;
  63:      }
  64:  }

 

至此. 简单的封装结束.  在 Web 层 Action 中调用类似 CURD 的方法时, 每个 DAO 和 Service 就不需要写了.