目录
前端代码:
后端代码:
controller
service层接口
service接口的实现
mapper层接口
xml
sql
效果:(点击操作列的删除,可删除一行数据。勾选多个多选框再点击批量删除,可删除多个)
批量删除
编辑
删除
export default {
data() {
return {
tableData: [],
selectedIds: [], // 存储选中行的ID的数组
}
},
methods: {
//判断用户选择了什么
handleSelectionChange(selection) {
this.selectedIds = selection.map(item => item.id);
},
//删除用户(删除多个)
batchDeleteUser() {
this.deleteUser(this.selectedIds)
},
//删除用户(删除单个)
deleteOneUser(row) {
let selectIdArray = [];
selectIdArray.push(row.id);
this.deleteUser(selectIdArray)
},
//删除用户逻辑
deleteUser(selectIdArray) {
this.$confirm('确认删除?')
.then(() => {
//点击确定后的逻辑
this.$axios({
method: "post",
url: "http://localhost:8080/api/user/deleteUser",
data: selectIdArray
}).then((res) => {
if (res.data.code === '200') {
this.$message({
message: res.data.message,
type: "success"
});
this.selectAllUser();
} else {
this.$message({
message: res.data.message,
type: "failed"
});
}
})
})
.catch(() => {});
}
}
}
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
//删除用户
@PostMapping("/deleteUser")
public Result deleteUser(@RequestBody String[] userArray){
return userService.deleteUser(Arrays.asList(userArray));
}
}
public interface UserService {
Result deleteUser(List UserList);
}
public Result deleteUser(List userList) {
// 判断userList是否为空
if (CollectionUtils.isEmpty(userList)) {
// 如果是空,则返回删除成功信息和删除数量为0
return Result.succeed("删除成功", 0);
}
// 调用userMapper.deleteUser方法来删除指定的用户
int count = userMapper.deleteUser(userList);
// 判断删除是否成功
if (count == userList.size()) {
// 如果全部删除成功,则返回删除成功信息和删除的用户数量
return Result.succeed("删除成功", count);
} else {
// 如果只有部分删除成功,则返回部分删除成功信息和删除的用户数量
return Result.succeed("删除部分成功", count);
}
}
public interface UserMapper {
int deleteUser(List userList);
}
update user
set
status='0'
where id in
#{item}
数据库
/*
Navicat Premium Data Transfer
Source Server : database
Source Server Type : MySQL
Source Server Version : 80029
Source Host : localhost:3306
Source Schema : score_management_db
Target Server Type : MySQL
Target Server Version : 80029
File Encoding : 65001
Date: 18/02/2024 13:52:16
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userId` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '账号',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名',
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '密码',
`phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '手机号',
`grades` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '班级',
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '性别',
`age` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '年龄',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '地址',
`status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '1' COMMENT '状态',
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('zhangsan', '张三', '1234', '1778299200', '软件一班', '男', '341', '广西1', '1', 1);
INSERT INTO `user` VALUES ('zhangsi', '张四', '1234', '1778299200', '软件一班', '男399999', '3嗯嗯嗯', '广v', '1', 2);
INSERT INTO `user` VALUES ('liuliu', '刘六', '1234', '1778299200', '软件二班', '男', '34', '广西', '1', 3);
INSERT INTO `user` VALUES ('1', '陈万祥222', '1', '55566777888', '软件二班', NULL, NULL, NULL, '0', 10);
INSERT INTO `user` VALUES ('laoxiaming', '劳霞明', 'laoxiaming', '4646436', '软件二班', NULL, NULL, NULL, '1', 11);
SET FOREIGN_KEY_CHECKS = 1;