【问题标题】:unable to load data in chrome from mysql database using thymeleaf but in console getting query无法使用 thymeleaf 从 mysql 数据库加载 chrome 中的数据,但在控制台中获取查询
【发布时间】:2022-01-15 19:49:16
【问题描述】:
**Flight.java**
```package com.shahbaz.flightreservation.entities;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Entity;
@Entity
public class Flight extends AbstractEntity {

    private String flightNumber;
    private String operatingAirlines;
    private String departureCity;
    private String arrivalCity;
    private Date dateOfDeparture;
    private Timestamp estimatedDepartureTime;
    
    public String getFlightNumber() {
        return flightNumber;
    }
    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }
    public String getOperatingAirlines() {
        return operatingAirlines;
    }
    public void setOperatingAirlines(String operatingAirlines) {
        this.operatingAirlines = operatingAirlines;
    }
    public String getDepartureCity() {
        return departureCity;
    }
    public void setDepartureCity(String departureCity) {
        this.departureCity = departureCity;
    }
    public String getArrivalCity() {
        return arrivalCity;
    }
    public void setArrivalCity(String arrivalCity) {
        this.arrivalCity = arrivalCity;
    }
    public Date getDateOfDeparture() {
        return dateOfDeparture;
    }
    public void setDateOfDeparture(Date dateOfDeparture) {
        this.dateOfDeparture = dateOfDeparture;
    }
    public Timestamp getEstimatedDepartureTime() {
        return estimatedDepartureTime;
    }
    public void setEstimatedDepartureTime(Timestamp estimatedDepartureTime) {
        this.estimatedDepartureTime = estimatedDepartureTime;
    }
    
}```

**AbstractEntity**

```package com.shahbaz.flightreservation.entities;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class AbstractEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    
}```

**FlightRepository**

```package com.shahbaz.flightreservation.repos;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.shahbaz.flightreservation.entities.Flight;

public interface FlightRepository extends JpaRepository<Flight,Long> {

    @Query("from Flight where departureCity=:departureCity and arrivalCity=:arrivalCity and dateOfDeparture=:dateOfDeparture")
    List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String to,@Param("dateOfDeparture") Date departureDate);
    
}```

**FlightController**

```package com.shahbaz.flightreservation.controllers;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shahbaz.flightreservation.entities.Flight;
import com.shahbaz.flightreservation.repos.FlightRepository;

@Controller
public class FlightController {

    @Autowired
    FlightRepository flightRepository;
    
    @RequestMapping("/findFlights")
    public String findFlights(@RequestParam("from") String from,@RequestParam("to") String to,
            @RequestParam("departureDate") @DateTimeFormat(pattern="mm-dd-yyyy") Date departureDate,ModelMap modelMap)
    {
        List<Flight> flights=flightRepository.findFlights(from,to,departureDate);
        modelMap.addAttribute("flights", flights);
        return "displayFlights";
    }
    
    
}```
**displayFlights.html**

```<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Flights</title>
</head>
<body>
<h2>Flights:</h2>
<table>
<tr>
<th>Airlines</th>
<th>Departure City</th>
<th>Arrival City</th>
<th>Departure Time</th>
</tr>

<tr th:each="flight: ${flights}">
<td th:text="${flight.operatingAirlines}"></td>
<td th:text="${flight.departureCity}"></td>
<td th:text="${flight.arrivalCity}"></td>
<td th:text="${flight.estimatedDepartureTime}"></td>
<td><a th:href="@{'showCompleteReservation?flightId='+${flight.id}}">Select</a></td>
</tr>
</table>
</body>
</html>```

application.properties

*spring.datasource.url=jdbc:mysql://localhost:3306/reservation?

useSSL=false&serverTimezone=CET&useLegacyDatetimeCode=false

spring.datasource.username=root

spring.datasource.password=root

server.servlet.context-path=/flightreservation

spring.jpa.show-sql=true*

我在控制台中收到查询,但在发送请求时输出未反映在 chrome 中

休眠:选择 flight0_.id 作为 id1_0_,flight0_.arrival_city 作为arrival_2_0_,flight0_.date_of_departure 作为date_of_3_0_,flight0_.departure_city 作为departur4_0_,flight0_.estimated_departure_time 作为estimate5_0_,flight0_.flight_number 作为flight_n6_0_,flight0_.operating_airlines 作为operating从航班 flight0_ where flight0_.departure_city=?和 flight0_.arrival_city=?和 flight0_.date_of_departure=? 我正在搜索数据库中可用的数据,日期格式也适合 dateOfDeparture

【问题讨论】:

  • 你不能给fromtodepartureTime赋值
  • 我正在使用 chrome 传递值:-from to 和 leaveDate 如果我不通过,那么我将如何从数据库中获取它们之间的航班可用性。如果你能详细说明

标签: java spring-boot hibernate thymeleaf


【解决方案1】:

请更改 FlightRepository 中 findFlights 方法的 @Query 注释中的自定义查询

来自

 @Query("from Flight where departureCity=:departureCity and arrivalCity=:arrivalCity and dateOfDeparture=:dateOfDeparture")
List<Flight> findFlights(@Param("departureCity") String from,@Param("arrivalCity") String to,@Param("dateOfDeparture") Date departureDate);

@Query("from Flight where departureCity=:fromCity and arrivalCity=:toCity and dateOfDeparture=:departureDate")
List<Flight> findFlights(@Param("departureCity") String fromCity,@Param("arrivalCity") String toCity,@Param("dateOfDeparture") Date departureDate);

基本上,这个想法是,您需要使用 findFlights 方法的参数来构建自定义查询,而不是 @Param 注释中的参数。

【讨论】:

  • 在将 FlightRepository 更改为您建议的版本后出现错误,但现在在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“flightController”的 bean 时出错':通过字段'flightRepository'表达的不满足的依赖关系;
猜你喜欢
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 2018-11-24
  • 2015-01-12
  • 2020-11-16
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多