J2EE中dao層和Service層的理解

來源:互聯網
上載者:User

 

dao層

最基本的CRUD操作,方法體裡的內容一兩句代碼搞定。如this.save(student)等等。

 

Service層

①可能調用多個dao。有一個Service,叫StudentService。比如存進去一個Student,利用的是StudentDao。要把存入Student這個操作,記錄到系統日誌裡面,還要調用LogDao。這樣就需要把兩個Dao注入到StudentService裡面。

 

②可能加入了邏輯判斷。比如存入一個student對象,那麼這個student對象可能是“添加”操作傳來的,還是“更新”做操作傳來的呢。那麼就要加入邏輯判斷:

 

public void add(Student s)
{
  stuDao.save(ry);
  LOG log = new LOG();
  log.setCreator(s.getCreator());

  log.setName("學生管理");
  log.setTime(Utility.GetTime());
  log.setDetial("錄入學生資訊   ID: " + s.getId() + ",姓名: " + s.getName);
  logDao.add(log);
 }

 public String saveStudent(String type, Student s)
 {
   String result = "";
   if (StringUtil.checkNotNull(type) && "add".equals(type))
   {
      add(s);
      result = "添加成功";
   }

   return result;
 }

有些很靈活較,較複雜的查詢,五花八門的,乾脆就在dao裡面寫好,Service直接調用好了。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.