MybatisPlus物理删除、逻辑删除

MybatisPlus物理删除、逻辑删除

  • 物理删除:数据在物理层面删除了,文件中没有这条数据了
  • 逻辑删除:修改了标记,文件中还是存在的

基于这几篇博客修改

[整合MybatisPlus测试]

[MybatisPlus自动填充时间]

[MybatisPlus乐观锁]

物理删除

package com.xiang;

import com.baomidou.mybatisplus.annotation.TableField;
import com.xiang.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/23 1:44
 */
@SpringBootTest
public class PhysicalDeletion {
    @Autowired
    UserMapper userMapper;

    /**
     * 实现物理删除
     */

    @Test
    //通过id删除
    void deleteById() {
        int deleteById = userMapper.deleteById(1451572730934198273l);
        if (deleteById > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

    }

    @Test
        //批量删除
    void deleteBatchIds() {
        int batchIds = userMapper.deleteBatchIds(Arrays.asList(603, 604));
        if (batchIds > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    @Test
        //条件删除 条件与删除
    void deleteByMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", "向向");
        map.put("age", 18);
        int byMap = userMapper.deleteByMap(map);
        if (byMap > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }
}

通过id删除

JDBC Connection [HikariProxyConnection@643552582 wrapping com.mysql.cj.jdbc.ConnectionImpl@1937eaff] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1451572730934198273(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c2584d3]
删除成功

批量删除

JDBC Connection [HikariProxyConnection@2038185019 wrapping com.mysql.cj.jdbc.ConnectionImpl@4012d5bc] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? )
==> Parameters: 603(Integer), 604(Integer)
<==    Updates: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1caa9eb6]
删除成功

条件删除

JDBC Connection [HikariProxyConnection@1464031233 wrapping com.mysql.cj.jdbc.ConnectionImpl@2da66a44] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE age = ? AND username = ?
==> Parameters: 18(Integer), 向向(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e869098]
删除成功

删除前

删除后

逻辑删除

数据库中添加deleted字段

千万不要用delete关键字、不然你懂的...

user类中添加deleted属性

添加@TableLogic注解

package com.xiang.pojo;

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

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/21 2:50
 */

/**
 * `id`       int NOT NULL AUTO_INCREMENT,
 * `username` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
 * `sex`      varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
 * `age`      int NULL DEFAULT NULL,
 * `birthday` date NULL DEFAULT NULL,
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    //    @TableId(type = IdType.ID_WORKER) //数字类型的id使用,当类型为Long时默认使用,生成19位的Id值,
//    @TableId(type = IdType.ID_WORKER_STR)//字符串类型的id使用,需要写明注解,生成19位的Id值
    //同时需要设置数据库的字段id的类型为Bigint,因为int的长度只有11位

    private Long id;
    private String username;
    private String sex;
    private int age;
    private Date birthday;

    /***
     * 使用mybatisPlus自动填充时间
     *
     *     第一:添加注解
     *     第二:实现MetaObjectHandler接口
     *     第三:重写inserFill和updateFill方法
     *     第四:调用setFieldValByName方法
     */

    @TableField(fill = FieldFill.INSERT)//进行添加操作时有值
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)////进行添加和修改操作时有值
    private Date updateTime;

    /**
     * 添加version属性,注意添加@Version注解
     * 同时使用TableField设置初始值为1
     */
    @TableField(fill = FieldFill.INSERT)
    @Version
    private int version;

    //添加删除标记
    @TableLogic
    @TableField(fill = FieldFill.INSERT)//设置初始值
    private int deleted;
}


设置逻辑删除的初始值的方式有两种

1、使用TableField注解

2、在数据库给字段设置默认初始值

注:使用TableField注解和在数据库设置初始值只能选择一种,不能同时存在

测试类

package com.xiang;

import com.xiang.mapper.UserMapper;
import com.xiang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/23 19:22
 */
@SpringBootTest
public class LogicalDeletion {
    @Autowired
    UserMapper userMapper;

    /**
     * 实现逻辑删除
     */

    @Test
    void insert() {
        User user = new User();
        user.setUsername("小不点儿");
        user.setAge(19);
        int insert = userMapper.insert(user);
        if (insert > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        List<User> list = userMapper.selectList(null);
        for (User listUser : list) {
            System.out.println(listUser);
        }
    }

    @Test
        //根据id删除
    void deleteById() {
        int deleteById = userMapper.deleteById(1451895657063972866l);
        if (deleteById > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        List<User> list = userMapper.selectList(null);
        for (User user : list) {
            System.out.println(user);
        }
    }
}

删除前

删除后

JDBC Connection [HikariProxyConnection@423095039 wrapping com.mysql.cj.jdbc.ConnectionImpl@7e0bc8a3] will not be managed by Spring
==>  Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 1451895657063972866(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6fa0450e]
删除成功
JDBC Connection [HikariProxyConnection@172711139 wrapping com.mysql.cj.jdbc.ConnectionImpl@5793b87] will not be managed by Spring
==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0
==> Parameters: 
<==    Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
<==        Row: 1, xiang, 男, 18, 2021-10-03, null, null, null, 0
<==        Row: 559, 小向, 男, 18, 2021-10-04, null, null, null, 0
<==        Row: 602, admin, 女, 18, 2021-10-20, null, null, null, 0
<==        Row: 605, 小二, 女, 18, null, null, null, null, 0
<==        Row: 607, 小四, 女, 21, null, null, null, null, 0
<==        Row: 609, 小五, 女, 0, null, null, null, null, 0
<==        Row: 1451097869404868609, 向某, 男, 0, null, null, null, null, 0
<==        Row: 1451098975287668738, 周某, 男, 0, null, null, null, null, 0
<==      Total: 8
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@178f268a]
User(id=1, username=xiang, sex=男, age=18, birthday=Sun Oct 03 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=559, username=小向, sex=男, age=18, birthday=Mon Oct 04 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=602, username=admin, sex=女, age=18, birthday=Wed Oct 20 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=605, username=小二, sex=女, age=18, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=607, username=小四, sex=女, age=21, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=609, username=小五, sex=女, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=1451097869404868609, username=向某, sex=男, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=1451098975287668738, username=周某, sex=男, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)

posted @ 2021-10-23 21:09  阿向向  阅读(1089)  评论(0编辑  收藏  举报