WEB开发网
开发学院软件开发Java JavaEE 3层架构的 DAO, Service, Web 简单封装 阅读

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

 2009-11-17 00:00:00 来源:WEB开发网   
核心提示: XtyhbSerivce1:/**2:*3:*@authorHacker-TTAO4:*@version0.0.15:*CreateDate:Nov11,20091:36:20PM6:*ChangeList:7:*/8:publicinterfaceXtyhbServiceextendsGene

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 就不需要写了.

上一页  2 3 4 5 6 7 

Tags:JavaEE 架构 DAO

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接