JAVA学生成绩管理系统(增删改查+Springboot+MySql+MyBatis+Thymeleaf)idea版(修改时数据的回显)

1 篇文章 0 订阅
1 篇文章 0 订阅

该项目源码地址:源码地址请点击这里哟!

布局界面:

 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>studentsScoreDemo</artifactId>
    <version>1.0-SNAPSHOT</version>



    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.7</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.6.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>

</project>

application.yml

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studentsscore?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: //密码自己自行添加
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
mybatis:
  type-aliases-package: org.example.pojo
  mapper-locations: /mapper/*Mapper.xml

showAll.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主界面</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <style type="text/css">
        h2{
            font-family:宋体;
            font-weight:normal;

        }
    </style>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <h2 class="text-center">
                学生成绩管理系统
            </h2>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-6 column"></div>
        <div class="col-md-4 column">
            <form th:action="@{/student/query}" method="post">
                班级名称:<input type="text"  name="cls" id="cls"/>
                <button type="submit" class="btn btn-default">查询</button>
            </form>
        </div>
        <div class="col-md-2 column">
            <a th:href="@{/student/toAddStudent}">添加学生成绩信息</a>
        </div>
    </div>
    <p></p><hr style="border: 5px solid green;"/><p></p>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table">
                <thead>
                <tr>
                    <th>
                        学号
                    </th>
                    <th>
                        姓名
                    </th>
                    <th>
                        班级
                    </th>
                    <th>
                        C语言
                    </th>
                    <th>
                        JAVA
                    </th>
                    <th>
                        Python
                    </th>
                    <th>
                        操作1
                    </th>
                    <th>
                        操作2
                    </th>
                </tr>
                </thead>
                <tbody>
                <tr class="success" th:each = "item : ${list}">
                    <td th:text="${item.id}"></td>
                    <td th:text="${item.name}"></td>
                    <td th:text="${item.cls}"></td>
                    <td th:text="${item.C}"></td>
                    <td th:text="${item.JAVA}"></td>
                    <td th:text="${item.Python}"></td>
                    <td>
                        <a th:href="@{/student/removeById(id=${item.id})}">删除</a>&nbsp;&nbsp;

                    </td>
                    <td>
                        <a th:href="@{/student/queryById(id=${item.id})}">修改</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

add.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加页面</title>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
  <div class="row clearfix">
    <div class="col-md-2 column">
    </div>
    <div class="col-md-8 column">
      <form class="form-horizontal" role="form" th:action="@{/student/addStudent}" method="post">
        <div class="form-group">
          <label class="col-sm-2 control-label">学号</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="id" id="id"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">姓名</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="name" id="name"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">班级</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="cls" id="cls"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">C语言</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="C" id="C"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">JAVA</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="JAVA" id="JAVA"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">Python</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="Python" id="Python"/>
          </div>
        </div>

        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">提交</button>
          </div>
        </div>
      </form>
    </div>
    <div class="col-md-2 column">
    </div>
  </div>
</div>
</body>
</html>

update.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>修改页面</title>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
  <div class="row clearfix">
    <div class="col-md-2 column">
    </div>
    <div class="col-md-8 column">
      <form class="form-horizontal" role="form" th:action="@{/student/changeStudent}" method="post">
        <input th:value="${student.id}" type="text" name="id"  id="id" hidden/>
        <div class="form-group">
          <label class="col-sm-2 control-label">姓名</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="name" id="name" th:value="${student.name}"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">班级</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="cls" id="cls" th:value="${student.cls}"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">C语言</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="C" id="C" th:value="${student.C}"/>
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label">JAVA</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="JAVA" id="JAVA" th:value="${student.JAVA}"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">Python</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="Python" id="Python" th:value="${student.Python}"/>
          </div>
        </div>

        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">修改</button>
          </div>
        </div>
      </form>
    </div>
    <div class="col-md-2 column">
    </div>
  </div>
</div>
</body>
</html>

studentMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.example.dao.StudentDao">
    <select id="selectAll" resultType="student">
        select * from stucore
    </select>
    <delete id="deleteById" parameterType="String">
        delete from stucore where id=#{id}
    </delete>
    <insert id="insertStudent" parameterType="student">
        insert into stucore(id,name,cls,C,JAVA,Python) values (#{id},#{name},#{cls},#{C},#{JAVA},#{Python})
    </insert>
    <select id="selectById" parameterType="String" resultType="student">
        select * from stucore where id=#{id}
    </select>
    <update id="updateStudent" parameterType="student">
        update stucore set name=#{name},cls=#{cls},C=#{C},JAVA=#{JAVA},Python=#{Python} where id=#{id}
    </update>
    <select id="select" parameterType="String" resultType="student">
        select * from stucore where stucore.cls like #{cls}
    </select>
</mapper>

MainApplication

package org.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages="org.example")
@MapperScan({"org.example.mapper"})
@MapperScan (basePackages = "org.example.dao")
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

StudentService

package org.example.service;

import org.example.pojo.Student;

import java.util.List;

public interface StudentService {
    public List<Student> queryAll();
    public void removeById(String id);
    public void addStudent(Student student);
    public Student queryById(String id);
    public void changeStudent(Student student);
    public List<Student> query(String cls);
}

StudentServiceImpl

package org.example.service.impl;

import org.example.dao.StudentDao;
import org.example.pojo.Student;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao stud;
    @Override
    public List<Student> queryAll() {
        return stud.selectAll();
    }

    @Override
    public void removeById(String id) {
        try{
            stud.deleteById(id);
        }catch (Exception ex){
            throw new RuntimeException("根据ID删除有异常");
        }
    }

    @Override
    public void addStudent(Student student) {
        try{
            stud.insertStudent(student);
        }catch (Exception ex){
            throw new RuntimeException("添加有异常");
        }
    }

    @Override
    public Student queryById(String id) {
        return stud.selectById(id);
    }

    @Override
    public void changeStudent(Student student) {
        try{
            stud.updateStudent(student);
        }catch (Exception ex){
            throw new RuntimeException("修改有异常");
        }
    }

    @Override
    public List<Student> query(String cls) {
        return stud.select(cls);
    }
}

Student

package org.example.pojo;

public class Student {
    private String id;
    private String name;
    private String cls;
    private String C;
    private String JAVA;
    private String Python;

    public Student() {
    }

    public Student(String id, String name, String cls, String c, String JAVA, String python) {
        this.id = id;
        this.name = name;
        this.cls = cls;
        C = c;
        this.JAVA = JAVA;
        Python = python;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getCls() {
        return cls;
    }

    public void setCls(String cls) {
        this.cls = cls;
    }

    public String getC() {
        return C;
    }

    public void setC(String c) {
        C = c;
    }

    public String getJAVA() {
        return JAVA;
    }

    public void setJAVA(String JAVA) {
        this.JAVA = JAVA;
    }

    public String getPython() {
        return Python;
    }

    public void setPython(String python) {
        Python = python;
    }
}

StudentDao

package org.example.dao;

import org.apache.ibatis.annotations.Mapper;
import org.example.pojo.Student;

import java.util.List;

@Mapper
public interface StudentDao {

    public List<Student> selectAll();
    public void deleteById(String id);
    public void insertStudent(Student student);
    public Student selectById(String id);
    public void updateStudent(Student student);
    public List<Student> select( String cls);

}

StudentController

package org.example.controller;

import org.example.pojo.Student;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService ss;
    @RequestMapping("/queryAll")
    public String queryAll(Model model){
        List<Student> list = ss.queryAll();
        model.addAttribute("list",list);
        return "showAll";
    }

    @RequestMapping("removeById")
    public String removeById(String id){
        try{
            ss.removeById(id);
            return "redirect:/student/queryAll";
        }catch (Exception ex){
            return "error";
        }
    }

    @RequestMapping("/toAddStudent")
    public String toAddUser(){
        return "add";
    }

    @RequestMapping("addStudent")
    public String addStudent(Student student){
        try{
            ss.addStudent(student);
            return "redirect:/student/queryAll";
        }catch (Exception ex){
            return "error";
        }
    }

    @RequestMapping("/queryById")
    public String queryById(String id, ModelMap modelMap){
        Student student = ss.queryById(id);
        modelMap.addAttribute("student",student);
        return "update";
    }

    @RequestMapping("changeStudent")
    public String changeStudent(Student student){
        try{
            ss.changeStudent(student);
            return "redirect:/student/queryAll";
        }catch (Exception ex){
            return "error";
        }
    }

    @RequestMapping("/query")
    public String query(String cls,Model model){

        if (cls == null){
            List<Student> list = ss.query("%");
            model.addAttribute("list", list);
        }else {
            List<Student> list = ss.query(cls);
            model.addAttribute("list",list);
        }

        return "showAll";
    }
}

MySql

效果图:

 

作为一位新手,感谢大家支持,我会继续努力给大家带来更多地精彩内容,谢谢!

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大不了_自己学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值