MyBatis-Plus 之逻辑删除

MyBatis-Plus 之逻辑删除MyBatis-Plus之逻辑删除实现概念逻辑删除:文件没有被真正的删除,只不过是文件名的第一个字节被改成操作系统无法识别的字符,通常这种删除操作是可逆的,就是说用适当的工具或软件可以把删除的文件恢复出来。物理删除:指文件存储所用到的存储区域被真正的擦除或清零,这样删除的文件是不可以恢复的,物理删除是计算机处理数据时的一个概念。逻辑删除就是对要被删除的数据打上一个删除标记,在逻辑上,数据是被删除了,但数据本身依然存在!而物理删除则是把数据从介质上彻底删除掉。正文首先创建一个数据库表,如下图

大家好,又见面了,我是你们的朋友全栈君。

推荐:MyBatis Plus汇总

MyBatis-Plus 之逻辑删除

概念

  • 逻辑删除:文件没有被真正的删除,通常这种删除操作是可逆的,就是说用适当的工具或软件可以把删除的文件恢复出来。
  • 物理删除:指文件存储所用到的存储区域被真正的擦除或清零,这样删除的文件是不可以恢复的,物理删除是计算机处理数据时的一个概念。

逻辑删除就是对要被删除的数据打上一个删除标记,在逻辑上,数据是被删除了,但数据本身依然存在!而物理删除则是把数据从介质上彻底删除掉。

正文

首先创建一个数据库表,如下图所示:
在这里插入图片描述

然后创建一个Spring Boot项目。

pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.kaven</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml如下:

spring:
  application:
    name: mybatis-plus
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: ITkaven@123
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false

server:
  port: 8085

logging:
  level:
    root: warn
    com.kaven.mybatisplus.dao: trace
  pattern:
    console: '%p%m%n'

mybatis-plus:
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

在这里插入图片描述

实体类User:

package com.kaven.mybatisplus.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

@TableName("user")
@Data
public class User{ 
   

    @TableId
    private String id;

    @TableField(value = "username")
    private String username;

    @TableField(value = "password")
    private String password;

    @TableField(value = "age")
    private Integer age;
    
// @TableLogic(delval = "1" , value = "0") 每个实体类中可以定义逻辑删除配置
    @TableLogic
    @TableField(value = "deleted")
    private Integer deleted;

    /** * 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入数据库中 * 使用 transient 、 static 修饰属性也不会插入数据库中 */
    @TableField(exist = false)
    private String phone;
}

Mapper接口UserMapper:

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kaven.mybatisplus.entity.User;
import org.springframework.stereotype.Component;


@Component
public interface UserMapper extends BaseMapper<User> { 
   }

启动类:

package com.kaven.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.kaven.mybatisplus.dao")
public class AppRun { 
   
    public static void main(String[] args) { 
   
        SpringApplication.run(AppRun.class , args);
    }
}

@MapperScan(basePackages = "com.kaven.mybatisplus.dao")这个一定要加上。

我们先在数据库中添加几行数据,方便演示。
在这里插入图片描述

来演示一下逻辑删除。

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicalDeleteTest { 
   

    @Autowired
    private UserMapper userMapper;

    @Test
    public void deleteById(){ 
   
        int rows = userMapper.deleteById("10");
        System.out.println("影响行数: "+rows);
    }
}

结果如下:
在这里插入图片描述
逻辑删除不是真正的删除,它只是更新了逻辑删除标识,从上面的sql语句也能看出来,并且只能删除逻辑存在的数据,因为有AND deleted=0这个条件,很显然,上面的结果是正确的。

再来演示逻辑删除了的数据通过selectList()是否还可以查询出来。

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicalDeleteTest { 
   

    @Autowired
    private UserMapper userMapper;
    
    @Test
    public void selectList(){ 
   
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }
}

结果如下:
在这里插入图片描述
很显然是查询不出来的,因为有限制条件WHERE deleted=0,但这只是MyBatis-Plus帮我们加上去的,如果你自定义sql语句来查询,还是可以查询出来,因为你可以设置对deleted字段没有限制。

再来演示逻辑删除了的数据通过updateById()是否还可以更新。

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicalDeleteTest { 
   

    @Autowired
    private UserMapper userMapper;

    @Test
    public void updateById(){ 
   
        User user = new User();
        user.setId("10");
        user.setUsername("kaven");
        user.setAge(22);

        int rows = userMapper.updateById(user);
        System.out.println("影响行数: "+rows);
    }
}

结果如下:
在这里插入图片描述
可以看到,这个更新方法即使没有更新到数据,也不会报错,也可以知道,被逻辑删除了的数据是不能更新的(通过MyBatis-Plus),自己定义sql语句来实现更新就另说。

从上面这些演示,可以知道逻辑删除了的数据通过MyBatis-Plus是不能被查询、更新以及删除,那通过我们自定义的sql语句呢?我们来测试一下。

修改UserMapper:

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.kaven.mybatisplus.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface UserMapper extends BaseMapper<User> { 
   

    @Select("select * from user ${ew.customSqlSegment}")
    List<User> mySelectList(@Param(Constants.WRAPPER) Wrapper<User> userWrapper);
}

通过注解@Select("select * from user ${ew.customSqlSegment}")来自定义sql语句,在之前的博客有介绍过:MyBatis-Plus 之自定义sql

测试代码:

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicalDeleteTest { 
   

    @Autowired
    private UserMapper userMapper;
    
      @Test
    public void mySelectList(){ 
   
        List<User> userList = userMapper.mySelectList(Wrappers.<User>lambdaQuery().gt(User::getAge , 25));
        userList.forEach(System.out::println);
    }
}

结果如下:
在这里插入图片描述
很显然,我们自定义的sql语句,MyBatis-Plus是不会帮我们加限制条件的,逻辑删除了的数据也给我们查询出来了,所以如果要自定义sql语句来操作,就要自己来加限制条件。

比如:

package com.kaven.mybatisplus.dao;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kaven.mybatisplus.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicalDeleteTest { 
   

    @Autowired
    private UserMapper userMapper;

    @Test
    public void mySelectList(){ 
   
        List<User> userList = userMapper.mySelectList(Wrappers.<User>lambdaQuery()
                .gt(User::getAge , 25).eq(User::getDeleted , 0));
        userList.forEach(System.out::println);
    }
}

结果如下:
在这里插入图片描述
这样就没有逻辑删除了的数据被查询出来了。

逻辑删除标识就是一个普通的字段,只是我们用注解告诉了MyBatis-Plus,这是逻辑删除标识,这样MyBatis-Plus才能在我们进行CRUD时,给我们加限制条件,如果想要自定义sql语句来CRUD,就必须自己加上限制条件。

MyBatis-Plus的逻辑删除实现就介绍到这里。

写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143832.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大数据应用及其解决方案(完整版)

    大数据应用及其解决方案(完整版)目录1、大数据概述1.1.概述1.2.大数据定义1.3.大数据技术发展2、大数据应用2.1.大数据应用阐述2.2.大数据应用架构2.3.大数据行业应用2.3.1.医疗行业2.3.2.能源行业2.3.3.通信行业2.3.4.零售业3、大数据解决方案3.1.大数据技术组成3.1.1.分析技术3.1.2.存储数据库…

    2022年6月2日
    52
  • 约瑟夫环问题递归解法的一点理解

    约瑟夫环问题递归解法的一点理解约瑟夫环递归解法代码的一点理解。约瑟夫生者死者游戏约瑟夫游戏的大意:30个游客同乘一条船,因为严重超载,加上风浪大作,危险万分。因此船长告诉乘客,只有将全船一半的旅客投入海中,其余人才能幸免于难。无奈,大家只得同意这种办法,并议定30个人围成一圈,由第一个人数起,依次报数,数到第9人,便把他投入

    2022年6月4日
    28
  • docker查看redis版本[通俗易懂]

    docker查看redis版本[通俗易懂]dockerexec-itredisredis-server-v

    2022年6月2日
    58
  • 关于 ioctl 的 FIONREAD 参数[通俗易懂]

    关于 ioctl 的 FIONREAD 参数[通俗易懂]ioctl是用来设置硬件控制寄存器,或者读取硬件状态寄存器的数值之类的。而read,write是把数据丢入缓冲区,硬件的驱动从缓冲区读取数据一个个发送或者把接收的数据送入缓冲区。 ioctl(keyFd,FIONREAD,&b)得到缓冲区里有多少字节要被读取,然后将字节数放入b里面。接下来就可以用read了。read(keyFd,&b,sizeof(b))清

    2022年7月23日
    12
  • error at hooking api ntprotect_read,match and write

    error at hooking api ntprotect_read,match and write 编译环境:delphi2010+windows7u,用途读取其他程序中readprocessmemory和writeprocessmemory的参数,但不知读取偏移即a+($b),b是怎么读的  一、用hook全局钩子线程钩子:已实现使用INLINEhookapi,CriticalSection临界区,dll分为动态loadlibry和静态加载问题1:对多线程目标

    2025年11月5日
    2
  • Visual Studio 2015 移动跨平台开发初体验

    Visual Studio 2015 移动跨平台开发初体验

    2021年11月24日
    57

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号