Springboot+html5+mysql的CRUD增删改查(基础版本详细,附带源码)(一)

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: Springboot+html5+mysql的CRUD增删改查(基础版本详细,附带源码)(一)

后台写的总体分为两个部分

第一部分:纯后台的代码实现CRUD(增删改查)

第二部分:前后端交互实现CRUD(增删改查)





先贴下公共的一些代码:

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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
<!--           maven 添加json-->
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
        </dependency>
        <!--jquery-webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--bootstrap-webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!--配置文件注入时使用后会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </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>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.6.RELEASE</version>
            </plugin>
        </plugins>
    </build>
</project>


application.yml 的数据库连接代码:

spring:
    datasource:
      name:
      url: jdbc:mysql://127.0.0.1:3306/login?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
      username: root
      password: root
    messages:
      ##message.propertiesi是默认的国际化配置文件,不需要特别指定路径
      basename: i18n.login
  ##禁用thymeleaf的缓存
    thymeleaf:
      cache: false
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.entity
server:
  port: 1234
  servlet:
    context-path: /


application.properties html的一些配置的代码

# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false


sql的代码:

/*
 Navicat Premium Data Transfer
 Source Server         : bendi
 Source Server Type    : MySQL
 Source Server Version : 50732
 Source Host           : localhost:3306
 Source Schema         : login
 Target Server Type    : MySQL
 Target Server Version : 50732
 File Encoding         : 65001
 Date: 24/05/2021 16:32:11
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `ID` int(11) NOT NULL COMMENT 'ID',
  `NAME` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字',
  `LOGIN` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '账号',
  `PASSWORD` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (2, '不知道', 'yan', '78578578');
INSERT INTO `user` VALUES (11, '11111', '555555', '44');
INSERT INTO `user` VALUES (22, '8861111', '33', '335');
INSERT INTO `user` VALUES (88, '88888888888', '33781', '335787');
INSERT INTO `user` VALUES (2054, '4524', '44444', '4242');
INSERT INTO `user` VALUES (8877, '888', '887', '878787');
SET FOREIGN_KEY_CHECKS = 1;


下面介绍第一部分无页面代码:


Controller

package com.example.demo.controller;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RequestMapping("user")
@RestController
public class UserController {
    //todo    ====================================后台调用---》   每个控制台上面有  自己复制即可
    @Autowired
    private UserService userService;
    /**
     * 查询所有的数据
     * @return
     *
     * http://localhost:1234/user/list
     */
    @RequestMapping("list")
    public List<UserEntity> findall(){
        return userService.findall();
    }
    /** 当使用@RequestMapping URI id 样式映射时,
     * 即 someUrl/{paramId}, id @Pathvariable注解绑定它传过来的值到方法的参数上。
     *
     * 根据ID查询
     * @param id
     * @return
     *
     * http://localhost:1234/user/select/2
     */
    @RequestMapping("/select/{id}")
    public String selectId(@PathVariable int id){
            return userService.getselectId(id).toString();
    }
    /**
     *
     * 根据ID删除
     * @param id
     * @return
     *
     * http://localhost:1234/user/delect/2
     */
    @RequestMapping("/delect/{id}")
    public String delectId(@PathVariable int id){
      int result=userService.delectId(id);
        if (result>=1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }
    /**
     * http://localhost:1234/user/index
     * 展示页面
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("index");
    }
    /**
     * http://localhost:1234/user/ahtml
     * 新增页面
     * @return
     */
    @RequestMapping("/ahtml")
    public ModelAndView adds(){
        return new ModelAndView("add");
    }
    /**
     *  新增数组
     * @param userEntity
     * @return
     *http://localhost:1234/user/adds
     *
     * http://localhost:1234/user/ahtml  用这个
     * 返回值是IdUserEntity(id=11, name=22, login=33, password=44)
     */
    @RequestMapping("/adds")
    public  String  add(UserEntity userEntity){
        String str = "返回值是"+"Id"+userEntity;
        System.out.println(str);
        int result=userService.addall(userEntity);
        if (result>=1){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }
    /**
     *  修改
     * @param userEntity
     * @return
     *
     * http://localhost:1234/user/updatess
     */
        @RequestMapping("/updatess")
        public  String Upate(UserEntity userEntity){
            //这里是需要修改的数据  如果id么有就算新增
            userEntity.setId("2");
            userEntity.setName("不知道");
            userEntity.setLogin("yan");
            userEntity.setPassword("78578578");
          int results=userService.update(userEntity);
            if(results==1){
                return "更新成功";
            }
            return "更新失败";
        }
}


实体类 entity

package com.example.demo.entity;
import lombok.Data;
@Data  //这里引用了data 所以不需要 get/set的方法了
public class UserEntity {
    private String id;//id
    private  String name;//姓名
    private String login; //账号
    private String password;// 密码
}


Service

package com.example.demo.service;
import com.example.demo.entity.UserEntity;
import java.util.List;
public interface UserService {
    /**
     * 查询所有的数据
     * @return
     */
    List<UserEntity> findall();
    /**
     * 根据iD查询数据
     * @param id
     * @return
     */
    UserEntity getselectId(int id);
    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    int delectId(int id);
    /**
     * 新增数据
     * @param userEntity
     * @return
     */
    int addall(UserEntity userEntity);
    /**
     * 修改数据
     * @param userEntity
     * @return
     */
    int update(UserEntity userEntity);
}
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
5月前
|
XML Java Nacos
Spring Boot 整合Nacos 版本兼容适配 史上最详细文档
本文介绍SpringBoot整合Nacos的完整流程,涵盖Nacos下载安装、配置中心与服务发现集成、版本兼容性问题及实战配置。重点解决SpringBoot 3.3.0与Nacos版本适配难题,推荐使用Spring Cloud Alibaba方案,并提供项目开源地址供参考学习。
|
4月前
|
Ubuntu 关系型数据库 MySQL
MySQL源码编译安装
本文详细介绍了MySQL 8.0及8.4版本的源码编译安装全过程,涵盖用户创建、依赖安装、cmake配置、编译优化等步骤,并提供支持多Linux发行版的一键安装脚本,适用于定制化数据库部署需求。
1007 4
MySQL源码编译安装
|
6月前
|
NoSQL 关系型数据库 MySQL
在Visual Studio Code中设置MySQL源码调试环境
以上步骤涵盖了在VS Code中设置MySQL源码调试环境的主要过程,是一个相对高级的任务,旨在为希望建立强大开发和调试环境的开发者提供指引。遵循这些步骤,将可以利用VS Code强大的编辑和调试功能来深入理解和改进MySQL数据库的底层实现。
505 0
|
11月前
|
关系型数据库 MySQL PHP
源码编译安装LAMP(HTTP服务,MYSQL ,PHP,以及bbs论坛)
通过以上步骤,你可以成功地在一台Linux服务器上从源码编译并安装LAMP环境,并配置一个BBS论坛(Discuz!)。这些步骤涵盖了从安装依赖、下载源代码、配置编译到安装完成的所有细节。每个命令的解释确保了过程的透明度,使即使是非专业人士也能够理解整个流程。
336 18
|
SQL 关系型数据库 MySQL
vb6读取mysql,用odbc mysql 5.3版本驱动
通过以上步骤,您可以在VB6中使用ODBC MySQL 5.3驱动连接MySQL数据库并读取数据。配置ODBC数据源、编写VB6代码
466 32
|
前端开发 JavaScript Java
springboot图书馆管理系统前后端分离版本
springboot图书馆管理系统前后端分离版本
218 12
|
关系型数据库 MySQL Linux
MySQL版本升级(8.0.31->8.0.37)
本次升级将MySQL从8.0.31升级到8.0.37,采用就地升级方式。具体步骤包括:停止MySQL服务、备份数据目录、下载并解压新版本的RPM包,使用`yum update`命令更新已安装的MySQL组件,最后启动MySQL服务并验证版本。整个过程需确保所有相关RPM包一同升级,避免部分包遗漏导致的问题。官方文档提供了详细指导,确保升级顺利进行。
1398 16
|
JavaScript 安全 Java
java版药品不良反应智能监测系统源码,采用SpringBoot、Vue、MySQL技术开发
基于B/S架构,采用Java、SpringBoot、Vue、MySQL等技术自主研发的ADR智能监测系统,适用于三甲医院,支持二次开发。该系统能自动监测全院患者药物不良反应,通过移动端和PC端实时反馈,提升用药安全。系统涵盖规则管理、监测报告、系统管理三大模块,确保精准、高效地处理ADR事件。
569 1
|
关系型数据库 MySQL
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
mysql 5.7.x版本查看某张表、库的大小 思路方案说明
293 5

推荐镜像

更多