• 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- 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- 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

  • 相关阅读:
    ASP脚本获取服务器全部参数列表说明
    HTML基础教程
    HTML5代码大全
    CSS 属性大全
    Web前端单词大全
    css常用代码大全
    曾国藩:诚敬静谨恒!
    鼠标经过显示菜单
    月入3000+项目
    右侧菜单显示隐藏
  • 原文地址:https://www.cnblogs.com/Doaoao/p/10707071.html
Copyright © 2020-2023  润新知