基于SpringBoot3实现MyBatis-Plus(SSMP)整合快速入门CURD(增删改查)

news2025/4/26 4:17:03

目录

一、快速搭建SpringBoot-Web工程脚手架。

1.1 Spring Initializr 初始化工程。(官方提供)

1.2 工程脚手架初始化详细步骤。(IDEA2024.1.1)

二、MyBatis-Plus的特性与快速上手。

2.1 官网地址与基本特性。

2.2 快速上手技术栈基础。

2.3 Spring Boot2 的 MyBatis-Plus Starter 依赖。

2.4 Spring Boot3 的 MyBatis-Plus Starter 依赖。(当前使用)

三、基于SpringBoot实现SSMP整合入门CURD步骤。

3.1 引入Lombok、Druid核心起步依赖。

3.2 编写application.yml配置文件。

3.3 包结构创建与分层。

3.4 实体类Student。

 3.5 controller层。(RESTful风格的增删改查请求接口)

3.6 service层。(MyBatis-Plus开发极简化)

<1>StudentService。(继承 IService 接口)

<2>StudentServiceImpl。(继承 ServiceImpl 类。实现 StudentService 接口)

3.7 mapper层。(MyBatis-Plus开发极简化)

<1>StudentMapper。(继承 BaseMapper 接口)

3.8 封装响应结果类Result。

3.9 增删改查接口的测试与响应。

<1>http://localhost:8080/student/list。(查询所有)

<2>http://localhost:8080/student/selectById。(根据id查询)

<3>http://localhost:8080/student/save。(新增)

修改配置文件中连接池的空闲检测参数。

<4>http://localhost:8080/student/update。(修改)

<5>http://localhost:8080/student/remove。(删除)​


一、快速搭建SpringBoot-Web工程脚手架。

1.1 Spring Initializr 初始化工程。(官方提供)


1.2 工程脚手架初始化详细步骤。(IDEA2024.1.1)










二、MyBatis-Plus的特性与快速上手。

2.1 官网地址与基本特性。
  • MyBatis-Plus 🚀 为简化开发而生


2.2 快速上手技术栈基础。

2.3 Spring Boot2 的 MyBatis-Plus Starter 依赖。
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.11</version>
</dependency>
2.4 Spring Boot3 的 MyBatis-Plus Starter 依赖。(当前使用)
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.11</version>
</dependency>

三、基于SpringBoot实现SSMP整合入门CURD步骤。

3.1 引入Lombok、Druid核心起步依赖。
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
3.2 编写application.yml配置文件。


# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/hyl?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
    username: root
    password: root123
    type: com.alibaba.druid.pool.DruidDataSource  # 声明Druid数据源类型声明

# mybatis-plus配置
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: AUTO
3.3 包结构创建与分层。


3.4 实体类Student。
  • 实体类 Student 与数据库表 tb_student 对应,每个属性对应表中的一个字段。
  • @TableName 注解指定了表名,@TableField 注解指定了属性对应表中的字段名

  • 代码示例。
package com.hyl.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("tb_student")  // 表明该类对应数据库中的 tb_student 表
@Data
public class Student {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer age;
    @TableField("is_boy")  // 表明该属性对应数据库表中的 is_boy 字段
    private String isBoy;
}
 3.5 controller层。(RESTful风格的增删改查请求接口)
  • http://localhost:8080/student/list。(查询所有)
  • http://localhost:8080/student/selectById。(根据id查询)
  • http://localhost:8080/student/save。(新增)
  • http://localhost:8080/student/update。(修改)
  • http://localhost:8080/student/remove。(删除)
package com.hyl.controller;

