首页 > 代码库 > springmvc之使用RESTFUL—CRUD(creat-read-update-delete)——后端版

springmvc之使用RESTFUL—CRUD(creat-read-update-delete)——后端版

一、搭建环境

1、配置pom.xml文件

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4 
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>com.wn</groupId>
  8     <artifactId>forth-ssmm</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10     <name>forth-ssmm</name>
 11     <packaging>war</packaging>
 12 
 13     <properties>
 14         <java.version>1.8</java.version>
 15         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 16         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 17         <spring.version>4.2.4.RELEASE</spring.version>
 18         <mybatis-spring.version>1.2.2</mybatis-spring.version>
 19         <mybatis.version>3.2.8</mybatis.version>
 20         <servlet-api.version>3.1.0</servlet-api.version>
 21         <velocity.version>1.7</velocity.version>
 22         <velocity-tools.version>2.0</velocity-tools.version>
 23         <jackson.version>2.6.4</jackson.version>
 24 
 25     </properties>
 26 
 27     <!-- 引入实际依赖 -->
 28     <dependencies>
 29         <!-- servlet -->
 30         <dependency>
 31             <groupId>javax.servlet</groupId>
 32             <artifactId>javax.servlet-api</artifactId>
 33             <version>${servlet-api.version}</version>
 34             <scope>provided</scope>
 35         </dependency>
 36 
 37         <!-- spring -->
 38         <dependency>
 39             <groupId>org.springframework</groupId>
 40             <artifactId>spring-core</artifactId>
 41             <version>${spring.version}</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-beans</artifactId>
 46             <version>${spring.version}</version>
 47         </dependency>
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-context</artifactId>
 51             <version>${spring.version}</version>
 52         </dependency>
 53         <dependency>
 54             <groupId>org.springframework</groupId>
 55             <artifactId>spring-web</artifactId>
 56             <version>${spring.version}</version>
 57         </dependency>
 58         <dependency>
 59             <groupId>org.springframework</groupId>
 60             <artifactId>spring-webmvc</artifactId>
 61             <version>${spring.version}</version>
 62         </dependency>
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-jdbc</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68         <!-- 使用json必需的三个包 -->
 69         <dependency>
 70             <groupId>com.fasterxml.jackson.core</groupId>
 71             <artifactId>jackson-annotations</artifactId>
 72             <version>${jackson.version}</version>
 73         </dependency>
 74         <dependency>
 75             <groupId>com.fasterxml.jackson.core</groupId>
 76             <artifactId>jackson-core</artifactId>
 77             <version>${jackson.version}</version>
 78         </dependency>
 79         <dependency>
 80             <groupId>com.fasterxml.jackson.core</groupId>
 81             <artifactId>jackson-databind</artifactId>
 82             <version>${jackson.version}</version>
 83         </dependency>
 84         <!-- 这个是使用velocity的必备包 -->
 85         <dependency>
 86             <groupId>org.springframework</groupId>
 87             <artifactId>spring-context-support</artifactId>
 88             <version>${spring.version}</version>
 89             <scope>compile</scope>
 90         </dependency>
 91         <!-- mysql -->
 92         <dependency>
 93             <groupId>mysql</groupId>
 94             <artifactId>mysql-connector-java</artifactId>
 95             <version>5.1.27</version>
 96             <scope>runtime</scope>
 97         </dependency>
 98         <!-- 数据源 -->
 99         <dependency>
100             <groupId>org.apache.tomcat</groupId>
101             <artifactId>tomcat-jdbc</artifactId>
102             <version>7.0.47</version>
103         </dependency>
104         <!-- mybatis -->
105         <dependency>
106             <groupId>org.mybatis</groupId>
107             <artifactId>mybatis</artifactId>
108             <version>${mybatis.version}</version>
109         </dependency>
110         <dependency>
111             <groupId>org.mybatis</groupId>
112             <artifactId>mybatis-spring</artifactId>
113             <version>${mybatis-spring.version}</version>
114         </dependency>
115         <!-- velocity -->
116         <dependency>
117             <groupId>org.apache.velocity</groupId>
118             <artifactId>velocity</artifactId>
119             <version>${velocity.version}</version>
120         </dependency>
121         <dependency>
122             <groupId>org.apache.velocity</groupId>
123             <artifactId>velocity-tools</artifactId>
124             <version>${velocity-tools.version}</version>
125         </dependency>
126         <!-- import lombok -->
127         <dependency>
128             <groupId>org.projectlombok</groupId>
129             <artifactId>lombok</artifactId>
130             <version>1.16.8</version>
131             <scope>provided</scope>
132         </dependency>
133     </dependencies>
134     <build>
135         <plugins>
136             <plugin>
137                 <groupId>org.apache.maven.plugins</groupId>
138                 <artifactId>maven-compiler-plugin</artifactId>
139                 <configuration>
140                     <source>1.8</source>
141                     <target>1.8</target>
142                     <encoding>utf-8</encoding>
143                 </configuration>
144             </plugin>
145         </plugins>
146     </build>
147 </project>

2、配置web.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 5     <!-- 配置DispatcherServlet -->
 6     <!-- 配置DispatcherServlet的作用是:如果在某个方法上配置了@RequestMapping("/helloworld"), 
 7         当浏览器访问helloworld时,DispatcherServlet会将这个请求发送给@RequestMapping("/helloworld") 
 8         所在的方法上,执行这个方法 -->
 9     <servlet>
10         <servlet-name>SpringDispatcherServlet</servlet-name>
11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12         <!-- 配置DispatcherServlet的一个初始化参数:配置springMVC配置文件的位置和名称 -->
13         <!-- 我们也可以不适用contextConfigLocation来配置springmvc配置文件的位置和名称,可以使用默认的, 默认的配置文件为:/WEB-IFN/<servlet-name>-servlet.xml -->
14         <init-param>
15             <param-name>contextConfigLocation</param-name>
16             <param-value>classpath*:springmvc.xml</param-value>
17         </init-param>
18         <!--SpringDispatcherServlet在当前web应用被加载的时候被创建,而不是等第一次请求的时候被创建 -->
19         <load-on-startup>1</load-on-startup>
20     </servlet>
21     <servlet-mapping>
22         <servlet-name>SpringDispatcherServlet</servlet-name>
23         <url-pattern>/</url-pattern>  <!-- 可以应答所有请求 -->
24     </servlet-mapping>
25     
26     <welcome-file-list>
27         <welcome-file>/index.jsp</welcome-file>
28     </welcome-file-list>
29 </web-app>

3、配置springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 7                            http://www.springframework.org/schema/context 
 8                            http://www.springframework.org/schema/context/spring-context-3.2.xsd
 9                            http://www.springframework.org/schema/mvc 
10                            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
11     <!-- 配置自动扫描的包 -->
12     <context:component-scan base-package="com.guigu.springmvc.crud"/>
13     <!--  使用json需要启动的配置 -->
14     <mvc:annotation-driven />
15 </beans>

二、创建实体类

1、Department

 1 package com.guigu.springmvc.crud.entities;
 2 
 3 public class Department {
 4     /**
 5      * 虽然定义成int型也可以,但是后续要往map中放,如果用int放不进去,尽量用Integer
 6      */
 7     private Integer id;
 8     private String departmentName;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getDepartmentName() {
19         return departmentName;
20     }
21 
22     public void setDepartmentName(String departmentName) {
23         this.departmentName = departmentName;
24     }
25 /**
26  * 如果没有任何构造器(有参或无参)系统默认提供无参构造器,如果构造了有参构造器,必须构造无参构造器,
27  * 否则测试中Department d = new Department();会报错
28  * Department dm = new Department(1, "AA");(1、属性多的话,不好对应2、如果以后增加属性得重新生成构造器)相当于下面三句话
29  *      Department d = new Department();    (增加属性无需重新生成构造器,只要在后面加属性即可)
30         d.setId(1);
31         d.setDepartmentName("AA");
32  */
33     public Department() {
34 
35     }
36 /**
37  * 可以用来赋值
38  */
39     public Department(Integer id, String departmentName) {// 将传进来的参数赋值给属性
40         this.id = id;
41         this.departmentName = departmentName;
42     }
43 }

2、Employee

 1 package com.guigu.springmvc.crud.entities;
 2 
 3 public class Employee {
 4     private Integer id;
 5     private String lastName;
 6     private String email;
 7     // 0 female 1 male
 8     private Integer gender;
 9     private Department department;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 
19     public String getLastName() {
20         return lastName;
21     }
22 
23     public void setLastName(String lastName) {
24         this.lastName = lastName;
25     }
26 
27     public String getEmail() {
28         return email;
29     }
30 
31     public void setEmail(String email) {
32         this.email = email;
33     }
34 
35     public Integer getGender() {
36         return gender;
37     }
38 
39     public void setGender(Integer gender) {
40         this.gender = gender;
41     }
42 
43     public Department getDepartment() {
44         return department;
45     }
46 
47     public void setDepartment(Department department) {
48         this.department = department;
49     }
50 
51     public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
52         this.id = id;
53         this.lastName = lastName;
54         this.email = email;
55         this.gender = gender;
56         this.department = department;
57     }
58 
59     @Override
60     public String toString() {
61         return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender
62                 + ", department=" + department + "]";
63     }
64 
65     public Employee() {
66 
67     }
68 }

三、创建DAO

1、DepartmentDao

 1 package com.guigu.springmvc.crud.dao;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import org.springframework.stereotype.Repository;
 8 
 9 import com.guigu.springmvc.crud.entities.Department;
10 
11 @Repository
12 public class DepartmentDao {
13     private static Map<Integer, Department> departments = null;
14     static {
15         departments = new HashMap<Integer, Department>();
16         departments.put(101, new Department(101, "D-AA"));
17         departments.put(102, new Department(102, "D-BB"));
18         departments.put(103, new Department(103, "D-CC"));
19         departments.put(104, new Department(104, "D-DD"));
20         departments.put(105, new Department(105, "D-EE"));
21     }
22 
23     public Collection<Department> getDepartments() {//显示所有部门
24         return departments.values();
25     }
26 
27     public Department getDepartment(Integer id) { //根据id获取部门
28         return departments.get(id);
29     }
30 }

2、EmployeeDao

 1 package com.guigu.springmvc.crud.dao;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Repository;
 9 
10 import com.guigu.springmvc.crud.entities.Department;
11 import com.guigu.springmvc.crud.entities.Employee;
12 
13 @Repository
14 public class EmployeeDao {
15     public static Map<Integer, Employee> employees = null;
16     @Autowired
17     private DepartmentDao departmentDao;
18     static {
19         employees = new HashMap<Integer, Employee>();
20         employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
21         employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
22         employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
23         employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
24         employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
25     }
26     public static Integer initId = 1006;
27 
28     public void save(Employee employee) {
29         if (employee.getId() == null) {
30             employee.setId(initId++);
31         }
32         /*
33          * employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId())); 
34          * 等价于 
35          * Department d1 =employee.getDepartment(); 
36          * Integer id = d1.getId(); 
37            Department department = departmentDao.getDepartment(id);
38          * employee.setDepartment(department);
39          */
40         Department d1 = employee.getDepartment();
41         Integer id = d1.getId();
42         Department department = departmentDao.getDepartment(id);
43         employee.setDepartment(department);
44 
45         employees.put(employee.getId(), employee);
46     }
47 
48     public Collection<Employee> getAll() {
49         return employees.values();
50     }
51 
52     public Employee get(Integer id) {
53         return employees.get(id);
54     }
55 
56     public void delete(Integer id) {
57         employees.remove(id);
58     }
59 }

四、创建EmployeeHandler,实现增、删、改、查

1、添加员工信息

 1 package com.guigu.springmvc.crud.handlers;
 2 
 3 import java.util.Map;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.bind.annotation.ResponseBody;
11 
12 import com.guigu.springmvc.crud.dao.EmployeeDao;
13 import com.guigu.springmvc.crud.entities.Department;
14 import com.guigu.springmvc.crud.entities.Employee;
15 
16 @Controller
17 public class EmployeeHandler {
18     @Autowired
19     private EmployeeDao employeeDao;
20 
21     @RequestMapping(value="http://www.mamicode.com/addEmployee",method=RequestMethod.GET)
22     @ResponseBody
23     public Employee addEmployee(@RequestParam("id") Integer id, 
24                             @RequestParam("name") String lastName,
25                             @RequestParam("email") String email, 
26                             @RequestParam("gender") Integer gender,
27                             @RequestParam("did") Integer did,
28                             @RequestParam("dname") String dname) {
29         Employee employee = new Employee();
30         employee.setId(id);
31         employee.setLastName(lastName);
32         employee.setEmail(email);
33         employee.setGender(gender);
34         Department department = new Department(did, dname);
35         employee.setDepartment(department);
36         Map<Integer, Employee> es = EmployeeDao.employees;
37         es.put(id, employee);
38         
39         return employeeDao.get(id);
40     }
41 }

测试方式:在浏览器中输入:http://localhost:8080/forth-ssmm/addEmployee?id=1&name=hua&email=aa@qq.com&gender=0&did=009&dname=cto

运行结果:

技术分享

翻译为jason格式:

 1 {
 2     "id": 1,
 3     "lastName": "hua",
 4     "email": "aa@qq.com",
 5     "gender": 0,
 6     "department": {
 7         "id": 9,
 8         "departmentName": "cto"
 9     }
10 }

 2、删除员工信息

 1 package com.guigu.springmvc.crud.handlers;
 2 
 3 import java.util.Collection;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import com.guigu.springmvc.crud.dao.EmployeeDao;
