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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 本文主要讲解如何用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简单使用


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
28天前
|
前端开发 JavaScript Java
springboot图书馆管理系统前后端分离版本
springboot图书馆管理系统前后端分离版本
39 12
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
67 6
|
26天前
|
JavaScript Java 测试技术
基于Java+SpringBoot+Vue实现的车辆充电桩系统设计与实现(系统源码+文档+部署讲解等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
57 6
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue的班级综合测评管理系统设计与实现(系统源码+文档+数据库+部署等)
✌免费选题、功能需求设计、任务书、开题报告、中期检查、程序功能实现、论文辅导、论文降重、答辩PPT辅导、会议视频一对一讲解代码等✌
40 4
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生体质测试管理系统设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
38 2
|
1月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
84 8
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的冬奥会科普平台设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
45 0
|
30天前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
56 2
|
4月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
207 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
4月前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
139 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块