最近在做一个前后端分离的小项目,需要删除用户表的用户,但是用户再别的表做了外键,所以只能做成逻辑删除,一通百度查资料后得以实现:
1、用户实体类
package com.sie.demo.entity;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.sql.Timestamp;
//@Data:作用于类上,是以下注解的集合:@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("Users")
public class Users implements Serializable {
private static final long serialVersionUID = 30L;
//mabatis-plus:数据库的字段命名方式为使用下划线连接,对应的实体类应该是驼峰命名方式
String userName;
String password;
String name;
int status;
int sex;
@JSONField(format ="yyyy-MM-dd HH:mm:ss")
Timestamp createTime;
@JSONField(format ="yyyy-MM-dd HH:mm:ss")
Timestamp updateTime;
String email;
String note;
@TableId("user_id")
long userId;
String phoneNumber;
@TableLogic//这个标签用来标识这是个用来伪删除的字段
@TableField("IS_DELETE")
int isDelete;
public Users(String userName, String password) {
this.userName = userName;
this.password = password;
}
}
2、yml配置
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
3、service层,
@Override
/**
* 删除用户
*/
public Integer deleteUserByIds(String[] userSelected) {
Wrapper wrapper = new QueryWrapper();
List userIds = Arrays.asList(userSelected);
return userMapper.deleteBatchIds(userIds);
}
4、controller层
@DeleteMapping("delete")
public RetResult<PageUserVo> deleteUserByIds(String[] ids){
Integer isDelete = userService.deleteUserByIds(ids);
if (isDelete != 0){
return RetResponse.makeRsp(200,"删除成功");
}else {
return RetResponse.makeRsp(500,"删除失败");
}
}
6、运行后控制台的sql语句直接就是update了
完毕!?
springboot+oracle+mybatis-plus实现分页:https://blog.csdn.net/weixin_46108919/article/details/119274278?spm=1001.2014.3001.5502
|