最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus中的邏輯刪除功能及實(shí)例分析

 更新時(shí)間:2025年03月14日 11:38:30   作者:極客李華  
本文將詳細(xì)講解MyBatis-Plus中的邏輯刪除特性,并結(jié)合實(shí)際案例進(jìn)行演示和說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

MyBatis-Plus中的邏輯刪除功能及實(shí)例

介紹:

  • 在實(shí)際開發(fā)中,數(shù)據(jù)的刪除操作是一個(gè)常見需求。
  • MyBatis-Plus提供了方便且靈活的邏輯刪除功能,可以簡化開發(fā)人員在刪除數(shù)據(jù)時(shí)的操作。

案例背景

假設(shè)我們有一個(gè)博客系統(tǒng),包含Blog類作為博客實(shí)體。在這個(gè)系統(tǒng)中,我們希望能夠?qū)Σ┛瓦M(jìn)行軟刪除操作,即將要?jiǎng)h除的博客標(biāo)記為已刪除狀態(tài),而不是直接物理刪除。

通過邏輯刪除,我們可以保留刪除記錄,有助于追蹤數(shù)據(jù)變更歷史和實(shí)現(xiàn)審計(jì)需求。

使用邏輯刪除功能

配置數(shù)據(jù)庫和實(shí)體類

首先,在使用邏輯刪除功能之前,我們需要進(jìn)行相關(guān)的配置。

  1. 在數(shù)據(jù)庫表中,我們需要添加一個(gè)用于表示邏輯刪除狀態(tài)的字段。例如,我們可以添加一個(gè)名為deletedtinyint類型字段,默認(rèn)值為0。
  2. 在實(shí)體類(這里是Blog類)中,我們使用Lombok來減少getter和setter方法的編寫工作。

以下是一個(gè)示例:

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("blog")
public class Blog {
    private Long id;
    private String title;
    private String content;
    @TableLogic
    private Integer deleted;
}

在上述示例中,我們使用@Data注解簡化了getter和setter方法的編寫,并使用@TableName注解標(biāo)記了實(shí)體類對(duì)應(yīng)的數(shù)據(jù)庫表名。同時(shí),使用@TableLogic注解標(biāo)記了deleted字段為邏輯刪除字段。

執(zhí)行邏輯刪除操作

接下來,我們將演示如何在MyBatis-Plus中使用邏輯刪除功能。

方式一:局部配置

局部配置適用于只需要在某個(gè)Mapper接口中應(yīng)用邏輯刪除功能的情況。

首先,在Mapper接口中添加@Repository注解并繼承BaseMapper接口,例如:

import org.springframework.stereotype.Repository;

@Repository
public interface BlogMapper extends BaseMapper<Blog> {
}

然后,在Service層的實(shí)現(xiàn)類中進(jìn)行邏輯刪除的調(diào)用:

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Override
    public boolean deleteBlogById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }
}

在上述示例中,我們直接調(diào)用baseMapper的deleteById方法進(jìn)行邏輯刪除操作。MyBatis-Plus會(huì)自動(dòng)將邏輯刪除字段設(shè)置為1(表示已刪除)。

方式二:全局配置

全局配置適用于對(duì)整個(gè)項(xiàng)目中的所有Mapper接口都應(yīng)用邏輯刪除功能的情況。

首先,在application.yml或application.properties文件中配置全局屬性:

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

然后,設(shè)置全局配置項(xiàng)并在Mapper接口中使用@TableLogic注解:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加樂觀鎖插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

在上述示例中,我們創(chuàng)建了一個(gè)@Configuration類,并注冊(cè)了Mybatis-Plus提供的分頁插件(PaginationInterceptor)和樂觀鎖插件(OptimisticLockerInnerInterceptor)。這些插件可以根據(jù)具體需求進(jìn)行配置。

測(cè)試

為了驗(yàn)證邏輯刪除功能是否正常工作,我們可以編寫單元測(cè)試。

以下是一個(gè)簡單的測(cè)試實(shí)例:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BlogServiceTest {

    @Autowired
    private BlogService blogService;

    @Test
    public void testDeleteBlog() {
        Long blogId = 1L; // 假設(shè)要?jiǎng)h除ID為1的博客
        boolean result = blogService.deleteBlogById(blogId);
        System.out.println("Delete successful: " + result);
    }
}

在上述測(cè)試中,我們注入了BlogService接口,并調(diào)用deleteBlogById方法來執(zhí)行邏輯刪除操作。

通過編寫和運(yùn)行這些測(cè)試用例,我們可以驗(yàn)證使用邏輯刪除功能時(shí)的正常性。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

亚东县| 甘孜县| 潞西市| 铜川市| 中卫市| 曲阳县| 莆田市| 天峻县| 沅陵县| 交口县| 宁化县| 公安县| 科技| 长沙县| 阜新| 漳平市| 临漳县| 东海县| 南漳县| 应城市| 繁昌县| 江都市| 临汾市| 肃南| 满城县| 拜城县| 台东县| 论坛| 峨眉山市| 塔河县| 马边| 宜宾县| 辽中县| 金沙县| 洛隆县| 察雅县| 双柏县| 奉节县| 玉龙| 蒙阴县| 卢龙县|