SpringMVC_實現簡單的增刪改查

實現簡單的增刪改查

1:創建User的javabean

複製代碼

package com.doaoao.bean;
public class User {
    private String name;
    private String phone;
    private String address;

    public User(){}

    public User(String name, String phone, String address) {
        this.name = name;
        this.phone = phone;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

複製代碼

2:創建一個初始化數據的工具類

複製代碼

package com.doaoao.util;

import com.doaoao.bean.User;

import java.util.*;

public class DataUtil {
    private static HashMap<String, User> dataMap = new HashMap<String ,User>();

    // 模擬初始化數據
    static {
        User user1 = new User("zhangsan","10086","北京");
        User user2 = new User("lisi","10000","福建");
        User user3 = new User("wangwu","10001","廣東");
        User user4 = new User("zhaoliu","10011","天津");

        dataMap.put("1",user1);
        dataMap.put("1",user2);
        dataMap.put("1",user3);
        dataMap.put("1",user4);
    }

    // 查找全部數據
    public static HashMap<String ,User> findAll(){
        return dataMap;
    }

    // 根據id查找
    public static User findById(String id){
        return dataMap.get(id);
    }

    // 創建用戶
    public static void create(User user)throws Exception{
        Set<Map.Entry<String, User>> entries = dataMap.entrySet();
        Iterator<Map.Entry<String, User>> iterator = entries.iterator();
        int max = 3;
        while (iterator.hasNext()) {
            Map.Entry<String, User> next = iterator.next();
            int i = Integer.parseInt(next.getKey());
            if (i > max) {
                max = i;
            }
        }
        dataMap.put(++max+"",user);
    }

    // 修改用戶數數據
    public static void update(String id,User user) throws Exception{
        dataMap.put(id,user);
    }

    // 根據id刪除用戶
    public static void delete(String id)throws Exception{
        dataMap.remove(id);
    }
}

複製代碼

3:創建Controlle

複製代碼

package com.doaoao.controller;

import com.doaoao.bean.User;
import com.doaoao.util.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.HashMap;

@Controller
public class UserController {

    // 查找所有用戶
    @RequestMapping("/findAll.do")
    public ModelAndView findAll()throws Exception{
        HashMap<String, User> allUser = DataUtil.findAll();

        ModelAndView mv = new ModelAndView();
        mv.addObject("allUser",allUser);
        mv.setViewName("user_list");
        return mv;
    }

    // 根據id查找
    @RequestMapping("/findById.do")
    public ModelAndView findById(String id) throws Exception{
        ModelAndView mv = new ModelAndView();
        User user = DataUtil.findById(id);
        HashMap<String ,User> allUser = new HashMap<>();
        allUser.put(id,user);

        mv.addObject("allUser",allUser);
        mv.addObject("id",id);
        mv.setViewName("user_list");
        return mv;
    }

    // 新增
    @RequestMapping("/create.do")
    public String create(User user)throws Exception{
        DataUtil.create(user);
        return "redirect:findALl.do";
    }

    // 修改
    @RequestMapping("/update.do")
    public String update(String id,User user) throws Exception{
        DataUtil.update(id,user);
        return "redirect:findALl.do";
    }

    // 刪除
    @RequestMapping("/delete.do")
    public String delete(String id) throws Exception{
        DataUtil.delete(id);
        return "redirect:findALl.do";
    }
}

複製代碼

4: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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">



    <mvc:annotation-driven/>
    <!-- 註冊組件掃描器 -->
    <context:component-scan base-package="com.doaoao.*"/>
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>


    <!--內部視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

複製代碼

5:user_list.jsp文件

複製代碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link href="../css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container theme-showcase" role="main">
    <div class="page-header">
        <form id="queryById" action="/findById.do" method="post">
            <input type="text" name="id" placeholder="請輸入id" value="${id}">
            <button id="query" type="button" class="btn btn-sm btn-primary">查詢</button>
            <a id="add" type="button" class="btn btn-sm btn-success" href="/jsp/user_add.jsp">添加</a>
        </form>

    </div>
    <div class="row">
        <div class="">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>編號</th>
                    <th>姓名</th>
                    <th>手機</th>
                    <th>生日</th>
                    <th>地址</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${allUser}" var="user">
                    <tr>
                        <td>${user.key}</td>
                        <td>${user.value.name}</td>
                        <td>${user.value.phone}</td>
                        <td>${user.value.birthday}</td>
                        <td>${user.value.address}</td>
                        <td>
                            <a type="button" class="btn btn-sm btn-info" href="/delete.do?id=${user.key}">刪除</a>
                            <a type="button" class="btn btn-sm btn-warning" href="/goUpdate.do?id=${user.key}">修改</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script>
    $(function () {

        $("#query").click(function () {
            $("#queryById").submit();
        })

    });

</script>
</body>
</html>

複製代碼

6:user_add.jsp文件

複製代碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="page-header"></div>
<div class="container">
    <form action="/create.do" method="post" style="max-width: 330px;padding: 15px;margin: 0 auto;">
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" class="form-control" id="name" name="name">
        </div>
        <div class="form-group">
            <label for="phone">手機:</label>
            <input type="text" class="form-control" id="phone" name="phone">
        </div>
        <div class="form-group">
            <label for="birthday">生日:</label>
            <input type="date" class="form-control" id="birthday" name="birthday">
        </div>
        <div class="form-group">
            <label for="address">地址:</label>
            <input type="text" class="form-control" id="address" name="address">
        </div>
        <input type="submit" value="提交">
    </form>
</div>
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
</body>
</html>

複製代碼

7:user_update.jsp文件

複製代碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="page-header"></div>
<div class="container">
    <form action="/update.do" method="post" style="max-width: 330px;padding: 15px;margin: 0 auto;">
        <input name="id" type="hidden" value="${id}">
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" class="form-control" id="name" name="name" value="${user.name}">
        </div>
        <div class="form-group">
            <label for="phone">手機:</label>
            <input type="text" class="form-control" id="phone" name="phone" value="${user.phone}">
        </div>
        <div class="form-group">
            <label for="birthday">生日:</label>
            <input type="date" class="form-control" id="birthday" name="birthday" value="${user.birthday}">
        </div>
        <div class="form-group">
            <label for="address">地址:</label>
            <input type="text" class="form-control" id="address" name="address" value="${user.address}">
        </div>
        <input type="submit" value="提交">
    </form>
</div>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
</body>
</html>

複製代碼

 本筆記參考自:小猴子老師教程 http://www.monkey1024.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章