当前位置: 首页 > ai >正文

基于springboot3+mybatis整合,使用mybatisPlus插件自动完成简单的 增删改查操作

目录

一,添加mybatisPlus插件,构建标准化结构

一 ,添加mybatis-plus 依赖

二,在设置下载mybatisPlus插件

三,连接目标数据库,完成相关配置

二,完成简单的CRUD操作

1 查询

1.1 查询所有

1.2 查询单个

2 添加

3 修改

4 删除


一,添加mybatisPlus插件,构建标准化结构

一 ,添加mybatis-plus 依赖

          <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.11</version></dependency>

二,在设置下载mybatisPlus插件

setting设置-插件-MybatisPlus

点击应用,确定,重启IDEA,就可以了!

三,连接目标数据库,完成相关配置

Config Database

点击test connect

  • 发现连接成功!

Code Generator

点击code generatro 操作之后,就会自动生成 pojo,service,controller,mapper 包,以及resources文件夹下的mapper目录, 映射文件


二,完成简单的CRUD操作

案例中,我将直接在controller类注入使用@Autowired mapper接口成员变量 注入代理对象


案例中,操作的tb_user表

注意:案例中的操作都是比较简单的,如果使用的方法满足你的心意,可以使用条件构造器设定特殊条件,最后将条件构造器对象作为参数传递


完整代码

package com.it.heima.demo2.controller;import com.it.heima.demo2.mapper.TbUserMapper;
import com.it.heima.demo2.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** <p>*  前端控制器* </p>** @author author* @since 2025-04-26*/
@RestController
@RequestMapping("/tb-user")
public class TbUserController {// 注入mapper 接口@Autowiredprivate TbUserMapper tbUserMapper;@RequestMapping("/selectAll")//查询所有用户信息public void hello(){List<TbUser> tbUsers = tbUserMapper.selectList(null);for (TbUser tbUser : tbUsers) {System.out.println(tbUser);}}//查询单个@RequestMapping("/selectOne")public void selectOne(){
//        TbUser tbUser1 = new TbUser();
//        tbUser1.setName("tom");
//        tbUser1.setAge(23);TbUser tbUser = tbUserMapper.selectById(11);System.out.println(tbUser);}//添加@RequestMapping("/add")public void add(){TbUser tbUser = new TbUser();tbUser.setName("tom");tbUser.setAge(23);tbUser.setAddress("北京");tbUser.setEmail("12345678901@qq.com");tbUser.setQq("12345678901");tbUserMapper.insert(tbUser);}//修改@RequestMapping("/update")public void update(){TbUser tbUser = new TbUser();tbUser.setId(20);tbUser.setName("李四");tbUser.setAge(23);tbUser.setAddress("上海");tbUser.setPhoto("12345678901");tbUser.setEmail("12345678901@qq.com");tbUserMapper.updateById(tbUser);}//删除@RequestMapping("/delete")public void delete(){tbUserMapper.deleteById(20);}}

1 查询

1.1 查询所有
// 参数  是条件构造器,现在传递null,表示 不传递条件构造器对象只进行简单的全体查询
List<TbUser> tbUsers = tbUserMapper.selectList(null);
@RequestMapping("/selectAll")//查询所有用户信息public void hello(){List<TbUser> tbUsers = tbUserMapper.selectList(null);for (TbUser tbUser : tbUsers) {System.out.println(tbUser);}}

测试


1.2 查询单个
// 通过id查询用户
TbUser tbUser = tbUserMapper.selectById(1);

注意:该参数不仅可以简单的id 还可以是实体类对象

//查询单个@RequestMapping("/selectOne")public void selectOne(){TbUser tbUser = tbUserMapper.selectById(11);System.out.println(tbUser);}

测试


2 添加

// 参数传递实体类对象
tbUserMapper.insert(tbUser);
//添加@RequestMapping("/add")public void add(){TbUser tbUser = new TbUser();tbUser.setName("tom");tbUser.setAge(23);tbUserMapper.insert(tbUser);}

测试

3 修改

// 参数是实体类对象
tbUserMapper.updateById(tbUser);
//修改@RequestMapping("/update")public void update(){TbUser tbUser = new TbUser();tbUser.setId(20);tbUser.setName("李四");tbUser.setAge(23);tbUser.setAddress("上海");tbUser.setPhoto("12345678901");tbUser.setEmail("12345678901@qq.com");tbUserMapper.updateById(tbUser);}

测试


4 删除

// 通过传递id 删除某一行数据
tbUserMapper.deleteById(20);
//删除@RequestMapping("/delete")public void delete(){tbUserMapper.deleteById(20);}

测试

http://www.xdnf.cn/news/3801.html

相关文章:

  • LeetCode 热题 100 189. 轮转数组
  • 多语言笔记系列:Polyglot Notebooks 混合使用多语言并共享变量
  • 第三节:OpenCV 基础入门-安装与配置 OpenCV (Python/C++ 环境)
  • LeetCode 1128.等价多米诺骨牌对的数量:计数
  • 大连理工大学选修课——图形学:第五章 二维变换及二维观察
  • 多语言笔记系列:Polyglot Notebooks 多种使用方式
  • [2025]MySQL的事务机制是什么样的?redolog,undolog、binog三种日志的区别?二阶段提交是什么?ACID怎么保证的?主从复制的过程?
  • JVM happens-before 原则有哪些?
  • 利用KMP找出模式串在目标串中所有匹配位置的起始下标
  • 【25软考网工】第五章(4)ARP和RARP
  • 【Touching China】2007-2011
  • Go语言--语法基础4--基本数据类型--类型转换
  • MPI,Pthreads和OpenMP等并行实验环境配置
  • 【第三十四周】多模态大模型调研
  • Uni-app 组件使用
  • 什么是Linux中的systemd?
  • leetcode 59. 螺旋矩阵 II
  • 小土堆pytorch--tensorboard的使用
  • 【c++深入系列】:万字详解vector(附模拟实现的vector源码)
  • Spring MVC的工作流程, DispatcherServlet 的工作流程
  • 25.1linux中外置RTC芯片的PCF8563实验(知识)_csdn
  • 嵌入式GPIO 实验(流水灯程序,八段数码管显示程序)
  • Kubernetes 安装 kubectl
  • Qt实现 hello world + 内存泄漏(5)
  • C++学习:六个月从基础到就业——C++11/14:lambda表达式
  • MATLAB实现二氧化硅和硅光纤的单模光波特性与仿真
  • 打印Excel表格时单元格文字内容被下一行遮盖的解决方法
  • CPU 的指令集存放在什么地方?
  • 深度解析ZFNet:微调优化与可视化创新
  • 【现代深度学习技术】现代循环神经网络06:编码器-解码器架构