1. 程式人生 > 其它 >MybatisPlus物理刪除、邏輯刪除

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)