mybatis plus逻辑删除

一 物理删除

1.1 application.yml文件

html 复制代码
server:
  port: 8087
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/business_schema?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
  redis:
    host: localhost
    port: 6379
    database: 0
    lettuce:
      pool:
        max-active: 100  # 最大连接数
        max-idle: 50     # 最大空闲连接数
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

1.2 实体类

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("book")
public class Book {
    private Integer id;
    private String bookName;
    private String bookType;
    private Integer isDeleted;
}

1.3 接口

java 复制代码
@RestController
@RequestMapping("/mango")
public class BookController {

    @Autowired
    private IBookService bookService;

    @GetMapping("/book/list")
    public List<Book> queryBookList() {
        return bookService.queryBookList();
    }

    @GetMapping("/removeById")
    private void removeById(@RequestParam Long id) {
        bookService.removeById(id);
    }

}

1.4 执行SQL

html 复制代码
==>  Preparing: DELETE FROM book WHERE id=?
==> Parameters: 14(Long)
<==    Updates: 1

这样执行物理删除,会把数据真正的删除掉

二 逻辑删除(注解)

2.1 数据表

数据表设置一个逻辑删除is_deleted字段,并设置默认值0

2.2 application.yml文件

html 复制代码
server:
  port: 8087
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/business_schema?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
  redis:
    host: localhost
    port: 6379
    database: 0
    lettuce:
      pool:
        max-active: 100  # 最大连接数
        max-idle: 50     # 最大空闲连接数
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2.3 实体类

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("book")
public class Book {
    private Integer id;
    private String bookName;
    private String bookType;
    @TableLogic
    private Integer isDeleted;
}

实体类的isDeleted字段添加com.baomidou.mybatisplus.annotation.TableLogic注解

2.4 接口

java 复制代码
@RestController
@RequestMapping("/mango")
public class BookController {

    @Autowired
    private IBookService bookService;

    @GetMapping("/book/list")
    public List<Book> queryBookList() {
        return bookService.queryBookList();
    }

    @GetMapping("/removeById")
    private void removeById(@RequestParam Long id) {
        bookService.removeById(id);
    }

}

2.5 执行SQL

2.5.1 执行删除接口

html 复制代码
==>  Preparing: UPDATE book SET is_deleted=1 WHERE id=? AND is_deleted=0
==> Parameters: 14(Long)
<==    Updates: 1

执行的是update语句,修改的是is_deleted字段

2.5.2 执行查询接口

html 复制代码
==>  Preparing: SELECT id,book_name,book_type,is_deleted FROM book WHERE is_deleted=0
==> Parameters: 
<==    Columns: id, book_name, book_type, is_deleted
<==        Row: 14, bbc, zn, 0
<==      Total: 1

执行查询SQL时,会在where条件后面默认添加is_deleted=0这个条件

三 逻辑删除(配置文件)

3.1 数据表

数据表设置一个逻辑删除is_deleted字段,并设置默认值0

3.2 application.yml文件

html 复制代码
server:
  port: 8087
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/business_schema?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
  redis:
    host: localhost
    port: 6379
    database: 0
    lettuce:
      pool:
        max-active: 100  # 最大连接数
        max-idle: 50     # 最大空闲连接数
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      logic-delete-field: isDeleted # 全局逻辑删除字段名
      logic-delete-value: 1       # 逻辑已删除值
      logic-not-delete-value: 0   # 逻辑未删除值

配置文件中添加mybatis-plus.global-config.db-config相关配置,指定全局逻辑删除字段名,逻辑已删除值和逻辑未删除值

3.3 实体类

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("book")
public class Book {
    private Integer id;
    private String bookName;
    private String bookType;
    private Integer isDeleted;
}

3.4 接口

java 复制代码
@RestController
@RequestMapping("/mango")
public class BookController {

    @Autowired
    private IBookService bookService;

    @GetMapping("/book/list")
    public List<Book> queryBookList() {
        return bookService.queryBookList();
    }

    @GetMapping("/removeById")
    private void removeById(@RequestParam Long id) {
        bookService.removeById(id);
    }

}

3.5 执行SQL

3.5.1 执行删除接口

html 复制代码
==>  Preparing: UPDATE book SET is_deleted=1 WHERE id=? AND is_deleted=0
==> Parameters: 14(Long)
<==    Updates: 1

执行的是update语句,修改的是is_deleted字段

3.5.2 执行查询接口

html 复制代码
==>  Preparing: SELECT id,book_name,book_type,is_deleted FROM book WHERE is_deleted=0
==> Parameters: 
<==    Columns: id, book_name, book_type, is_deleted
<==        Row: 14, bbc, zn, 0
<==      Total: 1

执行查询SQL时,会在where条件后面默认添加is_deleted=0这个条件

相关推荐
天若有情6735 小时前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis
不能只会打代码6 小时前
Day 011 — Spring 全家桶深度拆解
spring boot·mybatis·spring aop·spring mvc·spring ioc
憧憬成为web高手20 小时前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
Java面试题总结2 天前
mybatis插件
java·tomcat·mybatis
Devin~Y3 天前
互联网大厂Java面试实战:Spring Boot、MyBatis、Redis、Kafka、JWT、Spring Cloud 与 AI 场景追问
java·spring boot·redis·spring cloud·kafka·mybatis·spring security
Ethan01073 天前
mybatis插件
mybatis
扎Zn了老Fe4 天前
MyBatis-Plus必知必会:告别低效CRUD,高效开发持久层
后端·mybatis
醉城夜风~4 天前
MyBatis 基础CRUD全套实战学习笔记
笔记·学习·mybatis
万亿少女的梦1684 天前
基于微信小程序、Spring Boot与Vue3的智慧养老管理系统设计与实现
spring boot·redis·微信小程序·mybatis·vue3
4154115 天前
MyBatis-Plus + PostGIS 实战(四):GeoJSON 序列化与前端地图对接,全局 WKT + 局部 GeoJSON,两种格式优雅共存
java·mybatis·postgis·geojson