J2EE中dao層和Service層的理解
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直接調用好了。
最後更新:2017-04-02 06:52:22