import com.hyl.pojo.Result;
import com.hyl.pojo.Student;
import com.hyl.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 查询所有信息
     * @return
     */
    @GetMapping("/list")
    public Result selectList(){
        List<Student> list = studentService.list();
        return Result.success("查询成功",list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("selectById")
    public Result selectById(@RequestParam Integer id){
        Student student = studentService.getById(id);
        return Result.success("查询成功",student);
    }

    /**
     * 新增
     * @param student
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Student student){
        boolean flag = studentService.save(student);
        return flag ? Result.success("新增成功") : Result.error();
    }

    /**
     * 修改
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Student student){
        boolean flag = studentService.updateById(student);
        return flag ? Result.success("修改成功") : Result.error();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/remove")
    public Result delete(@RequestParam Integer id){
        boolean flag = studentService.removeById(id);
        return flag ? Result.success("删除成功") : Result.error();
    }
    
}
3.6 service层。(MyBatis-Plus开发极简化)
<1>StudentService。(继承 IService 接口)
  • 注意:IService<Student实体类>
package com.hyl.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.hyl.pojo.Student;

public interface StudentService extends IService<Student> {
}

<2>StudentServiceImpl。(继承 ServiceImpl 类。实现 StudentService 接口)
  • 注意:ServiceImpl<StudentMapper接口,Student实体类>
package com.hyl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyl.mapper.StudentMapper;
import com.hyl.pojo.Student;
import com.hyl.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
3.7 mapper层。(MyBatis-Plus开发极简化)
<1>StudentMapper。(继承 BaseMapper 接口)
  • 注意:BaseMapper<Student实体类>
package com.hyl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hyl.pojo.Student;

public interface StudentMapper extends BaseMapper<Student> {
}
3.8 封装响应结果类Result。
package com.hyl.pojo;

import lombok.Data;

//封装响应结果类
@Data
public class Result {
    private String code;
    private String msg;
    private Object data;

    public Result() {
    }

    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //响应success(无数据返回)
    public static Result success(){
        return new Result("200","操作成功");
    }
    //响应success(无数据返回)
    public static Result success(String msg){
        return new Result("200",msg);
    }
    //响应success(有数据返回)
    public static Result success(Object data) {
        return new Result("200","操作成功",data);
    }
    //响应success(有数据返回)
    public static Result success(String msg,Object data) {
        return new Result("200",msg,data);
    }

    //响应error(无数据返回)
    public static Result error(){
        return new Result("500","操作失败");
    }
    //响应error(自定义异常信息提示)
    public static Result error(String code, String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
3.9 增删改查接口的测试与响应。
<1>http://localhost:8080/student/list。(查询所有)



<2>http://localhost:8080/student/selectById。(根据id查询)


<3>http://localhost:8080/student/save。(新增)



  • 修改配置文件中连接池的空闲检测参数。
  • WARN Druid的日志暂时不需要管。因为这是 Druid 的正常机制,用于清理无效连接,避免连接泄漏。
  • 可以在 application.yml 中修改连接池的空闲检测参数,避免过早关闭正常连接。
spring:
  datasource:
    druid:
      # 配置连接空闲时间(单位:毫秒),默认 30 分钟(1800000ms),可适当延长
      min-evictable-idle-time-millis: 1800000 
      # 检测空闲连接的间隔时间(单位:毫秒),默认 60 秒(60000ms)
      time-between-eviction-runs-millis: 60000 
      # 申请连接时是否检测有效性(开启后,避免获取到无效连接)
      test-on-borrow: true 

<4>http://localhost:8080/student/update。(修改)



<5>http://localhost:8080/student/remove。(删除)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2342962.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

主题模型三大基石:Unigram、LSA、PLSA详解与对比

&#x1f31f; 主题模型演进图谱 文本建模三阶段&#xff1a; 词袋模型 → 潜在语义 → 概率生成 Unigram → LSA → PLSA → LDA &#x1f4e6; 基础模型&#xff1a;Unigram模型 核心假设 文档中每个词独立生成&#xff08;词袋假设&#xff09; 忽略词语顺序和语义关联 …

基准指数选股策略思路

一种基于Python和聚宽平台的量化交易策略&#xff0c;主要包含以下内容&#xff1a; 1. 导入必要的库 - 导入jqdata和jqfactor库用于数据获取和因子计算。 - 导入numpy和pandas库用于数据处理。 2. 初始化函数 - 设置基准指数为沪深300指数。 - 配置交易参数&#xff0c;如使用…

SAP接口超时:对 FOR ALL ENTRIES IN 的优化

SAP接口超时 经分析要10多分钟以上才出结果&#xff0c;且是这个语句耗时较长&#xff1a; SELECTaufnrmatnrbdmnglgortmeinschargFROM resbINTO CORRESPONDING FIELDS OF TABLE lt_lylcddxhFOR ALL ENTRIES IN lt_lylcddWHERE aufnr IN r_aufnr发现RESB有420万条记录&#xf…

Shell 脚本入门:从零开始写自动化脚本

目录 一、Shell 、Shell 命令、Shell 脚本 二、常用 Shell 命令与注释写法 三、echo 命令的使用 四、Shell 变量类型 五、变量与参数使用 六、读取用户输入 七、算术运算 八、条件判断与流程控制 九、循环结构 十、函数定义与调用 一、Shell 、Shell 命令、Shell 脚本…

【最新版】西陆健身系统源码全开源+uniapp前端

一.系统介绍 一款基于UniappThinkPHP开发健身系统&#xff0c;支持多城市、多门店&#xff0c;包含用户端、教练端、门店端、平台端四个身份。有团课、私教、训练营三种课程类型&#xff0c;支持在线排课。私教可以通过上课获得收益&#xff0c;在线申请提现功能&#xff0c;无…

常见移动机器人底盘模型对比(附图)

1. 概述 底盘模型驱动场景优势劣势双轮差速两轮驱动室内AGV结构简单、成本低转弯半径大&#xff0c;易打滑四轮差速四轮独立驱动复杂地形无人车全方位转向&#xff0c;机动性强控制复杂&#xff0c;能耗高阿克曼模型前轮转向后驱户外无人驾驶车高速稳定性好转弯半径大&#xf…

【MongoDB】windows安装、配置、启动

&#x1fa9f; 一、下载 MongoDB 安装包 打开官方地址&#xff1a; &#x1f449; https://www.mongodb.com/try/download/community 配置下载选项&#xff1a; 选项设置Version最新&#xff08;默认就好&#xff09;OSWindowsPackageMSI&#xff08;推荐&#xff09; 点击【D…

GitLab_密钥生成(SSH-key)

目录 1.密钥命令 2.自定义路径 3.输2次密码 4.查看公钥&#xff1a;&#xff08;打开文件&#xff09; 5. 把公钥&#xff0c;放到GitLab上面 6.填写公钥标题 7.点击 Add key 按钮 8. 验证添加是否成功 9. 测试 SSH 连接 10.彩蛋&#xff08;把ssh-key添加到python文…

【视频时刻检索】Text-Video Retrieval via Multi-Modal Hypergraph Networks 论文阅读

Text-Video Retrieval via Multi-Modal Hypergraph Networks 论文阅读 ABSTRACT1 INTRODUCTION2 PRELIMINARIES3 OUR FRAMEWORK3.1 Multi-Modal Hypergraph Networks3.2 Variational Inference 4 EXPERIMENT6 CONCLUSION 文章信息&#xff1a; 发表于&#xff1a;WSDM 24 原文…

BUUCTF-[GWCTF 2019]re3

[GWCTF 2019]re3 查壳&#xff0c;64位无壳 然后进去发现主函数也比较简单&#xff0c;主要是一个长度校验&#xff0c;然后有一个mprotect函数&#xff0c;说明应该又是Smc&#xff0c;然后我们用脚本还原sub_402219函数处的代码 import idc addr0x00402219 size224 for …

C++入侵检测与网络攻防之暴力破解

目录 1.nessus扫描任务 2.漏洞信息共享平台 3.nessus扫描结果 4.漏扫报告的查看 5.暴力破解以及hydra的使用 6.crunch命令生成字典 7.其他方式获取字典 8.复习 9.关于暴力破解的防御的讨论 10.pam配置的讲解 11.pam弱密码保护 12.pam锁定账户 13.shadow文件的解析 …

管理100个小程序-很难吗

20公里的徒步-真难 群里的伙伴发起了一场天目山20公里徒步的活动&#xff0c;想着14公里都轻松拿捏了&#xff0c;思考了30秒后&#xff0c;就借着春风带着老婆孩子就出发了。一开始溪流清澈见底&#xff0c;小桥流水没有人家&#xff1b;青山郁郁葱葱&#xff0c;枯藤老树没有…

如何在Linux用libevent写一个聊天服务器

废话少说&#xff0c;先看看思路 因为libevent的回调机制&#xff0c;我们可以借助这个机制来创建bufferevent来实现用户和用户进行通信 如果成功连接后我们可以直接在listener回调函数里创建一个bufferevent缓冲区&#xff0c;并为每个缓冲区设置相应的读回调和事件回调&…

马浩棋:产通链CT-Chain 破局不动产 RWA,引领数智金融新变革

全球不动产 RWA 数智金融高峰论坛上马浩棋先生致辞 在全球不动产 RWA 数智金融高峰论坛暨产通链 CT-Chain 上链首发会的现场&#xff0c;犀牛世纪集团&#xff08;香港&#xff09;有限公司董事会主席马浩棋成为众人瞩目的焦点。此次盛会汇聚了全球金融、区块链及不动产领域的…

学习整理在centos7上安装mysql8.0版本教程

学习整理在centos7上安装mysql8.0版本教程 查看linux系统版本下载mysql数据库安装环境检查解压mysql安装包创建MySQL需要的目录及授权新增用户组新增组用户配置mysql环境变量编写MySQL配置文件初始化数据库初始化msyql服务启动mysql修改初始化密码配置Linux 系统服务工具,使My…

SIEMENS PLC程序解读 -BLKMOV (指定长度数据批量传输)

1、程序代码 2、程序解读 这段西门子 PLC 程序&#xff08;程序段 10&#xff09;实现了基于条件的数据块移动功能&#xff0c;具体解释如下&#xff1a; 条件触点&#xff1a; %M0.1 Always<>(TRUE)&#xff08;注释为 AT<>1&#xff09;&#xff1a;当 M0.1 的值…

初识HashMap

HashMap&#xff1a;无序&#xff0c;不重复&#xff0c;无索引 HashMap小练习&#xff1a; import java.text.ParseException; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer;import static java.lang.Math.abs;public cla…

隧道高清晰广播如何提升行车安全体验?

在隧道中行驶时&#xff0c;驾驶员常面临回声干扰、语音模糊、信息过载等问题&#xff0c;传统广播系统可能不仅未能提供有效信息&#xff0c;反而因噪音增加驾驶压力。高清晰广播通过数字降噪、动态音效优化等技术&#xff0c;显著改善驾驶员的听觉体验&#xff0c;进而提升行…

从0开始搭建一套工具函数库,发布npm,支持commonjs模块es模块和script引入使用

文章目录 文章目标技术选型工程搭建1. 初始化项目2. 安装开发依赖3. 项目结构4. 配置文件tsconfig.json.eslintrc.jseslint.config.prettierrc.jsrollup.config.cjs创建 .gitignore文件 设置 Git 钩子创建示例工具函数8. 版本管理和发布9 工具函数测试方案1. 安装测试依赖2. 配…

Cadence学习笔记之---原理图设计基本操作

目录 01 | 引 言 02 | 环境描述 03 | 原理图工具介绍 04 | 原理图设计基本操作 05 | 生成页间引用 06 | 元件自动编号 07 | 结 尾 01 | 引 言 书接上回&#xff0c;在前文中讲述了怎样制作常用的库元件&#xff0c;如电阻、二极管&#xff0c;IC器件&#xff0c;以及怎…