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=false
    driver-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 方便我们以后实际开发。

 

  • 16
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值