Postman使用实例
实体类Emp
package com.example.springboot_postman.pojo;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import javax.persistence.*;
import java.io.Serializable;/*** @Author moon* @Date 2023/10/2 18:29* @Description*/
@Entity
@Table(name = "t_emp")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
public class Emp implements Serializable {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "emp_id")private int id;@Column(name = "emp_name")private String name;@Column(name = "age")private int age;@Column(name = "gender")private String gender;@Column(name = "email")private String email;}
JPA接口EmpDao
package com.example.springboot_postman.dao;import com.example.springboot_postman.pojo.Emp;
import org.springframework.data.jpa.repository.JpaRepository;/*** @Author moon* @Date 2023/10/2 18:43* @Description*/
public interface EmpDao extends JpaRepository<Emp, Integer> {
}
1. 分页查询
EmpService:
public Page<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")public ResponseEntity<Page<Emp>> queryByPage(@RequestBody Map<String, Integer> map) {System.out.println(map.get("page") + map.get("size"));return ResponseEntity.ok(this.EmpService.queryByPage(map.get("page"), map.get("size")));}
2. 根据id查询
EmpService:
public Emp queryById(Integer id) {Emp emp = new Emp();emp.setId(id);return empDao.getOne(emp.getId());}
EmpController
@PostMappingpublic ResponseEntity<Emp> add(Emp Emp) {return ResponseEntity.ok(this.EmpService.insert(Emp));}
3. 增
EmpService
public Emp insert(Emp emp) {empDao.save(emp);return emp;}
EmpController
@PostMappingpublic ResponseEntity<Emp> add(@RequestBody Emp Emp) {System.out.println(Emp);return ResponseEntity.ok(this.EmpService.insert(Emp));}
4. 删
EmpService
public Boolean deleteById(Integer id) {Emp emp = new Emp();emp.setId(id);empDao.deleteById(emp.getId());return true;}
EmpController
@DeleteMapping("{id}")public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {return ResponseEntity.ok(this.EmpService.deleteById(id));}
5. 改
EmpService
public Emp update(Emp emp) {empDao.save(emp);return emp;}
EmpController
@PutMappingpublic ResponseEntity<Emp> edit(@RequestBody Emp Emp) {return ResponseEntity.ok(this.EmpService.update(Emp));}
其中分页管理也可以这样写
@GetMappingpublic ResponseEntity<Page<Emp>> queryByPage(@RequestBody Map<String, Integer> map) {return ResponseEntity.ok(this.EmpService.queryByPage(map.get("page"), map.get("size")));}
GET请求这样传参数
package com.example.springboot_logback.web;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class TestController {// getLogger()的入参是当前类,否则输出日志的类名会是错误的private final Logger logger = LoggerFactory.getLogger(TestController.class);@RequestMapping(value = "/test", method = RequestMethod.GET)public String logTest(String name, String age) {System.out.println(name + ":" + age);logger.info("logTest,name:{},age:{}", name, age);return "success";}
}
POST请求才用这个格式