0


Postman使用实例

Postman使用实例

实体类Emp

packagecom.example.springboot_postman.pojo;importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importjavax.persistence.*;importjava.io.Serializable;/**
 * @Author moon
 * @Date 2023/10/2 18:29
 * @Description
 */@Entity@Table(name ="t_emp")@Data@AllArgsConstructor@NoArgsConstructor@JsonIgnoreProperties(value ={"hibernateLazyInitializer"})publicclassEmpimplementsSerializable{@Id@GeneratedValue(strategy =GenerationType.IDENTITY)@Column(name ="emp_id")privateint id;@Column(name ="emp_name")privateString name;@Column(name ="age")privateint age;@Column(name ="gender")privateString gender;@Column(name ="email")privateString email;}

JPA接口EmpDao

packagecom.example.springboot_postman.dao;importcom.example.springboot_postman.pojo.Emp;importorg.springframework.data.jpa.repository.JpaRepository;/**
 * @Author moon
 * @Date 2023/10/2 18:43
 * @Description
 */publicinterfaceEmpDaoextendsJpaRepository<Emp,Integer>{}

1. 分页查询

EmpService:

publicPage<Emp>queryByPage(Integer page,Integer size){Pageable pageable =PageRequest.of(page, size,Sort.by(Sort.Direction.DESC,"id"));return empDao.findAll(pageable);}

EmpController

@RequestMapping("/getByPage")publicResponseEntity<Page<Emp>>queryByPage(@RequestBodyMap<String,Integer> map){System.out.println(map.get("page")+ map.get("size"));returnResponseEntity.ok(this.EmpService.queryByPage(map.get("page"), map.get("size")));}

在这里插入图片描述

2. 根据id查询

EmpService:

publicEmpqueryById(Integer id){Emp emp =newEmp();
        emp.setId(id);return empDao.getOne(emp.getId());}

EmpController

@PostMappingpublicResponseEntity<Emp>add(EmpEmp){returnResponseEntity.ok(this.EmpService.insert(Emp));}

在这里插入图片描述

3. 增

EmpService

publicEmpinsert(Emp emp){
        empDao.save(emp);return emp;}

EmpController

@PostMappingpublicResponseEntity<Emp>add(@RequestBodyEmpEmp){System.out.println(Emp);returnResponseEntity.ok(this.EmpService.insert(Emp));}

在这里插入图片描述

4. 删

EmpService

publicBooleandeleteById(Integer id){Emp emp =newEmp();
        emp.setId(id);
        empDao.deleteById(emp.getId());returntrue;}

EmpController

@DeleteMapping("{id}")publicResponseEntity<Boolean>deleteById(@PathVariableInteger id){returnResponseEntity.ok(this.EmpService.deleteById(id));}

在这里插入图片描述

5. 改

EmpService

publicEmpupdate(Emp emp){
        empDao.save(emp);return emp;}

EmpController

@PutMappingpublicResponseEntity<Emp>edit(@RequestBodyEmpEmp){returnResponseEntity.ok(this.EmpService.update(Emp));}

在这里插入图片描述
其中分页管理也可以这样写

@GetMappingpublicResponseEntity<Page<Emp>>queryByPage(@RequestBodyMap<String,Integer> map){returnResponseEntity.ok(this.EmpService.queryByPage(map.get("page"), map.get("size")));}

GET请求这样传参数

packagecom.example.springboot_logback.web;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/test")publicclassTestController{// getLogger()的入参是当前类,否则输出日志的类名会是错误的privatefinalLogger logger =LoggerFactory.getLogger(TestController.class);@RequestMapping(value ="/test", method =RequestMethod.GET)publicStringlogTest(String name,String age){System.out.println(name +":"+ age);
        logger.info("logTest,name:{},age:{}", name, age);return"success";}}

在这里插入图片描述
POST请求才用这个格式

在这里插入图片描述

标签: postman 测试工具

本文转载自: https://blog.csdn.net/qq_41298636/article/details/133513188
版权归原作者 lmoon33 所有, 如有侵权,请联系我们删除。

“Postman使用实例”的评论:

还没有评论