Java入门10:使用MyBatis-Plus添加数据

195 阅读1分钟

该文章是Java入门系列的第十章:使用MyBatis-Plus添加数据

添加数据

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

其中student是我们要添加的学生数据,我们把上一章节删除的小黄再添加回来,使用studentMapper.insert()方法即可往数据库插入一条数据

@GetMapping("/insert")
public String insertStudent(){
    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.insert(student1);
    return name + "添加成功!";
}

代码编写完成后重新运行项目,我们打开http://localhost:8080/insert ,可以看到添加成功的提示

image.png

再打开http://localhost:8080/select ,已经可以查到小黄了

image.png

写在最后

以上就是使用MyBatis-Plus添加数据的全部说明,下一章节介绍使用MyBatis-Plus修改数据