spring boot CRUD(查询全部,查询ID,模糊查询,删除id,全部删除,新增多条/单条 修改)(一)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: spring boot CRUD(查询全部,查询ID,模糊查询,删除id,全部删除,新增多条/单条 修改)(一)

只是简单的写了一下后段的一些接口;


项目架构:

架构介绍:


pom.xml


<?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.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.DemoCrud</groupId>
    <artifactId>democurd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>democurd</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-jdbc</artifactId>
        </dependency>
        <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>
        <!-- mybatis整合Springboot -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>



数据库sql


/*
 Navicat Premium Data Transfer
 Source Server         : deptuser
 Source Server Type    : MySQL
 Source Server Version : 80020
 Source Host           : localhost:3306
 Source Schema         : cruddemo
 Target Server Type    : MySQL
 Target Server Version : 80020
 File Encoding         : 65001
 Date: 08/07/2022 23:11:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(0) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
  `age` int(0) NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
  `newdate` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (11, '44', 77, '44', '2022-07-08 01:55:42');
INSERT INTO `user` VALUES (16, '88', 88, '8888', '2022-07-08 00:00:00');
INSERT INTO `user` VALUES (18, '44', 77, '44', '2022-07-08 02:12:43');
INSERT INTO `user` VALUES (21, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (22, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (23, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (24, '44', 77, '44', '2022-07-08 02:15:14');
INSERT INTO `user` VALUES (36, 'tt', 44, '12345456', '2022-07-03 00:08:25');
SET FOREIGN_KEY_CHECKS = 1;


application.properties


我这边暂时么写前段这个不写也行,我习惯的贴下

# 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


application.yml


spring:
  datasource:
    name:
    url: jdbc:mysql://127.0.0.1:3306/cruddemo?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.democru.pojo
server:
  port: 6688
  servlet:
    context-path: /


启动类:


package com.example.democrud.democurd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(scanBasePackages = "com.example") //指定项目扫描不加也可以
@MapperScan(basePackages = "com.example.democrud.democurd.usermapper") //mapper 经常不自动加载故加了下
//@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //排除掉数据库自动加载
public class DemocurdApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemocurdApplication.class, args);
    }
}


controller 控制台:


package com.example.democrud.democurd.controller;
import com.example.democrud.democurd.pojo.userdemo;
import com.example.democrud.democurd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//@RestController
@Controller
@RequestMapping("/user/")
public class CrudController {
    @Autowired
    public UserService userService;
    @RequestMapping("abc")
    @ResponseBody
    public String hello() {
        return "hello  你好";
    }
    /**
     * 查询  :模糊查询 单点查询  查询全部
     * 删除
     * 增加 数据
     * <p>
     *
     *
     * @param
     */
