首页 > 解决方案 > Spring Boot Mysql查询根据@query值获取所有记录

问题描述

我正在尝试使用 Spring Boot JPA 从 MySQL 表中检索所有记录。以下是存储库定义

MovieRepository 类

@Query("From MovieEntity m, TheaterMovieShowEntity tms, TheaterEntity t where t.city=?1 and t.theaterid=tms.theaterid and m.movieid=tms.movieid and t.seatsavailable>=?2")
    List<Movie> getMoviesDetails(String cityName, int noOfTickets)throws MovieException;

电影服务类

public List<Movie> getMoviesDetails(String cityName, int noOfTickets)throws MovieException{
        List<Movie>moviesIntheCityList = new ArrayList<Movie>();
        **moviesIntheCityList = (List<Movie>) movieRepository.getMoviesDetails(cityName, noOfTickets);**
        System.out.println("in the getMoviesDetails after querying"+moviesIntheCityList); // This is null
        return moviesIntheCityList;
    }

我错过了什么?

标签: mysqlspring-bootspring-boot-jpa

解决方案


只需使用

List<Movie> movieRepository.findByCityNameAndNoOfTickets(String cityName, int noOfTickets);

在您的 MovieRepository 中。然后调用它。在 Spring Data JPA 中,如果您遵循正确的命名约定,那么您只需在 Repository 中声明方法的名称,JPA 将为您实现它。


推荐阅读