Buscar..


Introducción

En esta sección agregaré un ejemplo para el controlador de resto de inicio de Spring con Get y post request.

Controlador de reposacabezas de muelle

En este ejemplo, mostraré cómo formular un controlador de descanso para obtener y publicar datos en la base de datos utilizando JPA con la mayor facilidad y el menor código.

En este ejemplo, nos referiremos a la tabla de datos llamada buyerRequirement.

BuyingRequirement.java

@Entity @Table (name = "BUYINGREQUIREMENTS") @NamedQueries ({@NamedQuery (name = "BuyingRequirement.findAll", query = "SELECT b FROM BuyingRequirement b"))) clase pública BuyingRequirement Extensiones de dominio Serializable {privado final final privado serialVersionUID = 1L;

@Column(name = "PRODUCT_NAME", nullable = false)
private String productname;

@Column(name = "NAME", nullable = false)
private String name;

@Column(name = "MOBILE", nullable = false)
private String mobile;

@Column(name = "EMAIL", nullable = false)
private String email;

@Column(name = "CITY")
private String city;


public BuyingRequirement() {
}


public String getProductname() {
    return productname;
}

public void setProductname(String productname) {
    this.productname = productname;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

Esta es la clase de entidad que incluye el parámetro que se refiere a las columnas en la tabla byuingRequirement y sus captadores y definidores.

IBuyingRequirementsRepository.java (interfaz JPA)

@Repository
@RepositoryRestResource
public interface IBuyingRequirementsRepository extends JpaRepository<BuyingRequirement, UUID> {
   // Page<BuyingRequirement> findAllByOrderByCreatedDesc(Pageable pageable);
    Page<BuyingRequirement> findAllByOrderByCreatedDesc(Pageable pageable);
    Page<BuyingRequirement> findByNameContainingIgnoreCase(@Param("name") String name, Pageable pageable);
}

BuyingRequirementController.java

@RestController
@RequestMapping("/api/v1")
public class BuyingRequirementController {

    @Autowired
    IBuyingRequirementsRepository iBuyingRequirementsRepository;
    Email email = new Email();

    BuyerRequirementTemplate buyerRequirementTemplate = new BuyerRequirementTemplate();

    private String To = "[email protected]";
    // private String To = "[email protected]";
    private String Subject = "Buyer Request From Pharmerz ";

    @PostMapping(value = "/buyingRequirement")
    public ResponseEntity<BuyingRequirement> CreateBuyingRequirement(@RequestBody BuyingRequirement buyingRequirements) {

        String productname = buyingRequirements.getProductname();
        String name = buyingRequirements.getName();
        String mobile = buyingRequirements.getMobile();
        String emails = buyingRequirements.getEmail();
        String city = buyingRequirements.getCity();
        if (city == null) {
            city = "-";
        }

        String HTMLBODY = buyerRequirementTemplate.template(productname, name, emails, mobile, city);

        email.SendMail(To, Subject, HTMLBODY);


        iBuyingRequirementsRepository.save(buyingRequirements);
        return new ResponseEntity<BuyingRequirement>(buyingRequirements, HttpStatus.CREATED);
    }


    @GetMapping(value = "/buyingRequirements")
    public Page<BuyingRequirement> getAllBuyingRequirements(Pageable pageable) {

        Page requirements = iBuyingRequirementsRepository.findAllByOrderByCreatedDesc(pageable);
        return requirements;
    }

    @GetMapping(value = "/buyingRequirmentByName/{name}")
    public Page<BuyingRequirement> getByName(@PathVariable String name,Pageable pageable){
        Page buyersByName = iBuyingRequirementsRepository.findByNameContainingIgnoreCase(name,pageable);

        return buyersByName;
    }
}

Incluye el método

  1. Método de publicación que publica los datos en la base de datos.
  2. Obtenga el método que obtiene todos los registros de la tabla de compras de requisitos.
  3. Este es también un método de obtención que encontrará el requisito de compra por el nombre de la persona.


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow