SprintBoot的接口编写调试

spider   ·   发表于 2023-05-19 19:12:51   ·   技术文章

使用Navicat导入sql文件
student.sql

1、先创建一个Student类

写入实体类

  1. package org.example.pojo;
  2. import lombok.Data;
  3. @Data
  4. public class Student {
  5. private long id;
  6. private String number;
  7. private String name;
  8. private int age;
  9. private int chi;
  10. private int math;
  11. private int eng;
  12. }

在创建一个映射类

  1. package org.example.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import org.example.pojo.Student;
  4. //继承BaseMapper,也把Student传进去
  5. public interface StudentMapper extends BaseMapper<Student> {
  6. }

映射类写好之后,我们回到SprintBoot路由那边,尝试编写设置从数据里查询一些数据,返回到Web

SprintBoot路由回显数据库内容

  1. package org.example;
  2. import com.google.gson.Gson;
  3. import org.example.mapper.StudentMapper;
  4. import org.example.pojo.Student;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.security.PrivilegedAction;
  11. import java.util.List;
  12. import java.util.PrimitiveIterator;
  13. //使用@SuppressWarnings去除所有告警
  14. @SuppressWarnings("all")
  15. @RestController
  16. public class Controller {
  17. //这个@Autowired是自动接线的意思,只要加上了这个,他就会自动去找Mapper类,其次再从你的数据库配置文件中读取数据
  18. @Autowired
  19. StudentMapper studentMapper;
  20. //把students转成一个字符串,用Gson
  21. //声明一个Gson
  22. private Gson gson= new Gson();
  23. @GetMapping("/test")
  24. public String test() {
  25. //获取数据库数据
  26. List<Student> students = studentMapper.selectList(null);
  27. //通过Gson,将student转成字符串返回字符串到桌面
  28. return gson.toJson(students);
  29. }
  30. }


做了路由设置之后,我们还需要回到Application中,在@SpringBootApplication启动类下添加一个注解

Application启动类下面一行添加注解

把包的名字复制过来

只是添加这个

  1. package org.example;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.SpringBootConfiguration;
  5. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. @SpringBootConfiguration
  8. @SpringBootApplication
  9. //让MapperScan扫描我们的mapper文件下的接口,这样的话,SprintBoot就知道你的这个映射接口在mapper里了
  10. @MapperScan("org.example.mapper")
  11. //@EnableAutoConfiguration
  12. public class Application {
  13. public static void main(String[] args) {
  14. SpringApplication.run(Application.class,args);
  15. }
  16. }

启动访问Web情况

http://localhost:8080/test

以上,就是Mybatis-plus的查询

2、使用Mybatis-plus插入、删除、修改

删除接口

我们提前把一组数据复制过来先

  1. {"id":13,"number":"113","name":"小黄","age":14,"chi":78,"math":99,"eng":93}


然后我们回到Controller再写一个接口
{“id”:13,”number”:”113”,”name”:”小黄”,”age”:14,”chi”:78,”math”:99,”eng”:93}

  1. package org.example;
  2. import com.google.gson.Gson;
  3. import org.example.mapper.StudentMapper;
  4. import org.example.pojo.Student;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.security.PrivilegedAction;
  11. import java.util.List;
  12. import java.util.PrimitiveIterator;
  13. //使用@SuppressWarnings去除所有告警
  14. @SuppressWarnings("all")
  15. @RestController
  16. public class Controller {
  17. //这个@Autowired是自动接线的意思,只要加上了这个,他就会自动去找Mapper类,其次再从你的数据库配置文件中读取数据
  18. @Autowired
  19. StudentMapper studentMapper;
  20. //把students转成一个字符串,用Gson
  21. //声明一个Gson
  22. private Gson gson= new Gson();
  23. @GetMapping("/test")
  24. public String test() {
  25. //获取数据库数据
  26. List<Student> students = studentMapper.selectList(null);
  27. //通过Gson,将student转成字符串返回字符串到桌面
  28. return gson.toJson(students);
  29. }
  30. //再写一个del接口
  31. @GetMapping("/delete")
  32. public void deleteStudent(){
  33. //既然上面用Gson把students转成一个字符串,那么这里也可以将其转为一个对象使用
  34. String student = "{\"id\":13,\"number\":\"113\",\"name\":\"小黄\",\"age\":14,\"chi\":78,\"math\":99,\"eng\":93}";
  35. Student student1 = gson.fromJson(student,Student.class);
  36. studentMapper.deleteById(student1);
  37. }
  38. }

写好之后,可以看见,我们本来就是有小黄同学的,那么我们访问一下delete看看是否能删除小黄同学的信息


访问delete接口


再返回到test查看所有同学,看看小黄是否还在
可以看见小黄已经被删除了

插入数据

同理,我们可以把之前删除的那段接口复制过来稍作修改就可以用了

  1. //插入接口
  2. @GetMapping("/insert")
  3. public void insertStudent(){
  4. String student = "{\"id\":13,\"number\":\"113\",\"name\":\"蔡徐坤\",\"age\":14,\"chi\":78,\"math\":99,\"eng\":93}";
  5. Student student1 = gson.fromJson(student,Student.class);
  6. studentMapper.insert(student1);
  7. }

只改了这几个地方


然后重启一下SprintBoot访问inster这个接口


现在是没有蔡徐坤同学的信息的


访问insert接口:http://localhost:8080/insert

修改数据

同理,只修改三个地方

  1. //修改信息接口
  2. @GetMapping("/update")
  3. public void updateStudent(){
  4. String student = "{\"id\":13,\"number\":\"113\",\"name\":\"蔡徐坤小黑子\",\"age\":14,\"chi\":78,\"math\":99,\"eng\":93}";
  5. Student student1 = gson.fromJson(student,Student.class);
  6. studentMapper.updateById(student1);
  7. }

重启SprintBoot重启访问
更新数据

这里可以看见接口的数据可以使用了

用户名金币积分时间理由
Track-子羽 40.00 0 2023-05-23 11:11:41 一个受益终生的帖子~~

打赏我,让我更有动力~

1 条回复   |  直到 11个月前 | 470 次浏览

Track-子羽
发表于 11个月前

不属于漏洞分析或者安全相关的文章,故不参与5月活动哦

评论列表

  • 加载数据中...

编写评论内容
登录后才可发表内容
返回顶部 投诉反馈

© 2016 - 2024 掌控者 All Rights Reserved.