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

相关推荐
wuqingshun3141591 天前
说说mybatis的缓存机制
java·缓存·mybatis
java1234_小锋2 天前
说说MyBatis的工作原理吗?
java·mybatis
是宇写的啊2 天前
MyBaties
java·开发语言·mybatis
那个失眠的夜2 天前
RESTful 语法规范 核心注解详解
java·spring·mvc·mybatis
他是龙5512 天前
66:Java安全&SPEL表达式&SSTI模版注入&XXE&JDBC&MyBatis注入
java·安全·mybatis
MegaDataFlowers3 天前
MybatisPlus入门案例
mybatis
cheems95273 天前
[Mybatis] #{ } 与 ${ } 的底层博弈与工程实践
mybatis
2601_949817723 天前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
LaLaLa_OvO4 天前
mybatis 引用静态常量
java·mybatis