SpringMVC(七)RESTful案例

一、RESTful案例之准备工作:

和传统CRUD一样,实现对员工信息的增删改查

我们所在的环境为SpringMVC(六)RESTful-CSDN博客

在此环境上我们进行创建实体类(创建pojo包下面的Employee类):

package com.rgf.pojo;

public class Employee {
    private  Integer id;
    private  String LastName;

    private  String email;
    //1 male,0 female
    private  Integer gender;

    public Employee() {
    }

    public Employee(Integer id, String lastName, String email, Integer gender) {
        this.id = id;
        LastName = lastName;
        this.email = email;
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", LastName='" + LastName + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                '}';
    }
}

 之后我们创建dao层里面的实现类EmployeeDao,java:

package com.rgf.dao;

import com.rgf.pojo.Employee;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    private  static Mapemployees =null;
    static {
        employees=new HashMap();
        employees.put(1001,new Employee(1001,"E-AA","[email protected]",1));
        employees.put(1002,new Employee(1002,"E-BB","[email protected]",1));
        employees.put(1003,new Employee(1003,"E-CC","[email protected]",0));
        employees.put(1004,new Employee(1004,"E-DD","[email protected]",0));
        employees.put(1005,new Employee(1005,"E-EE","[email protected]",1));
    }

    private static Integer initId=1006;
    public  void save(Employee employee){
        //i++,先赋值,再自增
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employees.put(employee.getId(),employee);//如果此时员工id为null,则为添加功能;如果员工id不为null,则为修改功能。
    }

    public Collection getAll(){
        return  employees.values();
    }

    public  Employee get(Integer id){
        return  employees.get(id);
    }

    public void  delete(Integer id){
        employees.remove(id);
    }
}

 之后我们创建controller层里面的实现类EmployeeController.java:

package com.rgf.controller;

import com.rgf.pojo.Employee;
import org.springframework.stereotype.Controller;

/**
 * 查询所有的员工信息-->/employee-->get
 * 跳转到添加页面-->to/Add/-->get
 * 新增员工信息-->/employee-->post
 * 跳转到修改页面-->/employee/1-->get
 * 修改员工信息-->/employee-->put
 * 删除员工信息-->/employee/1-->delete
 */
@Controller
public class EmployeeController {
  
}

二、RESTful案例之列表功能: 

我们首先进行实现列表功能:

我们的配置环境如下所示:

springmvc.xml:




    
    
    
    
        
        
        
            
                
                
                        
                        
                        
                       
                        
                        
                        
                        
                     
                 
            
        
    
    
    

    
    

 pom.xml:


你可能感兴趣的:(SpringMVC,restful,后端,java)