Java入门9:使用MyBatis-Plus删除数据

125 阅读1分钟

该文章是Java入门系列的第九章:使用MyBatis-Plus删除数据

删除数据

打开 Controller.java 文件,我们编写删除数据的接口,添加的代码如下:

其中 student 是我们想要删除的学生的 Json 字符串,使用 gson.fromJson() 将 Json 字符串转成 Java 对象,student1就是我们要删除的对象,然后使用 studentMapper.deleteById() 删除student1,最后返回“删除成功”提示。

@GetMapping("/delete")
public String deleteStudent(){
    String student = "{"id":13,"number":"113","name":"小黄","age":14,"chi":78,"math":99,"eng":93}";
    Student student1 = gson.fromJson(student, Student.class);
    String name = student1.getName();
    studentMapper.deleteById(student1);
    return name + "删除成功!";
}

代码写好后我们保存,重新运行,先在页面上查询一下,访问http://localhost:8080/select 查询,结果如下:

image.png

可以看到有13条数据,然后我们访问http://localhost:8080/delete 删除小黄

image.png

删除成功后再访问http://localhost:8080/select ,可以看到小黄已经被删掉了

image.png

写在最后

以上就是使用MyBatis-Plus删除数据的全部说明