这是vo类 @Data @ApiModel(value = "飞行任务管理-后端列表页vo") public class BisTaskVO extends PageReqVO { @ApiModelProperty(value = "逻辑删除 1是 0否") private Integer isDel; } 帮我用mybatis-plus写个逻辑删除
时间: 2023-11-22 15:54:46 浏览: 74
好的,可以参考以下代码:
首先,在实体类BisTask中添加@TableLogic注解:
```
@Data
@ApiModel(value = "飞行任务管理-后端列表页vo")
public class BisTask extends Model<BisTask> {
@ApiModelProperty(value = "逻辑删除 1是 0否")
@TableLogic
private Integer isDel;
// 其他属性和方法省略
}
```
然后,在Mapper接口中继承BaseMapper,并使用@Mapper注解标注该接口:
```
@Mapper
public interface BisTaskMapper extends BaseMapper<BisTask> {
}
```
之后你就可以使用mybatis-plus提供的逻辑删除方法进行删除操作了,例如:
```
@Autowired
private BisTaskMapper bisTaskMapper;
// 删除id为1的记录,并设置isDel=1
bisTaskMapper.deleteByIdWithFill(new BisTask().setId(1).setIsDel(1));
```
这样就能够实现逻辑删除了。注意,逻辑删除只是将isDel字段的值设为1,而不是真的从数据库中删除记录。在查询时,mybatis-plus会自动过滤掉isDel=1的记录。
相关问题
这是VO类, @Data @ApiModel(value = "飞行任务管理-后端列表页vo") public class BisTaskVO extends PageReqVO implements Serializable { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "逻辑删除 1是 0否") private Integer isDel; } 写个逻辑删除,用mybatis-plus,写在service,不要写mapper和sql
好的,这里是一个可能的实现方案:
首先在 BisTask 实体类中添加 `@TableLogic` 注解标识逻辑删除字段:
```java
@Data
@TableName("bis_task")
public class BisTask implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableLogic // 标识逻辑删除字段
private Integer isDel;
}
```
然后在 BisTaskService 中实现逻辑删除的方法:
```java
@Service
public class BisTaskServiceImpl extends ServiceImpl<BisTaskMapper, BisTask> implements BisTaskService {
@Override
public boolean removeById(Integer id) {
BisTask task = new BisTask();
task.setId(id);
task.setIsDel(1); // 设置逻辑删除标志
return updateById(task);
}
}
```
这里使用了 Mybatis-Plus 提供的 `updateById` 方法,它会自动根据实体对象中的属性进行更新,只更新有值的字段。这里只设置了 `id` 和 `isDel` 两个属性,其他属性会被忽略。
这样,我们就完成了逻辑删除的实现,调用 `removeById` 方法即可将指定 ID 的记录标记为已删除。需要注意的是,查询时应该过滤掉已删除的记录,可以在查询条件中加上 `is_del = 0` 的限制。
@Data @ApiModel(value = "飞行任务管理-后端列表页vo") public class BisTaskVO extends PageReqVO { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "逻辑删除 1是 0否") private Integer isDel; } 这是个VO类,用mybatis-plus写个批量逻辑删除
可以使用 MyBatis-Plus 提供的 `IService` 接口中的 `removeByIds` 方法来实现批量逻辑删除。具体实现步骤如下:
1. 在 `BisTaskMapper` 接口中新增一个方法 `deleteBatchLogicByIds`,用于批量逻辑删除任务。方法定义如下:
```java
public interface BisTaskMapper extends BaseMapper<BisTask> {
int deleteBatchLogicByIds(@Param("ids") List<Integer> ids);
}
```
2. 在 `BisTaskServiceImpl` 实现类中,注入 `BisTaskMapper` 接口,并实现 `deleteBatchLogicByIds` 方法。方法定义如下:
```java
@Service
public class BisTaskServiceImpl extends ServiceImpl<BisTaskMapper, BisTask> implements BisTaskService {
@Autowired
private BisTaskMapper bisTaskMapper;
@Override
public boolean deleteBatchLogicByIds(List<Integer> ids) {
return bisTaskMapper.deleteBatchLogicByIds(ids) > 0;
}
}
```
3. 在控制器中调用 `deleteBatchLogicByIds` 方法实现批量逻辑删除。示例代码如下:
```java
@RestController
@RequestMapping("/task")
public class BisTaskController {
@Autowired
private BisTaskService bisTaskService;
@PostMapping("/deleteBatch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Result.fail("参数错误");
}
// 批量逻辑删除任务
boolean success = bisTaskService.deleteBatchLogicByIds(ids);
if (success) {
return Result.success();
}
return Result.fail("删除失败");
}
}
```
注意事项:
- `BisTask` 实体类中需要有 `isDel` 属性用于记录逻辑删除状态。
- 执行批量逻辑删除时,需要传入待删除任务的 id 列表。
- 执行成功时,返回 `Result.success()`,否则返回 `Result.fail()`。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)