背景:
在大致的学习SpringMVC到现在,已经具备使用SpringMVC写个简单的CRUD了。
既然如此,话不多说,我们即刻出发。嘻嘻?
第一步:首先创建一个javaweb工程,创建基本的web工程结构,导入包。
所有的工程都放在com.springmvc.crud包中,先来弄一个基本的需要保存的javabean对象。
分别是Employee(id,lastName,email,gender,department)和 Department(id,departmentName)
将这两个类放在entities包中
Empoyee.java
package com.springmvc.crud.entities;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department;
这里省略了get/set方法,toString方法。
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
}
public Employee() { //无参的构造方法十分重要
}
}
Department.java
package com.springmvc.crud.entities;
public class Department {
private Integer id;
private String departmentName;
省略get/set方法
public Department(){
}
public Department(Integer id, String departmentName) {
this.id = id;
this.departmentName = departmentName;
}
}
第二步,因为这里没有用到数据库,我们模拟dao,将EmployeeDao和DepartmentDao类放在dao包中
EmployeeDao.java
package com.springmvc.crud.dao;
import com.springmvc.crud.entities.Department;
import com.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository //注意这里的声明
public class EmployeeDao {
private static Map<Integer,Employee> employees = null;
@Autowired //自动装配
private DepartmentDao departmentDao;
static { //模拟数据库中已经存在这些数据
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
}
private static Integer initId = 1006;
public void save(Employee employee){
if(employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//模拟数据库得到所有的数据
public Collection<Employee> getAll(){
return employees.values();
}
//模拟数据库根据id取出一条数据
public Employee get(Integer id){
return employees.get(id);
}
//模拟数据库根据id删除一条数据
public void delete(Integer id){
employees.remove(id);
}
}
DepartmentDao.java
package com.springmvc.crud.dao;
import com.springmvc.crud.entities.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class DepartmentDao {
private static Map<Integer,Department> departments = null;
static{
departments = new HashMap<Integer, Department>();
departments.put(101, new Department(101, "D-AA"));
departments.put(102, new Department(102, "D-BB"));
departments.put(103, new Department(103, "D-CC"));
departments.put(104, new Department(104, "D-DD"));
departments.put(105, new Department(105, "D-EE"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
ok,初步的准备工作就完成了,现在我们有了javabean业务的实体类以及相应的dao操作数据类。
下一章,开始我们的重头戏,handler处理请求。