//    public static void main(String[] args) {
//
//    }
    /**
     * 查询全部
     *http://localhost:6688/user/findall
     * @return
     */
    @ResponseBody
    @GetMapping("findall")
    public List<userdemo> findAll() {
        System.out.println("执行controller");
        List<userdemo> findall = userService.findall();
        System.out.println(findall);
        return findall;
    }
    /**
     * 查询id
     *http://localhost:6688/user/find/id
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("findid/{id}")
    public List<userdemo> findId(@PathVariable(value = "id") int id) {
        System.out.println("id" + id);
        List<userdemo> le = userService.findid(id);
        return le;
    }
    /**
     * http://localhost:6688/user/find/{like=》模糊查询的数据}
     * 模糊查询 相关数据
     * @param like
     * @return
     */
    @ResponseBody
    @GetMapping("find/{like}")
    public List<userdemo> findLike(@PathVariable String like) {
        System.out.println("like" + like);
        List<userdemo> likes = userService.findlike(like);
        return likes;
    }
    /**
     * http://localhost:6688/user/delectall
     * 删除全部
     * @return
     */
    @ResponseBody
    @GetMapping("delectall")
    public String delectAll() {
        System.out.println("删除全部");
        int delect = userService.delectAll();
        // 返回删除的条数 String.valueOf(delect)
        if (delect > 0) {
            String le = "删除成功,删除了" + delect + "条数据";
            return le;
        } else {
            return "删除失败,或者无可删除数据";
        }
    }
    /**
     * http://localhost:6688/user/delect/id(写删除的id)
     * 根据id删除
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("delect/{id}")
    public String delectId(@PathVariable int id) {
        int le = userService.delectId(id);
        if (le > 0) {
            String lee = "删除成功删除了一条数据 id为" + id;
            return lee;
        } else {
            return "删除失败";
        }
    }
    /**
     * http://localhost:6688/user/delectBatches
     * 刪除多個 傳入多個id即可
     * @return
     */
    @ResponseBody
    @GetMapping("delectBatches")
    public String delectBatches() {
        int le = 0;
        int[] nee = {34, 35};
        for (int i = 0; i < nee.length; i++) {
            le = userService.delectId(nee[i]);
        }
        if (le > 0) {
            String lee = "删除成功";
            return lee;
        } else {
            return "删除失败";
        }
    }
    /**
     * http://localhost:6688/user/addOnce
     *新增一个 或者批量新增
     * @param userdemo
     * @return
     */
    @ResponseBody
    @GetMapping("addOnce") // 一般采用put 请求但是这个demo不涉及前后交互
    public int addOnce(userdemo userdemo) {
        List<userdemo> userdemos = new ArrayList<>();
        Date lee = new Date();
        //新增一个或者多个
        userdemos.add(new userdemo("21", "44", "77", "44", lee));
        userdemos.add(new userdemo("22", "44", "77", "44", lee));
        userdemos.add(new userdemo("23", "44", "77", "44", lee));
        userdemos.add(new userdemo("24", "44", "77", "44", lee));
        int lo = userService.addOnce(userdemos);
        if (lo > 0) {
            System.out.println("您新增了" + lo + "条数据");
        } else {
            System.out.println("无新增");
        }
        return lo;
    }
    /**
     * http://localhost:6688/user/update  修改
     * @param userdemo
     * @return
     */
    @ResponseBody
    @GetMapping("update")
    public int update(userdemo userdemo) {
  //后端直接伪数据  进行赋值  发送新的数据
        userdemo.setId("16");
        userdemo.setName("88");
        userdemo.setAge("88");
        userdemo.setPassword("8888");
        userdemo.setNewdate(new Date());
        System.out.println(userdemo.toString());
         int le=userService.updateid(userdemo);
         if (le>0){
             System.out.println("修改完成");
         }
         return le;
    }
}


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
XML Java 数据库
【Spring】通过JdbcTemplate实现CRUD操作
【Spring】通过JdbcTemplate实现CRUD操作
32 0
|
1月前
|
前端开发 Java Spring
Java 新手如何使用Spring MVC 中的查询字符串和查询参数
Java 新手如何使用Spring MVC 中的查询字符串和查询参数
|
5月前
|
Java 关系型数据库 MySQL
基于springboot的教务查询管理系统。Javaee项目,springboot项目。
基于springboot的教务查询管理系统。Javaee项目,springboot项目。
|
3月前
|
Java
Springboot整合Elasticsearch 7.X 复杂查询
这里使用Springboot 2.7.12版本,Elasticsearch为7.15.0。
Springboot整合Elasticsearch 7.X 复杂查询
|
3月前
|
Java
SpringBoot中进行elasticSearch查询,使用QueryBuilders构建各类条件查询
SpringBoot中进行elasticSearch查询,使用QueryBuilders构建各类条件查询
67 0
|
3月前
|
存储 自然语言处理 Java
SpringBoot集成ElasticSearch时分页排序查询时遇到的坑每次只能返回10条数据
SpringBoot集成ElasticSearch时分页排序查询时遇到的坑每次只能返回10条数据
56 0
|
3月前
|
Java 数据库连接 API
Spring Boot整合Spring Data JPA进行CRUD和模糊查询
Spring Boot整合Spring Data JPA进行CRUD和模糊查询
38 0
|
3月前
|
前端开发 JavaScript 算法
springboot+vue+mybatis-plus+axios实现商品的CRUD
springboot+vue+mybatis-plus+axios实现商品的CRUD
36 0
|
3月前
|
Java 数据库 数据安全/隐私保护
springboot垃圾分类查询管理系统
springboot垃圾分类查询管理系统
|
3月前
|
Java Spring
springboot操作LDAP,查询指定ou下面的cn属性
springboot操作LDAP,查询指定ou下面的cn属性