14 import com.guigu.springmvc.crud.entities.Department;
15 import com.guigu.springmvc.crud.entities.Employee;
16 
17 @Controller
18 public class EmployeeHandler {
19     @Autowired
20     private EmployeeDao employeeDao;
21     
22     
23     @RequestMapping(value="http://www.mamicode.com/addEmployee",method=RequestMethod.GET)
24     @ResponseBody
25     public Employee addEmployee(@RequestParam("id") Integer id, 
26                             @RequestParam("name") String lastName,
27                             @RequestParam("email") String email, 
28                             @RequestParam("gender") Integer gender,
29                             @RequestParam("did") Integer did,
30                             @RequestParam("dname") String dname) {
31         Employee employee = new Employee();
32         employee.setId(id);
33         employee.setLastName(lastName);
34         employee.setEmail(email);
35         employee.setGender(gender);
36         Department department = new Department(did, dname);
37         employee.setDepartment(department);
38         Map<Integer, Employee> es = EmployeeDao.employees;
39         es.put(id, employee);
40         
41         return employeeDao.get(id);
42     }
43      @RequestMapping(value="http://www.mamicode.com/deleteEmployee", method=RequestMethod.GET)
44      @ResponseBody
45     public Collection<Employee> deleteEmployee(@RequestParam("id") Integer id){
46 //        Map<Integer, Employee> es = EmployeeDao.employees;
47 //        es.remove(id);
48         employeeDao.delete(id);
49         
50        return employeeDao.getAll();
51    }
52 
53 }

运行结果:

技术分享

翻译json格式:

 1 [
 2     {
 3         "id": 1002,
 4         "lastName": "E-BB",
 5         "email": "bb@163.com",
 6         "gender": 1,
 7         "department": {
 8             "id": 102,
 9             "departmentName": "D-BB"
10         }
11     },
12     {
13         "id": 1003,
14         "lastName": "E-CC",
15         "email": "cc@163.com",
16         "gender": 0,
17         "department": {
18             "id": 103,
19             "departmentName": "D-CC"
20         }
21     },
22     {
23         "id": 1004,
24         "lastName": "E-DD",
25         "email": "dd@163.com",
26         "gender": 0,
27         "department": {
28             "id": 104,
29             "departmentName": "D-DD"
30         }
31     },
32     {
33         "id": 1005,
34         "lastName": "E-EE",
35         "email": "ee@163.com",
36         "gender": 1,
37         "department": {
38             "id": 105,
39             "departmentName": "D-EE"
40         }
41     }
42 ]

3、修改员工信息

 1 package com.guigu.springmvc.crud.handlers;
 2 
 3 import java.util.Collection;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import com.guigu.springmvc.crud.dao.EmployeeDao;
14 import com.guigu.springmvc.crud.entities.Department;
15 import com.guigu.springmvc.crud.entities.Employee;
16 
17 @Controller
18 public class EmployeeHandler {
19     @Autowired
20     private EmployeeDao employeeDao;
21     
22     
23     @RequestMapping(value="http://www.mamicode.com/addEmployee",method=RequestMethod.GET)
24     @ResponseBody
25     public Employee addEmployee(@RequestParam("id") Integer id, 
26                             @RequestParam("name") String lastName,
27                             @RequestParam("email") String email, 
28                             @RequestParam("gender") Integer gender,
29                             @RequestParam("did") Integer did,
30                             @RequestParam("dname") String dname) {
31         Employee employee = new Employee();
32         employee.setId(id);
33         employee.setLastName(lastName);
34         employee.setEmail(email);
35         employee.setGender(gender);
36         Department department = new Department(did, dname);
37         employee.setDepartment(department);
38         Map<Integer, Employee> es = EmployeeDao.employees;
39         es.put(id, employee);
40         
41         return employeeDao.get(id);
42     }
43     @RequestMapping(value="http://www.mamicode.com/updateEmployee",method=RequestMethod.GET)
44          @ResponseBody
45          public Collection<Employee> updateEmployee(@RequestParam("id") Integer id,
46                                     @RequestParam("email") String email){
47              Employee employee = employeeDao.get(id);
48              employee.setEmail(email);
49              Map<Integer, Employee> es = EmployeeDao.employees;
50              es.put(id, employee);
51              return employeeDao.getAll();
52          }
53 }

运行结果:

技术分享

