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

SpringBoot 集成mybatis-plus

目录

前言

简介

前提

运用mybatis-plus(使用20241.1版本的idea)

1 自动创建springboot项目

1.1 点击新建,SpringBoot

1.2 添加依赖项,点击创建

2添加 MyBatis-Plus Starter 依赖

2.1 打开mybatis-plus官网,点击快速开始,选择springboot3 复制代码

2.2 粘贴到pom文件中的 ...............,点击右上角图案,等待加载成功

3  添加数据库连接配置(yml配置文件)

4 数据库图形化界面工具中建表(已经建好表,可以忽略)

5   预防表的全部删除或全部更新

6 创建包 entity,mapper,controller,创建类或接口

6.1  创建包 entity,在包中创建User类

6.2 创建包mapper,在包中创建UserMapper接口(代码在官网中也有)

6.3 创建包controller,在包中创建TestController类

7 点击运行

小结


 

前言

本篇博客,基于mybatis-plus官网,简单了解和实际运用mybatis-plus

简介

MyBatis-Plus 是 一个 MyBatis的增强工具,为简化springboot开发、提高效率而生。

mybatis-plus 官网:简介 | MyBatis-Plus

前提

2f4d42fcb4a7443bb7587e40308da584.png

运用mybatis-plus(使用20241.1版本的idea)

1 自动创建springboot项目

1.1 点击新建,SpringBoot

8e7a9885ac5b4fafb02ebbf2931bfd53.png

1.2 添加依赖项,点击创建

依赖项

1 Lombok

2 web--spring Web

3 SQL---MySQL Driver

 

6ffdee312aac4f85b1ab41a9fcc8ce8a.png

创建成功

eda171b9a9614b24af7d2862a3f71965.png

2添加 MyBatis-Plus Starter 依赖

2.1 打开mybatis-plus官网,点击快速开始,选择springboot3 复制代码

ecd7b182712a48268ee0867313542a7d.png

2.2 粘贴到pom文件中的<dependencies> ...............</dependencies>,点击右上角图案,等待加载成功

7070796d1acf4c4f83864b4cb6aa4ac0.png

3  添加数据库连接配置(yml配置文件)

数据库的配置信息,我没有使用官网给我们的代码

原因是 官网使用是H2 数据库的相关配置,暂时不知道怎么运用到idea中

8e8540b98557458aa0e4696375ef57a9.png

代码如下

spring:datasource:username: root(用户名)password: 123456(密码)url: jdbc:mysql://localhost:3306/ss(数据库名)?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driver

我打括号的,做修改,其他不改动。

68121f4da4ad4d25848d2e3cb64d3e36.png

4 数据库图形化界面工具中建表(已经建好表,可以忽略)

注意:如果你已经创建好表了,就不需要在官网中把sql脚本添加到图形化界面工具中,创建新的表。

如何导入sql脚本文件?

具体步骤如下

1 在桌面新建一个文本文档,后缀为.sql

1a261c14edbc40c6b73bd4d8fe0c3e00.png

2 双击打开这个文件

3 点击运行,选择合适的数据库(点击之后,会自动加载到你的目标库中)

c2947644ec914fd19bae11dcdc0f6e83.png

注意:如果你运行之后,又打算放在其他库中,点击右上角的小三角形图案,选择合适的数据库

5570786da10640f694055f26c6502f63.png

5   预防表的全部删除或全部更新

BlockAttackInnerInterceptor 是 MyBatis-Plus 框架提供的一个安全插件,专门用于防止恶意的全表更新和删除操作。该插件通过拦截 update 和 delete 语句,确保这些操作不会无意中影响到整个数据表,从而保护数据的完整性和安全性。

代码在官网---防全表更新与删除插件

0f8680d184184307a13ea4e7fcdda93d.png

f8d9bff889664549be4e61ba4b96da47.png

6 创建包 entity,mapper,controller,创建类或接口

6.1  创建包 entity,在包中创建User类

如果是使用,官网中的数据库的代码,就使用官网提供给我们的实体类User

代码如下

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

fd4f210023724a3abf2079f6d5455ab0.png

6.2 创建包mapper,在包中创建UserMapper接口(代码在官网中也有)

代码如下

public interface UserMapper extends BaseMapper<User> {

}

fe9b013c6e72409ab4c5a6223d3f2307.png

6.3 创建包controller,在包中创建TestController类

代码如下

package com.it.example04.controller;import com.it.example04.entity.User;
import com.it.example04.mapper.UserMapper;
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;@RestController
public class TestController {@Autowired
//该注解,创建userMapper对象private UserMapper userMapper;@RequestMapping("/list")public List<User>list(){return userMapper.selectList(null);
// 返回结果为表的字段和数据。访问浏览器中可以,看到表的字段和数据}}

注意:在你创建userMapper对象时,可能会报红!!

原因:你没有往spring容器中注入bean(对象)

解决方法:在UserMapper接口中添加@Mapper注解

32cceacf843b4e2ba8cb0f32cda4a3b6.png

7 点击运行

e02f5a0d67c34973a6c9cc8417c252ee.png

在浏览器中输入:http://localhost:8080/list,会出现你建好的表的信息

94f318a5835f49028bebb79a2f4f9db2.png

小结

本篇博客,主要讲了如何在springboot项目中运用mybatis-plus 方便我们以后实际开发。

 


http://www.mrgr.cn/news/15750.html

相关文章:

  • 网络安全领域含金量最高的5大赛事,每个网安人的梦!
  • 华为云征文|初识Flexus云服务X实例和参数配置,finalShell远程连接,安装MySQL并配置和远程访问
  • 制造企业如何启用BI工具,并构建自助式BI业务模式?
  • sickos 靶机渗透(wolf cms 渗透,squid 代理)
  • 【CF】1821E-Rearrange Brackets 题解
  • C++基础
  • 【Spring Boot 3】【Web】同时启用 HTTP 和 HTTPS
  • 51单片机-第九节-AT24C02存储器(I2C总线)
  • LeetCode_sql_day18(1841.联赛信息统计)
  • AI Agent从实操体验到代码理解
  • duckDB源码GDB调试
  • 安装飞桨paddle2.6.1+cuda11.7+paddleRS-develop开发版
  • Linux教程七:文件目录类命令ls、cd(图文详解)
  • PDF文本指令解析与文本水印去除
  • 西方法律思想史-整理
  • Vulnhub靶场 | DC系列 - DC-3
  • 红黑树刨析(删除部分)
  • 给ui添加 更换material 脚本
  • 微服务即时通讯系统环境搭建(客户端)
  • 6 款 AI 视频全自动国产剪辑软件推荐