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

简介: 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;
相关文章
|
人工智能 运维 Java
SpringBoot+MySQL实现动态定时任务
这是一个基于Spring Boot的动态定时任务Demo,利用spring-context模块实现任务调度功能。服务启动时会扫描数据库中的任务表,将任务添加到调度器中,并通过固定频率运行的ScheduleUpdater任务动态更新任务状态和Cron表达式。核心功能包括任务的新增、删除与Cron调整,支持通过ScheduledFuture对象控制任务执行。项目依赖Spring Boot 2.2.10.RELEASE,使用MySQL存储任务信息,包含任务基类ITask及具体实现(如FooTask),便于用户扩展运维界面以增强灵活性。
563 10
|
前端开发 Java 关系型数据库
基于Java+Springboot+Vue开发的鲜花商城管理系统源码+运行
基于Java+Springboot+Vue开发的鲜花商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的鲜花商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。技术学习共同进步
1041 7
|
JavaScript 前端开发 Java
制造业ERP源码,工厂ERP管理系统,前端框架:Vue,后端框架:SpringBoot
这是一套基于SpringBoot+Vue技术栈开发的ERP企业管理系统,采用Java语言与vscode工具。系统涵盖采购/销售、出入库、生产、品质管理等功能,整合客户与供应商数据,支持在线协同和业务全流程管控。同时提供主数据管理、权限控制、工作流审批、报表自定义及打印、在线报表开发和自定义表单功能,助力企业实现高效自动化管理,并通过UniAPP实现移动端支持,满足多场景应用需求。
1530 1
|
Ubuntu 关系型数据库 MySQL
MySQL源码编译安装
本文详细介绍了MySQL 8.0及8.4版本的源码编译安装全过程,涵盖用户创建、依赖安装、cmake配置、编译优化等步骤,并提供支持多Linux发行版的一键安装脚本,适用于定制化数据库部署需求。
2991 4
MySQL源码编译安装
|
供应链 JavaScript BI
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
这是一款专为小微企业打造的 SaaS ERP 管理系统,基于 SpringBoot+Vue+ElementUI+UniAPP 技术栈开发,帮助企业轻松上云。系统覆盖进销存、采购、销售、生产、财务、品质、OA 办公及 CRM 等核心功能,业务流程清晰且操作简便。支持二次开发与商用,提供自定义界面、审批流配置及灵活报表设计,助力企业高效管理与数字化转型。
1145 2
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
|
NoSQL 关系型数据库 MySQL
在Visual Studio Code中设置MySQL源码调试环境
以上步骤涵盖了在VS Code中设置MySQL源码调试环境的主要过程,是一个相对高级的任务,旨在为希望建立强大开发和调试环境的开发者提供指引。遵循这些步骤,将可以利用VS Code强大的编辑和调试功能来深入理解和改进MySQL数据库的底层实现。
839 0
|
机器学习/深度学习 数据采集 人机交互
springboot+redis互联网医院智能导诊系统源码,基于医疗大模型、知识图谱、人机交互方式实现
智能导诊系统基于医疗大模型、知识图谱与人机交互技术,解决患者“知症不知病”“挂错号”等问题。通过多模态交互(语音、文字、图片等)收集病情信息,结合医学知识图谱和深度推理,实现精准的科室推荐和分级诊疗引导。系统支持基于规则模板和数据模型两种开发原理:前者依赖人工设定症状-科室规则,后者通过机器学习或深度学习分析问诊数据。其特点包括快速病情收集、智能病症关联推理、最佳就医推荐、分级导流以及与院内平台联动,提升患者就诊效率和服务体验。技术架构采用 SpringBoot+Redis+MyBatis Plus+MySQL+RocketMQ,确保高效稳定运行。
1137 0
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。

推荐镜像

更多