springboot + mybatis + thymeleaf 学生管理系统 (增删改查)

简介: 本文主要讲解如何用mysql+springboot+mybatis+thymeleaf引擎模板实现一个增删改查的操作.流程如下:制作数据库数据表=>配置pom+application.properties+application.yml+maven=>(非必选)在idea中加数据库=>写增删改查接口+postman测试接口=>做html样式+ajax交互

前言


本文主要讲解如何用mysql+springboot+mybatis+thymeleaf引擎模板实现一个增删改查的操作.流程如下:


制作数据库数据表=>


配置pom+application.properties+application.yml+maven=>(非必选)在idea中加数据库=>写增删改查接口+postman测试接口=>做html样式+ajax交互




提示:以下是本篇文章正文内容,下面案例可供参考



一、后端实现过程

1.做数据库和数据表

-- 创建名为 manager 的数据库
CREATE DATABASE IF NOT EXISTS manager;
-- 使用 manager 数据库
USE manager;
-- 创建名为 student 的数据表
CREATE TABLE IF NOT EXISTS student (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) DEFAULT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



在上述代码中,我们主要实现了创建数据库和数据表的操作.创建成功后的数据库如下图所示.




2.配置maven、pom、application.properties以及application.yml


maven配置:


点击File=>setting=>搜索maven=>找到修改下图画圈位置,如果没有则也可以用idea自带配置



pom配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.student</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


在上述代码中,我们除了基本配置外,还配置了mybatis/mysql/lombok,分别有高效数据管理/数据存储/省略getset方法的功能.

application.properties

//springboot别名
spring.application.name=springboot-w
//端口号
server.port=8082
//数据库引擎
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//用的数据库和编码格式等
spring.datasource.url=jdbc:mysql://localhost:3306/manager?characterEncoding=utf-8&&useSSL=false
//数据库账户名
spring.datasource.username=root
//密码
spring.datasource.password=x5
//mapper.xml的路径
mybatis.mapper-locations=classpath:mapper/*.xml


application.yml

thymeleaf:
  prefix:
    classpath: /templates   # 访问template下的html文件需要配置模板,映射


3.写接口和测试


写接口流程:pojo(实体类)=>mapper接口=>mapper.xml=>service接口=>serviceImpl实体类=>controller控制层=>测试

下面的具体流程的代码:


pojo类


@Data
public class student {
    private Integer id;
    private String name;
}


@Data是lombod注解,旨在不需要写getset方法了.


mapper接口


@Mapper
public interface stumapper {
    public List<student> querystu();
    public boolean addstu(student stu);
    public boolean updstu(student stu);
    public boolean delstu(int id);
}


mapper.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="com.student.mapper.stumapper">
    <insert id="addstu" parameterType="com.student.pojo.student">
        INSERT INTO student(id,name) VALUES (#{id},#{name})
    </insert>
    <update id="updstu">
        update student set id=#{id},name=#{name} where id=#{id}
    </update>
    <delete id="delstu">
        delete from student where id=#{id}
    </delete>
    <select id="querystu" resultType="com.student.pojo.student">
        select * from student
    </select>
</mapper>


service


public interface service {
    List<student> querystu();
    boolean addstu(student stu);
    boolean updstu(student stu);
    boolean delstu(int id);
}


serviceImpl实体类

@Service
public class serviceImpl implements service {
    @Autowired
    stumapper m;
    @Override
    public List<student> querystu()
    {
        return m.querystu();
    }
    @Override
    public boolean addstu(student stu){
        return m.addstu(stu);
    }
    @Override
    public boolean updstu(student stu)
    {
        return m.updstu(stu);
    }
    @Override
    public boolean delstu(int id)
    {
        return m.delstu(id);
    }
}



controller控制层

@RestController
public class controller {
    @Autowired
    service s;
    @RequestMapping("querystu")
    public List<student> querystu()
    {
        return s.querystu();
    }
    @RequestMapping("addstu")
    public boolean addstu(student stu)
    {
        return s.addstu(stu);
    }
    @RequestMapping("updstu")
    public boolean updstu(student stu)
    {
        return s.updstu(stu);
    }
    @RequestMapping("delstu")
    public boolean delstu(int id)
    {
        return s.delstu(id);
    }
}


二、前端实现过程(thymeleaf模板+html+ajax)


1.html


代码如下:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        function queryData() {
            // 发送 AJAX 请求
            $.ajax({
                url: 'http://localhost:8082/querystu',
                type: 'GET',
                dataType: 'json',
                success(res){
                    var html='';
                    for(var i =0;i<res.length;i++)
                    {
                        html+='<tr><td>'+res[i].id+'</td>'+'<td>'+res[i].name+'</td><td> <button onclick="del('+res[i].id+')">删除</button></td></tr>';
                    }
                        console.log(html);
                    $('#tt').html(html);
                },
                error: function(xhr, status, error) {
                    console.error(error); // 处理错误情况
                }
            });
        }
        function add(){
            const id=document.getElementById('InputId').value;
            const name=document.getElementById('InputName').value;
            $.ajax({
                url: `http://localhost:8082/addstu?id=${id}&name=${name}`,
                type: 'post',
                dataType: 'json',
                success: function(res) {
                    console.log(res); // 将响应数据输出到控制台
                    queryData()
                },
                error: function(xhr, status, error) {
                    console.error(error); // 处理错误情况
                }
            });
            document.getElementById('InputId').value='',
                document.getElementById('InputName').value=''
        }
        function upd(Id,Name){
            const id=document.getElementById('InputupdId').value;
            const name=document.getElementById('InputupdName').value;
            $.ajax({
                url: `http://localhost:8082/updstu?id=${id}&name=${name}`,
                type: 'post',
                dataType: 'json',
                success: function(res) {
                    console.log(res); // 将响应数据输出到控制台
                    queryData()
                },
                error: function(xhr, status, error) {
                    console.error(error); // 处理错误情况
                }
            });
            document.getElementById('InputupdId').value='',
                document.getElementById('InputupdName').value=''
        }
        function del(Id){
            // const id=document.getElementById('InputdelId').value;
            const id=Id;
            $.ajax({
                url: `http://localhost:8082/delstu?id=${id}`,
                type: 'post',
                dataType: 'json',
                success: function(res) {
                    console.log(res); // 将响应数据输出到控制台
                    queryData()
                },
                error: function(xhr, status, error) {
                    console.error(error); // 处理错误情况
                }
            });
        }
        queryData()
    </script>
</head>
<body>
<!--<button onclick="queryData()">查询</button>-->
<div>
        <input type="text" value="" placeholder="请输入id" id="InputId"/>
        <input type="text" value="" placeholder="请输入名称" id="InputName">
    <button onclick="add()">添加</button>
</div>
<div>
    <input type="text" value="" placeholder="请输入id" id="InputupdId"/>
    <input type="text" value="" placeholder="请输入名称" id="InputupdName">
    <button onclick="upd()">修改</button>
</div>
<table>
    <thead>
    <tr>
        <th>学生Id</th>
        <th>学生名称</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tt">
    </tbody>
</table>
</body>
<style>
    table{
        margin:20px;
        border-collapse: collapse;
    }
    th{
        background-color: #cccccc;
    }
    tr{
        text-align: center;
        border: 1px solid #ccc;
    }
    td{
        padding:20px;
        border:1px solid #ccc;
    }
    button{
        background-color: white;
        width:70px;
        border-radius:20px;
        border:1px solid #ccc;
    }
    button :hover{
        background-color: #cccccc;
    }
</style>
</html>


2.代码解析和实现截图

在html代码中,我们主要利用onclick点击事件和jquery框架中的ajax交互.

jquery中ajax的语法格式

$.ajax({

url: 接口名,

type: ‘get’,

dataType: ‘json’,

success: function(res) {

console.log(res); // 将响应数据输出到控制台

queryData()

},

error: function(xhr, status, error) {

console.error(error); // 处理错误情况

}

});




总结


单表增删改查,springboot+html简单使用


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
|
12月前
|
监控 安全 JavaScript
2025基于springboot的校车预定全流程管理系统
针对传统校车管理效率低、信息不透明等问题,本研究设计并实现了一套校车预定全流程管理系统。系统采用Spring Boot、Java、Vue和MySQL等技术,实现校车信息管理、在线预定、实时监控等功能,提升学校管理效率,保障学生出行安全,推动教育信息化发展。
|
12月前
|
JavaScript Java 关系型数据库
基于springboot的高校运动会系统
本系统基于Spring Boot、Vue与MySQL,实现高校运动会报名、赛程安排及成绩管理的全流程信息化,提升组织效率,杜绝信息错漏与冒名顶替,推动体育赛事智能化发展。
|
12月前
|
JavaScript 安全 Java
基于springboot的大学生兼职系统
本课题针对大学生兼职信息不对称、权益难保障等问题,研究基于Spring Boot、Vue、MySQL等技术的兼职系统,旨在构建安全、高效、功能完善的平台,提升大学生就业竞争力与兼职质量。
|
12月前
|
JavaScript Java 关系型数据库
基于springboot的美食城服务管理系统
本系统基于Spring Boot、Java、Vue和MySQL技术,构建集消费者服务、商家管理与后台监管于一体的美食城综合管理平台,提升运营效率与用户体验。
|
12月前
|
Java 关系型数据库 MySQL
基于springboot的网咖网吧管理系统
本文探讨了基于Java、MySQL和SpringBoot的网吧管理系统的设计与实现。随着信息化发展,传统管理方式难以满足需求,而该系统通过先进技术提升管理效率、保障数据安全、降低运营成本,具有重要意义。
|
12月前
|
JavaScript Java 关系型数据库
基于springboot的摄影师分享交流社区系统
本系统基于Spring Boot与Vue构建摄影师分享交流平台,旨在打造专业社区,支持作品展示、技术交流与合作互动。采用Java、MySQL等成熟技术,提升摄影爱好者创作水平,推动行业发展。
基于SpringBoot的社区老年食堂系统
针对老龄化社会饮食难题,智慧社区老年食堂系统应运而生。融合Spring Boot、Vue、Java与MySQL技术,实现餐饮服务智能化、个性化,提升老年人生活质量与幸福感,推动社区养老服务升级。
基于springboot的民宿预定管理系统
本研究针对民宿市场管理效率低、信息化程度不足等问题,设计并实现基于Spring Boot、Vue和MySQL的民宿预订管理系统。系统提升预订效率与用户体验,助力行业数字化转型。