SpringMVC_14_RESTFUL_CRUD(一)初步准备

本文介绍了在SpringMVC环境下进行RESTFUL CRUD操作的初步准备,包括创建javaweb工程,搭建基本的web工程结构,导入所需包,并定义了Employee和Department两个实体类,以及模拟的DAO接口。准备工作完成后,为后续的handler处理请求奠定了基础。
摘要由CSDN通过智能技术生成

背景:

在大致的学习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处理请求。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值