SpringBoot-Thymeleaf-MySQL-SpringMVC实现网页端的数据库信息的增删改查(JavaEE巨详细版)

Hello,欢迎来到我的博客,既然选择了远方,便只顾风雨兼程

**

(源码已上传资源,0积分获取,觉得有用的点个赞嘛~源码点击这里

**
上一篇 博客只实现了数据库信息的网页端展示,本篇博客我们来更详细的写一下学生信息管理系统网页端跳转增删改查

https://blog.csdn.net/m0_51242270/article/details/127546231?spm=1001.2014.3001.5501
这个连接是之前写的在idea控制台进行增删改查的,有兴趣的友友可以康康~(Java-spring数据库编程(idea)实现学生账号登录以及管理员增删改查功能)
https://blog.csdn.net/m0_51242270/article/details/127950083?spm=1001.2014.3001.5501
这个连接是之前写的网页端展示数据库全部信息的,同样哟,这两篇博客都是本次实验必备的基础,需要自取哈~

下面进入主题(内容有点多,感谢欣赏~)

Java-spring数据库编程(idea)实现学生账号登录以及增删改查功能

本次实验内容是网页端输入网址进入登录页面,输入用户名root密码123456之后登陆成功进入学生信息管理系统 ,页面有增删改查等按钮,点击之后触发事件,跳转到相应的页面,选择你想进行的操作,从而实现数据库信息的增删查改。(如果有想要源码的友友,后台踢踢,我会把这个给优化一下,最近时间不很充裕,没有花时间搞这个,等寒假整理出来哟!)

本次内容的大纲为:

1、工程结构与结果展示
2、主要代码粘贴(需要完整版代码压缩包,可后台踢我哟,经常在线哒~
3、本次实验总结

1、工程结构与结果展示

先看一下工程结构:
在这里插入图片描述
以及运行结果如下:

登录主页面
在这里插入图片描述

在这里插入图片描述

输入用户及密码:root 123456
在这里插入图片描述

跳转
在这里插入图片描述
主操作页面:
在这里插入图片描述

根据id查询
在这里插入图片描述

结果如下:
在这里插入图片描述

根据姓名查询:
在这里插入图片描述

显示查询到的信息表格
在这里插入图片描述

添加学生信息
在这里插入图片描述

跳转页面
在这里插入图片描述

提交信息
在这里插入图片描述
添加成功
在这里插入图片描述

更新学生信息
在这里插入图片描述

更新页面
在这里插入图片描述

提交更新
在这里插入图片描述

更新成功
在这里插入图片描述
删除
在这里插入图片描述

选中后两个
在这里插入图片描述

删除成功!
在这里插入图片描述

2、主要代码粘贴

LoginController.java

package cn.edu.ldu.springbootweb.conctroller;

import cn.edu.ldu.springbootweb.dao.StudentDao;
import cn.edu.ldu.springbootweb.entity.Student;
import cn.edu.ldu.springbootweb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/")
public class LoginController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    Student student;
    /*@GetMapping("/toLoginPage")
    public String toLoginPage(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
    @PostMapping("/toLoginPage")
    public String toLoginPage(User user,Model model){
        if(user.getName().equals("root")&&user.getPass().equals("123456")){
            List<Student> studentList=studentDao.findAll();
            model.addAttribute("studentList", studentList);
            return "studentList";
        }
        else{
            return "error";
        }
    }*/
    @GetMapping("/login")
    public ModelAndView login(ModelAndView modelAndView){
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/toLoginPage")
    public String login(ModelAndView modelAndView, @Valid Student student, BindingResult bindingResult,Model model){
        if(bindingResult.hasErrors()){
            modelAndView.addObject("error",bindingResult.getFieldError().getDefaultMessage());
            modelAndView.setViewName("login");
            return "login";
        }
        String username = student.getUsername();
        String password = student.getPassword();

        if(!"root".equals(username)){
            modelAndView.addObject("error","无此用户!");
            modelAndView.setViewName("login");
            return "login";
        }
        if(!"123456".equals(password)){
            modelAndView.addObject("error","密码错误!");
            modelAndView.setViewName("login");
            return "login";
        }
        if("root".equals(username)&&"123456".equals(password)){
            List<Student> studentList=studentDao.findAll();
            model.addAttribute("studentList", studentList);
            modelAndView.addObject("username",username);
            modelAndView.setViewName("studentList");
            return "studentList";
        }

//        System.out.println(studentList);

        return "login";
    }
/*    @RequestMapping( "/toLoginPage")
    public String toLoginPage() {
        return "login";//跳转到order.html页面
    }
    @RequestMapping("/login")
    @ResponseBody
    public String findOrderWithUser(User user) {
        String username = student.getUser().getUserName();
        String pass = student.getUser().getPass();
        if(username == "root" && pass =="123456"){
            return "studentList";
        }
        else{
            return "用户名或密码错误,请重试!";
        }

    }*/

    @RequestMapping("/add")
    public String add(Student student,Model model) {
        student.getId();
        student.getUsername();
        student.getPassword();
        student.getCourse();
        studentDao.insert(student);
        List<Student> studentList=studentDao.findAll();
        model.addAttribute("studentList", studentList);
        return "studentList";
    }
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "add";
    }

    /*@RequestMapping ("/findById")
    @ResponseBody
    public String findById(Student student) {
        BigInteger id = student.getId();
        return student.toString();
    }
    @RequestMapping("/findOver")
    public String findOver(){
        return "studentList";
    }*/
    /*@RequestMapping("/toFindIdPage")
    public String toFindIdPage(){
        return "studentList";
    }*/
    @RequestMapping("/findById")
    @ResponseBody
    public Student findById(Student student){
        BigInteger userId = student.getUser().getUserId();
        student = studentDao.findById(userId);
        return student;
    }  //未添加不存在提醒

    @RequestMapping("/manageDelete")
    public String DeleteStudent(BigInteger[] ids,Student student,Model model) {
        for(BigInteger id : ids){
            student.getId();
            studentDao.delete(id);
        }
        List<Student> studentList = studentDao.findAll();
        model.addAttribute("studentList", studentList);
        return "studentList";
    }
    @RequestMapping("/findByName")
    public String findByName(Student student,Model model){
        String username = student.getUser().getUserName();
        List<Student> studentList= studentDao.findByName(username);
        model.addAttribute("studentList", studentList);
        return "studentList";
    }  //未添加不存在提醒

    @RequestMapping("/update")
    public String update(Student student,Model model) {
        studentDao.update(student);
        List<Student> studentList=studentDao.findAll();
        model.addAttribute("studentList", studentList);
        return "studentList";
    }
    @RequestMapping("/toUpdate")
    public String update() {
        return "update";
    }

}

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理系统</title>
    <link rel="stylesheet" th:href="@{/login/css/loginin.css}"type="text/css">
</head>
<body>

<div class="container">
    <div class="login-wrapper">

        <div class="header">Login</div>
        <div class="form-wrapper">
            <form th:action="@{/toLoginPage}" method="post">
            <input type="text" name="username" placeholder="username" class="input-item">
            <input type="password" name="password" placeholder="password" class="input-item">
            <input type="submit" name="submit" placeholder="submit" class="btn" value="登录">
<!--                <div id="login" class="btn">Login</div>-->
            </form>

        </div>
        <div class="msg">
<!--            Don't have account? <a href="">Sign up</a>-->
            <span th:text="${currentYear}"></span>
        </div>
    </div>
</div>

</body>
</html>

studentList.html

<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Student</title>
    <link rel="stylesheet" th:href="@{/login/css/mainPage.css}"type="text/css">
</head>
<div class="container">
    <body>

        <table border="1" align="center" bgcolor="#fafad2">
            <br>
            <br>
            <h1 align="center" >学生信息管理系统(SpringBoot)</h1>
            <div align="center" >
                <form th:action="@{/findById}" method="post" >
                    根据id查询:<input type="text" name="user.userId">
                <input type="submit" value="查询"></form>
                <form th:action="@{/findByName}" method="post" >
                    根据name查询:<input type="text" name="user.userName">
                    <input type="submit" value="查询"></form>
                <br>
                根据course查询:
                <select>
                    <option>-请选择-</option>
                    <option>Java</option>
                    <option>数据结构</option>
                    <option>Python</option>
                </select>
                <input type="submit" value="查询">
                <form th:action="@{/toAdd}" method="post" >
                    <input type="submit" value="添加学生信息"><br>
                </form>
                <form th:action="@{/toUpdate}" method="post">
                        <input type="submit" value="更新学生信息" >
                </form>

            </div>

            <br>
            <form th:action="@{manageDelete}" method="post">


            <tr>
                <th>选择</th>
                <th>num</th>
                <th>id</th>
                <th>username</th>
                <th>password</th>
                <th>course</th>
            </tr>
            <tr th:each="student,stat:${studentList}">
                <td>
                    <!--多选框-->
                    <input type="checkbox"  name="ids"
                           th:value="${student.id}">
                </td>
                <td th:text="${stat.count}"></td>
                <td th:text="${student.id}"></td>
                <td th:text="${student.username}"></td>
                <td th:text="${student.password}"></td>
                <td th:text="${student.course}"></td>

            </tr>
            <tr>
                <td>
                    <input type="submit" value="删除" >
                </td>
            </tr>
            </form>

        </table>
    </body>
</div>
</html>

update.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>update</title>
  <link rel="stylesheet" th:href="@{/login/css/loginin.css}"type="text/css">
</head>
<body>

<div class="container">
  <div class="login-wrapper">

    <div class="header">update</div>
    <div style="width:600px;height:100%;margin-left:10px;">
      <form th:action="@{/update}" method="post">
        <!--        form-control给input添加这个class后就会使用bootstrap自带的input框-->
        id:<input  type="text" name="id"><br>
        username:<input  type="text" name="username"><br>
        <!--注意参数的拼接-->
        password:<input type="text" name="password"><br>
        course:<input type="text" name="course"><br>
        <input type="submit" value="提交">
      </form>
    </div>
  </div>
</div>

</body>
</html>



add.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加student</title>
  <link rel="stylesheet" th:href="@{/login/css/loginin.css}"type="text/css">
</head>
<body>

<div class="container">
  <div class="login-wrapper">

    <div class="header">add</div>
    <div style="width:600px;height:100%;margin-left:10px;">
      <form th:action="@{/add}" method="post">
        <!--        form-control给input添加这个class后就会使用bootstrap自带的input框-->
        id:<input  type="text" name="id"><br>
        username:<input  type="text" name="username"><br>
        <!--注意参数的拼接-->
        password:<input type="text" name="password"><br>
        course:<input type="text" name="course"><br>
        <input type="submit" value="提交">
      </form>
    </div>
  </div>
</div>

</body>
</html>



3、本次实验总结

由于一些原因,这里只粘贴了部分主要的代码,完整版代码可后台踢踢,有帮助的话,可以留下你的666嘛
代码写的也比较清楚,对代码哪里不懂的可以留言哈,看到会及时回复的,Bye~

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyInn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值