动力节点旗下在线教育品牌  |  咨询热线:400-8080-105 学Java全栈,上蛙课网
首页 > 文章

Spring MVC实现简单增删查改

09-02 17:37 68浏览
举报 T字号
  • 大字
  • 中字
  • 小字

基本的增删查改是每一个开发框架都必备的基础功能,Spring MVC也不例外。Spring MVC本身是不带有数据库的需要整合数据库才能实现高级的数据库功能,但Spring MVC本身还是可以实现一些简单增删查改。下面是Spring MVC实现简单增删查改的具体步骤:

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 dataMap = new HashMap();

 

    // 模拟初始化数据

    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 findAll(){

        return dataMap;

    }

 

    // 根据id查找

    public static User findById(String id){

        return dataMap.get(id);

    }

 

    // 创建用户

    public static void create(User user)throws Exception{

        Set> entries = dataMap.entrySet();

        Iterator> iterator = entries.iterator();

        int max = 3;

        while (iterator.hasNext()) {

            Map.Entry 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 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 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: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>

5: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>

上面的7个步骤对于Spring MVC实现简单增删查改缺一不可,所以我们如果真的需要实现Spring MVC简单增删查改,就要把上面的7个步骤一一解读,并且不出差错的运用到你的Spring MVC框架中去。当然,为了解决大家的类似问题,在本站的Spring MVC视频教程中有更全面的解析,能更加快速的帮你解决问题。

0人推荐
共同学习,写下你的评论
0条评论
携代码的男孩纸
程序员携代码的男孩纸

24篇文章贡献108038字

作者相关文章更多>

推荐相关文章更多>

DOM渲染的详细过程

QCode09-04 14:38

CSS水平和垂直居中技巧大梳理

Code大师09-04 14:50

mui的input框在IOS系统下无法聚焦或点击多次才能聚焦

不写代码你养我啊08-23 11:14

推荐的-视.频播放器以及在线客服

不写代码你养我啊09-17 18:02

谈谈java多线程的三大特性

要学习了06-18 18:13

发评论

举报

0/150

取消