DAO = Data Access Object
DAO模式将数据控制层和业务逻辑层分离。
DAO模式的实践,大抵可以分成三个部分:
1)数据实现(DAO)
2)Model
3)Service
这里的数据实现,可以直接通过JDBC的方式,或者Hibernate之类,数据控制与业务逻辑层的分离,道理也在这里。
Model可以理解为普通的POJO。
而Service则是就复杂情况而言的,包含了多个DAO的组合,加入了事务等处理。
看具体代码:
DAO部分:
1-1. com.cleversoft.lab.dao.CollegeDAO.java:
package com.cleversoft.lab.dao;
import java.util.List;
import java.util.Map;
public interface CollegeDAO {
public List getAllColleges();
public Map getCollegesMap();
}
1-2. com.cleversoft.lab.dao.CollegeDAOImp.java:
package com.cleversoft.lab.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.cleversoft.lab.model.College;
public class CollegeDAOImp implements CollegeDAO {
private static ArrayList<College> colleges;
private static HashMap<Integer, College> collegesMap;
static {
colleges = new ArrayList<College>();
colleges.add(new College(1, "常熟理工学院", "说明1"));
colleges.add(new College(2, "苏州大学", "说明2"));
colleges.add(new College(3, "北京大学", "说明3"));
collegesMap = new HashMap<Integer, College>();
Iterator i = colleges.iterator();
while (i.hasNext()) {
College college = (College) i.next();
collegesMap.put(college.getCollegeID(), college);
}
}
public List getAllColleges() {
return colleges;
}
public Map getCollegesMap() {
return collegesMap;
}
}
2-1. com.cleversoft.lab.dao.DepartmentDAO.java:
package com.cleversoft.lab.dao;
import java.util.List;
import com.cleversoft.lab.model.Department;
public interface DepartmentDAO {
public List getAllDepartments();
public Department getDepartment(Integer id);
public void update(Department dep);
public void insert(Department dep);
public void delete(Integer id);
}
2-2. com.cleversoft.lab.dao.DepartmentDAOImp.java:
package com.cleversoft.lab.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.cleversoft.lab.model.College;
import com.cleversoft.lab.model.Department;
public class DepartmentDAOImp implements DepartmentDAO {
private static ArrayList<Department> departments;
@SuppressWarnings("unused")
private static Map collegesMap;
static {
departments = new ArrayList<Department>();
departments.add(new Department(1, "计算机系", "说明1", new College(1,
"常熟理工学院", "说明1")));
departments.add(new Department(2, "计算机系", "说明2", new College(2, "苏州大学",
"说明2")));
departments.add(new Department(3, "计算机系", "说明3", new College(3, "北京大学",
"说明3")));
CollegeDAO collegeDAO = new CollegeDAOImp();
collegesMap = collegeDAO.getCollegesMap();
}
public void delete(Integer id) {
for (int i = 0; i < departments.size(); i++) {
Department department = (Department) departments.get(i);
if (department.getDepartmentID().equals(id)) {
departments.remove(i);
break;
}
}
}
public List getAllDepartments() {
return departments;
}
public Department getDepartment(Integer id) {
Department dep = null;
Iterator i = departments.iterator();
while (i.hasNext()) {
dep = (Department) i.next();
if (dep.getDepartmentID().equals(id)) {
break;
}
}
return dep;
}
public void insert(Department dep) {
int lastId = 0;
Iterator i = departments.iterator();
while (i.hasNext()) {
Department department = (Department) i.next();
if (department.getDepartmentID() > lastId) {
lastId = department.getDepartmentID();
}
}
dep.setCollege((College) collegesMap.get(dep.getCollege()
.getCollegeID()));
dep.setDepartmentID(lastId + 1);
departments.add(dep);
}
public void update(Department dep) {
Integer id = dep.getDepartmentID();
for (int i = 0; i < departments.size(); i++) {
Department department = (Department) departments.get(i);
if (department.getDepartmentID().equals(id)) {
dep.setCollege((College) collegesMap.get(dep.getCollege()
.getCollegeID()));
departments.set(i, dep);
break;
}
}
}
}
Model部分:
1. com.cleversoft.lab.model.College.java:
package com.cleversoft.lab.model;
import java.io.Serializable;
@SuppressWarnings("serial")
public class College implements Serializable {
Integer collegeID;
String collegeName;
String collegeDesc;
public College() {
}
public College(Integer collegeID, String collegeName, String collegeDesc) {
this.collegeID = collegeID;
this.collegeName = collegeName;
this.collegeDesc = collegeDesc;
}
public String getCollegeDesc() {
return collegeDesc;
}
public void setCollegeDesc(String collegeDesc) {
this.collegeDesc = collegeDesc;
}
public Integer getCollegeID() {
return collegeID;
}
public void setCollegeID(Integer collegeID) {
this.collegeID = collegeID;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
}
2. com.cleversoft.lab.model.Department.java:
package com.cleversoft.lab.model;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Department implements Serializable {
Integer departmentID;
String departmentName;
String departmentDesc;
College college;
public Department() {
}
public Department(Integer departmentID, String departmentName,
String departmentDesc, College college) {
this.departmentID = departmentID;
this.departmentName = departmentName;
this.departmentDesc = departmentDesc;
this.college = college;
}
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}
public String getDepartmentDesc() {
return departmentDesc;
}
public void setDepartmentDesc(String departmentDesc) {
this.departmentDesc = departmentDesc;
}
public Integer getDepartmentID() {
return departmentID;
}
public void setDepartmentID(Integer departmentID) {
this.departmentID = departmentID;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
Service:
1-1: com.cleversoft.lab.service.CollegeService.java:
package com.cleversoft.lab.service;
import java.util.List;
import java.util.Map;
public interface CollegeService {
public List getAllColleges();
public Map getCollegesMap();
}
1-2: com.cleversoft.lab.service.CollegeDAOService.java:
package com.cleversoft.lab.service;
import java.util.List;
import java.util.Map;
import com.cleversoft.lab.dao.CollegeDAO;
import com.cleversoft.lab.dao.CollegeDAOImp;
public class CollegeDAOService implements CollegeService {
CollegeDAO dao;
public CollegeDAOService() {
this.dao = new CollegeDAOImp();
}
public List getAllColleges() {
return dao.getAllColleges();
}
public Map getCollegesMap() {
return dao.getCollegesMap();
}
}
2-1: com.cleversoft.lab.service.DepartmentService.java:
package com.cleversoft.lab.service;
import java.util.List;
import com.cleversoft.lab.model.Department;
public interface DepartmentService {
public List getAllDepartments();
public Department getDepartment(Integer id);
public void update(Department dep);
public void insert(Department dep);
public void delete(Integer id);
}
2-2: com.cleversoft.lab.service.DepartmentDAOService.java:
package com.cleversoft.lab.service;
import java.util.List;
import com.cleversoft.lab.dao.DepartmentDAO;
import com.cleversoft.lab.dao.DepartmentDAOImp;
import com.cleversoft.lab.model.Department;
public class DepartmentDAOService implements DepartmentService {
DepartmentDAO dao;
public DepartmentDAOService() {
this.dao = new DepartmentDAOImp();
}
public void delete(Integer id) {
dao.delete(id);
}
public List getAllDepartments() {
return dao.getAllDepartments();
}
public Department getDepartment(Integer id) {
return dao.getDepartment(id);
}
public void insert(Department dep) {
dao.insert(dep);
}
public void update(Department dep) {
dao.update(dep);
}
}
通过实现来看,DAO模式,思路清晰,但是实现起来,还是比较麻烦的。
而ORM又帮助我们简化了什么,下回再续!