学习SpringMVC——你们要的REST风格的CRUD来了

来源:互联网 发布:sysbench mysql 测试 编辑:程序博客网 时间:2024/05/19 07:09

如果说前面是准备调料,洗菜,切菜,摆盘,那么今天就来完整的上道菜,主要说的是基于REST风格实现数据的增删改查(CRUD)操作。

用料 

  entity:Employee + Department

  对于这样的实体类大家肯定很熟悉,一个Employee就是一个员工,一个Department就是一个部门,一个员工只属于一个部门,一个部门可以对应多个员工。

  Employee.java

  

  Department.java

  

  dao层:除了entity层,我们还需要有dao层,用于对于entity的增删改查的操作,这里对应的有EmployeeDao和DepartmentDao下载

  EmployeeDao.java

该dao提供了:1. 添加保存一个employee——save方法

       2. 获取所有员工信息——getAll方法

       3. 根据指定员工id查找员工——get方法

       4. 根据指定员工id删除员工——delete方法

 

  DepartmentDao.java下载

  该dao提供了:1. 查找所有部门信息——getDepartments

       2. 根据指定id查找相应的部门信息——getDepartment

 

  另外这里需要用到的所有jar包都在这里下载

 

 

  用料齐活了, 就开始做菜了,要做的分别是REST清蒸C(添加记录)、REST油炸R(查询记录)、REST红烧U(更新记录)和REST水煮D(删除记录)。

 

1. 查询记录

  这个很简单,跟前几篇介绍的套路差不多,新建一个index.jsp,放入一个用于查询的超链接下载

1
<a href="emps">list all employees</a><br/>

 

相应的在Handler中需要添加映射支持,这里新建一个EmployeeHandler类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.jackie.springmvc.crud.handlers;
 
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.jackie.springmvc.crud.dao.EmployeeDao;
 
@Controller
public class EmployeeHandler {
     
    @Autowired
    private EmployeeDao employeeDao;
     
    @RequestMapping("/emps")
    public String list(Map<String, Object> map){
        map.put("employees", employeeDao.getAll());
        return "list";
    }
 
}

注意

  这里RequestMapping中没有指明RequestMethod的请求类型,默认是GET方式。下载

 

  最终我们需要将查询的数据解析出来放到页面上,这个页面就是list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:if test="${empty requestScope.employees}">
        没有员工信息
    </c:if>
    <c:if test="${!empty requestScope.employees }">
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <th>ID</th>
                <th>LastName</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Department</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
             
            <c:forEach items="${requestScope.employees }" var="emp">
                <tr>
                    <td>${emp.id }</td>
                    <td>${emp.lastName }</td>
                    <td>${emp.email }</td>
                    <td>${emp.gender==0?'Female':'Male' }</td>
                    <td>${emp.department.departmentName }</td>
                    <td><a href="emp/${emp.id}">Edit</a></td>
                    <td><a class="delete" href="emp/${emp.id}">Delete</a></td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
    <br><br>
</body>
</html>

  这里通过表格的方式显示,表格的每一行显示一个员工记录。

  如果Handler层没有传入employee,则显示“没有员工信息”。

  如果从Handler层传入查询到的employee的信息,则通过遍历的方式查询每条记录并显示在对应的表格中。

 

 

  结果如下:下载

  这里查询了所有的员工信息,为简便起见,这里的员工信息并不是从数据库中读取的,而是在EmployeeDao中通过静态模块声明构造的,将每个员工信息放入一个map集合中。

 

2. 添加记录

  添加记录即在原有的数据集合基础上,再新建一个员工信息,我们需要为员工的每个属性填充值,所以我们需要一个页面来为这些字段赋值,这个页面就叫input.jsp。

  添加员工信息的入口链接就放在list.jsp中

1
<a href = "emp">add new employee</a><br/>

 

  同时在EmployeeHandler中添加映射下载

1
2
3
4
5
6
@RequestMapping(value="/emp", method=RequestMethod.GET)
public String input(Map<String, Object> map){
    map.put("departments", departmentDao.getDepartments());
    map.put("employee", new Employee());
    return "input";
}

 

  input.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form:form action="emp" method="POST" modelAttribute="employee">
    <!-- path 属性对应html表单标签的name属性 -->
    LastName: <form:input path="lastName"></form:input><br>
    Email:<form:input path="email"></form:input><br>
    <%
        Map<String, String> genders = new HashMap<String, String>();
        genders.put("1", "male");
        genders.put("0", "female");
        request.setAttribute("genders", genders);
    %>
    Gender:<form:radiobuttons path="gender" items="${genders }"/><br>
    Department:<form:select path="department.id" items="${departments }" itemLabel="departmentName" itemValue="id"></form:select><br>
    <input type="submit" value="Submit"/>
 </form:form>
 
</body>
</html>

 

0 0