翻译json格式:

 1 [
 2     {
 3         "id": 1001,
 4         "lastName": "E-AA",
 5         "email": "123@qq.com",
 6         "gender": 1,
 7         "department": {
 8             "id": 101,
 9             "departmentName": "D-AA"
10         }
11     },
12     {
13         "id": 1002,
14         "lastName": "E-BB",
15         "email": "bb@163.com",
16         "gender": 1,
17         "department": {
18             "id": 102,
19             "departmentName": "D-BB"
20         }
21     },
22     {
23         "id": 1003,
24         "lastName": "E-CC",
25         "email": "cc@163.com",
26         "gender": 0,
27         "department": {
28             "id": 103,
29             "departmentName": "D-CC"
30         }
31     },
32     {
33         "id": 1004,
34         "lastName": "E-DD",
35         "email": "dd@163.com",
36         "gender": 0,
37         "department": {
38             "id": 104,
39             "departmentName": "D-DD"
40         }
41     },
42     {
43         "id": 1005,
44         "lastName": "E-EE",
45         "email": "ee@163.com",
46         "gender": 1,
47         "department": {
48             "id": 105,
49             "departmentName": "D-EE"
50         }
51     }
52 ]

4、查找员工信息

 1 package com.guigu.springmvc.crud.handlers;
 2 
 3 import java.util.Collection;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.bind.annotation.ResponseBody;
13 
14 import com.guigu.springmvc.crud.dao.EmployeeDao;
15 import com.guigu.springmvc.crud.entities.Department;
16 import com.guigu.springmvc.crud.entities.Employee;
17 
18 @Controller
19 public class EmployeeHandler {
20     @Autowired
21     private EmployeeDao employeeDao;
22     
23     
24     @RequestMapping(value="http://www.mamicode.com/addEmployee",method=RequestMethod.GET)
25     @ResponseBody
26     public Employee addEmployee(@RequestParam("id") Integer id, 
27                             @RequestParam("name") String lastName,
28                             @RequestParam("email") String email, 
29                             @RequestParam("gender") Integer gender,
30                             @RequestParam("did") Integer did,
31                             @RequestParam("dname") String dname) {
32         Employee employee = new Employee();
33         employee.setId(id);
34         employee.setLastName(lastName);
35         employee.setEmail(email);
36         employee.setGender(gender);
37         Department department = new Department(did, dname);
38         employee.setDepartment(department);
39         Map<Integer, Employee> es = EmployeeDao.employees;
40         es.put(id, employee);
41         
42         return employeeDao.get(id);
43     }
44     @RequestMapping(value="http://www.mamicode.com/getEmployee/{id}",method=RequestMethod.GET)
45          @ResponseBody
46          public Employee getEmployee(@PathVariable Integer id){
47              return employeeDao.get(id);
48          }
49          
50          @RequestMapping(value="http://www.mamicode.com/getAllEmployee",method=RequestMethod.GET)
51          @ResponseBody
52          public Collection<Employee> getAllEmployee(){
53              return employeeDao.getAll();
54          }
55      }

运行结果:

技术分享

注:由于此方法将id参数写入路径中,所以在测试时,要将id直接放入地址中即可。

翻译json格式:

 1 {
 2     "id": 1001,
 3     "lastName": "E-AA",
 4     "email": "aa@163.com",
 5     "gender": 1,
 6     "department": {
 7         "id": 101,
 8         "departmentName": "D-AA"
 9     }
10 }

技术分享

 1 [
 2     {
 3         "id": 1001,
 4         "lastName": "E-AA",
 5         "email": "aa@163.com",
 6         "gender": 1,
 7         "department": {
 8             "id": 101,
 9             "departmentName": "D-AA"
10         }
11     },
12     {
13         "id": 1002,
14         "lastName": "E-BB",
15         "email": "bb@163.com",
16         "gender": 1,
17         "department": {
18             "id": 102,
19             "departmentName": "D-BB"
20         }
21     },
22     {
23         "id": 1003,
24         "lastName": "E-CC",
25         "email": "cc@163.com",
26         "gender": 0,
27         "department": {
28             "id": 103,
29             "departmentName": "D-CC"
30         }
31     },
32     {
33         "id": 1004,
34         "lastName": "E-DD",
35         "email": "dd@163.com",
36         "gender": 0,
37         "department": {
38             "id": 104,
39             "departmentName": "D-DD"
40         }
41     },
42     {
43         "id": 1005,
44         "lastName": "E-EE",
45         "email": "ee@163.com",
46         "gender": 1,
47         "department": {
48             "id": 105,
49             "departmentName": "D-EE"
50         }
51     }
52 ]

 :直接在浏览器中测试时,请求方式只能用GET,正常情况下,删除的请求方式是DELETE,更新的请求方式是PUT

springmvc之使用RESTFUL—CRUD(creat-read-update-delete)——后端版