SpringBootのDaoレイヤシミュレーションデータベース操作
1985 ワード
単一テーブル操作:
関連表アクション:
# src/main/java/com/wu/dao/DepartmentDao .java
@Repository
public class DepartmentDao {
private static Map departments = null;
private static Integer initId = 5;
static {
departments = new HashMap();
departments.put(1, new Department(1, " "));
departments.put(2, new Department(2, " "));
departments.put(3, new Department(3, " "));
departments.put(4, new Department(4, " "));
}
public Collection index() {
return departments.values();
}
public void store(Department department) {
department.setId(initId);
departments.put(initId, department);
initId++;
}
public Department show(Integer id) {
return departments.get(id);
}
public void destroy(Integer id) {
departments.remove(id);
}
}
関連表アクション:
# src/main/java/com/wu/dao/EmployeeDao.java
@Repository
public class EmployeeDao {
private static Map employees = null;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap();
employees.put(1, new Employee(1, " ", 3));
employees.put(2, new Employee(2, " ", 1));
employees.put(3, new Employee(3, " ", 4));
employees.put(4, new Employee(4, " ", 2));
}
private static Integer initId = 5;
public Collection index() {
return employees.values();
}
public void store(Employee employee) {
employee.setId(initId);
employees.put(initId, employee);
initId++;
}
public Employee show(Integer id) {
Employee employee = employees.get(id);
Integer departmentId = employee.getDepartmentId();
Department department = departmentDao.getDepartment(departmentId);
employee.setDepartment(department);
return employee;
}
public void destroy(Integer id) {
employees.remove(id);
}
}