欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot编写批量删除

程序员文章站 2022-12-08 19:47:47
springboot编写逻辑删除1.创建数据库create table student( id int auto_increment primary key, name varchar(12) null, classes varchar(12) null, score int null, isdelete int default 1 null);2.编写studentmapper.xm...

springboot编写批量删除

1.创建数据库

create table student
(
    id       int auto_increment
        primary key,
    name     varchar(12)   null,
    classes  varchar(12)   null,
    score    int           null,
    isdelete int default 1 null
); 

2.编写studentmapper.xml文件

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.springboot.centersystem.dao.StudentMapper"> <resultMap id="BaseResultMap" type="com.springboot.centersystem.entity.Student" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="classes" property="classes" jdbcType="VARCHAR" /> <result column="score" property="score" jdbcType="INTEGER" /> <result column="isdelete" property="isdelete" jdbcType="INTEGER"/> </resultMap> <delete id="deleteid" parameterType="com.springboot.centersystem.entity.Student"> delete from student
            where id=#{id} </delete> </mapper> 

3.编写实体类

import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor public class Student { private Integer id; private String name; // 班级 private String classes; private Integer score; } 

4.编写studentmapper

import com.springboot.centersystem.entity.Student; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface StudentMapper { int deleteid(int id); } 

5.编写student.service

import com.springboot.centersystem.entity.Student; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.util.List; public interface StudentService { int deleteid(int id); } 

6.编写service实现类

@Service public class StudentServiceImpl implements StudentService { @Resource StudentMapper studentMapper; public int deleteid(int id){ return studentMapper.deleteid(id); } } 

7.编写controller

@RestController @RequestMapping("/poi") public class StudentController { @Autowired private StudentService studentService; @DeleteMapping("deleteid") @ResponseBody public String deleteid(@RequestBody int arr[]) { for (int a : arr) { studentService.deleteid(a); } //[5,6] return "删除成功"; } } 

8.测试

首先看到数据库两条数据

springboot编写批量删除

接着打开postman进行测试

springboot编写批量删除

查看数据库数据已经删除了

springboot编写批量删除

本文地址:https://blog.csdn.net/weixin_44389577/article/details/108037589

相关标签: java springboot