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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 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);
}
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8天前
|
运维 监控 安全
云HIS医疗管理系统源码——技术栈【SpringBoot+Angular+MySQL+MyBatis】
云HIS系统采用主流成熟技术,软件结构简洁、代码规范易阅读,SaaS应用,全浏览器访问前后端分离,多服务协同,服务可拆分,功能易扩展;支持多样化灵活配置,提取大量公共参数,无需修改代码即可满足不同客户需求;服务组织合理,功能高内聚,服务间通信简练。
26 4
|
2天前
|
关系型数据库 MySQL Shell
备份 MySQL 的 shell 脚本(mysqldump版本)
【4月更文挑战第28天】
9 0
|
8天前
|
Java 索引
vscode + springboot + HTML 搭建服务端(二)
vscode + springboot + HTML 搭建服务端(二)
8 1
|
8天前
|
Java
vscode + springboot + HTML 搭建服务端(一)
vscode + springboot + HTML 搭建服务端(一)
16 1
|
13天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
15天前
|
XML 移动开发 前端开发
HTML版本
【4月更文挑战第16天】HTML版本
17 4
|
16天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
20天前
|
消息中间件 Java 关系型数据库
JAVA云HIS医院管理系统源码、基于Angular+Nginx+ Java+Spring,SpringBoot+ MySQL + MyCat
JAVA云HIS医院管理系统 常规模版包括门诊管理、住院管理、药房管理、药库管理、院长查询、电子处方、物资管理、媒体管理等,为医院管理提供更有力的保障。 HIS系统以财务信息、病人信息和物资信息为主线,通过对信息的收集、存储、传递、统计、分析、综合查询、报表输出和信息共享,及时为医院领导及各部门管理人员提供全面、准确的各种数据。
|
20天前
|
消息中间件 缓存 运维
java+saas模式医院云HIS系统源码Java+Spring+MySQL + MyCat融合BS版电子病历系统,支持电子病历四级
云HIS系统是一款满足基层医院各类业务需要的健康云产品。该产品能帮助基层医院完成日常各类业务,提供病患预约挂号支持、病患问诊、电子病历、开药发药、会员管理、统计查询、医生工作站和护士工作站等一系列常规功能,还能与公卫、PACS等各类外部系统融合,实现多层机构之间的融合管理。
40 1
|
21天前
|
监控 数据可视化 安全
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
环境实时数据、动态监测报警,实时监控施工环境状态,有针对性地预防施工过程中的环境污染问题,打造文明生态施工,创造绿色的生态环境。
15 0
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql