一、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 Map<Integer, Employee>employees =null; static { employees=new HashMap<Integer,Employee>(); employees.put(1001,new Employee(1001,"E-AA","aa@163.com",1)); employees.put(1002,new Employee(1002,"E-BB","bb@163.com",1)); employees.put(1003,new Employee(1003,"E-CC","cc@163.com",0)); employees.put(1004,new Employee(1004,"E-DD","dd@163.com",0)); employees.put(1005,new Employee(1005,"E-EE","ee@163.com",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<Employee> 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:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/c" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描控制层组件--> <context:component-scan base-package="spring_mvc_rest.src.main.java.com.rgf"></context:component-scan> <!--配置Thymeleaf视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"><!--模板引擎--> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"><!--模板解析器--> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!--/WEB-INF/templates/index.html--> <!--视图前缀+逻辑视图+视图后缀就是我们完整的物理视图,即访问index.html,不用完整路径,即index即可进行访问--> <!--视图前缀--> <property name="prefix" value="/WEB-INF/templates/"/> <!--视图后缀--> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <!--开启mvc的注解驱动--> <mvc:annotation-driven/> <!--配置视图控制器--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> </beans>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"