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这个条件

相关推荐
摇滚侠1 天前
Mybatis 入门到项目实战 搭建 MyBatis 框架 01-14
java·tomcat·mybatis
敲个大西瓜1 天前
mybatis插件原理与编写
mybatis
可乐ea1 天前
【Spring Boot + MyBatis|第7篇】JWT 登录认证与拦截器实现
java·spring boot·后端·mybatis·状态模式
摇滚侠1 天前
MyBatis 入门到项目实战 MyBatis 核心配置文件 15-19
java·tomcat·mybatis
摇滚侠1 天前
MyBatis 入门到项目实战 IDEA 配置模板 20-22
java·intellij-idea·mybatis
独泪了无痕2 天前
MyBatis魔法堂:结果集映射
后端·mybatis
就叫_这个吧2 天前
IDEA Mybatis xml文件,实现sql语句联想,自动填入补充
xml·mysql·intellij-idea·mybatis
熠熠仔2 天前
Spring Boot 与 MyBatis-Plus 空间几何数据集成指南
spring boot·后端·mybatis
范什么特西2 天前
重点:mybatis注意细节
java·mysql·mybatis
接着奏乐接着舞2 天前
springboot mp mybatis plaus
windows·spring boot·mybatis