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

相关推荐
郭小李13 小时前
mybatis 动态 sql
java·sql·mybatis
知守观14 小时前
技术考古:iBATIS 2.3.4 时代的持久层,是如何用 Map 玩转 307 个存储过程的?
java·后端·mybatis
captain37614 小时前
MyBatis Plus(1)
java·服务器·mybatis
代码不停20 小时前
MyBatis进阶
spring boot·mybatis
captain3761 天前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
captain3761 天前
MyBatis(进阶1)
mybatis
郭小李2 天前
mybatis的 Mapper.xml语法
xml·java·mybatis
m0_639906942 天前
MyBatis 实现分页查询|逻辑分页、物理分页、PageHelper 原理
mybatis
captain3762 天前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
正儿八经的少年4 天前
MyBatis 一对一、一对多 SQL
数据库·sql·mybatis