Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

java - Spring Boot JPA @Query problem how to fix

I need some help. I'm writing web applications using JPA. I make it with a tutorials on YouTube and other pages. I Make a lot but i need help to view one thing from my aplication. I have clients and also dates of appointments because it is an application for a hairdressing salon.

This is how look my aplication in InteliJ https://i.stack.imgur.com/HlDXK.png I want to display clients where first name is for example "Patryk"

Here is code from model.klienci

package figura.zaklad_fryzjerski_v3.model;


import javax.persistence.*;

@Entity
@Table(name = "klienci")
public class Klienci {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_klienta")
    private Integer id_klienta;

    @Column(name = "imie")
    private String imieKlienta;

    @Column(name = "nazwisko")
    private String nazwiskoKlienta;

    @Column(name = "nr_telefonu_klienta")
    private Integer nrTelefonuKlienta;

    @Column(name = "adres_email")
    private  String adresEmailKlienta;
    getters and setters

Here is code from repository.KlienciRepository

package figura.zaklad_fryzjerski_v3.repository;

import figura.zaklad_fryzjerski_v3.model.Klienci;
import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;



@Repository
public interface KlienciRepository extends JpaRepository<Klienci, Integer> {

}

Here is code from service.KlienciService

package figura.zaklad_fryzjerski_v3.service.klienci;

import figura.zaklad_fryzjerski_v3.model.Klienci;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;


import java.util.List;

public interface KlienciService {

    List<Klienci> getAllKlieci();
    void saveKlienci(Klienci klienci);
    Klienci getKlienciById(Integer id_klienta);
    void usunKlientaById(Integer id_klienta);
    Page<Klienci> findPaginated(int pageNo, int pageSize);
}

Here is code from service.KlienciServiceImpl

package figura.zaklad_fryzjerski_v3.service.klienci;

    import figura.zaklad_fryzjerski_v3.model.Klienci;
    import figura.zaklad_fryzjerski_v3.repository.KlienciRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Service;

    import java.util.List;
import java.util.Optional;

@Service
//@Transactional
public class KlienciServiceImpl implements KlienciService{

    @Autowired
    private KlienciRepository klienciRepository;

    @Override
    public List<Klienci> getAllKlieci() {
        return klienciRepository.findAll();
    }

    @Override
    public void saveKlienci(Klienci klienci){
        this.klienciRepository.save(klienci);
    }

    @Override
    public Klienci getKlienciById(Integer id_klienta) {
        Optional<Klienci> optional = klienciRepository.findById(id_klienta);
        Klienci klienci = null;
        if(optional.isPresent()){
            klienci = optional.get();
        }else{
            throw new RuntimeException("Klient nie znaleziony po id :: "+ id_klienta);
        }
        return klienci;
    }

    @Override
    public void usunKlientaById(Integer id_klienta){
        this.klienciRepository.deleteById(id_klienta);
    }

    @Override
    public Page<Klienci> findPaginated(int pageNo, int pageSize) {

        Pageable pageable = PageRequest.of(pageNo - 1,pageSize);
        return this.klienciRepository.findAll(pageable);
    }
}

And i also include you controler here what i use to dispaly things on pages in my application

 //klient wy?wietlanie listy klientów
    @Autowired
    public KlienciService klienciService;
    @GetMapping("/klient_baza")
    public String getKlient_baza(Model model){
        model.addAttribute("listaKlientow",klienciService.getAllKlieci());
        return findPaginated(1, model);
    }

    //Dodawanie klientów do bazy danych
    @GetMapping("/klient_dodaj")
    public String getKlient_dodaj(Model model){
        Klienci klienci = new Klienci();
        model.addAttribute("klienci",klienci);
        return "/klient/klient_dodaj";
    }

    //zapisywanie klientów do bazy danych
    @PostMapping("/klientZapisz")
    public String saveKlient(@ModelAttribute("klienci") Klienci klienci, Model model){
        klienciService.saveKlienci(klienci);

        model.addAttribute("typ", "dodanie_rekordu");
        model.addAttribute("sukces", "Pomy?lnie dodano klienta");

        return getKlient_baza(model);
    }

    //strona z edytowaniem klienta
    @GetMapping("/klient_dodaj/{id_klienta}")
    public String showFormForUpdate(@PathVariable(value = "id_klienta") Integer id_klienta, Model model) {

        Klienci klienci = klienciService.getKlienciById(id_klienta);
        model.addAttribute("klienci", klienci);
        model.addAttribute("edycja", "true");
        return "/klient/klient_dodaj";

    }
    //usuwanie klienta  z bazy danych
    @GetMapping("/usun_klienta/{id_klienta}")
    public String usunKlienta(@PathVariable(value = "id_klienta")Integer id_klienta, Model model){

        model.addAttribute("typ","usuniecie_rekordu");
        model.addAttribute("sukces","Pomy?lnie usuni?to klienta");

        this.klienciService.usunKlientaById(id_klienta);
        return getKlient_baza(model);
    }
    //podzia? na strony
    @GetMapping("/klient_baza/{pageNo}")
    public String findPaginated(@PathVariable(value = "pageNo")int pageNo, Model model){

        int pageSize = 10;
        Page<Klienci> page = klienciService.findPaginated(pageNo,pageSize);
        List<Klienci> klienciList = page.getContent();

        model.addAttribute("aktualnaStrona", pageNo);
        model.addAttribute("wszystkieStrony", page.getTotalPages());
        model.addAttribute("total", page.getTotalElements());


        model.addAttribute("listaKlientow", klienciList);
        return "/klient/klient_baza";

        }

How to add here QUERY METHOD do display only this clients where name is "Patryk" using @Query?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't really need to use a method annotated with @Query. Spring Data is "smart" enough to generate a query based on the model properties. You can just add a method declaration to your KlienciRepository interface:

List<Klienci> listKlienciByNazwiskoKlienta(String name);

Spring data will look into the Klienci model for a property called nazwiskoKlienta and build a query that does an equality check using the value passed in. If you wanted to do a wildcard query, Spring data can handle that automatically too:

List<Klienci> listKlienciByNazwiskoKlientaContaing(String name);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...