第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Spring Data JPA 按日期和時間進行搜索?

如何使用 Spring Data JPA 按日期和時間進行搜索?

動漫人物 2023-05-10 15:39:49
我傾向于 Spring Data JPA,我有一個應用程序,我嘗試按日期搜索,但它不起作用。問題是當我嘗試在數(shù)據(jù)庫中搜索日期時。我有一個 MySQL 數(shù)據(jù)庫,如果我直接在 MySQL Workbench 中搜索,則可以正常工作,但如果我嘗試從我的應用程序中搜索,則無法正常工作。我什么也得不到。如果我嘗試搜索其他內容,我會得到結果。因此,當我嘗試搜索日期時,我確定存在問題。我不知道是什么問題。任何反饋將不勝感激。謝謝你!更新一開始,該應用程序運行良好。我可以按數(shù)據(jù)庫中的日期搜索。之后,我添加了 Spring Security 和更多實體,之后我無法按數(shù)據(jù)庫中的日期進行搜索,并且我沒有觸及搜索方法。這很奇怪?,F(xiàn)在我有第一個版本的應用程序并且可以運行,第二個版本不起作用。并且這兩個應用程序都針對同一個數(shù)據(jù)庫實例。但問題僅在于當我嘗試按日期搜索時,如果我按 departureCity 和 arrivalCity 進行搜索,它會完美運行,當我嘗試按日期搜索時,我什么也得不到,列表是空的。這是無法運行的應用程序版本。這是 Github 鏈接 -> https://github.com/eveningstar33/flightreservationapp這是另一個完美運行的應用程序:https ://github.com/eveningstar33/flightreservation實體:抽象實體類:package com.dgs.flightreservationapp.entities;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.MappedSuperclass;@MappedSuperclasspublic class AbstractEntity {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }}
查看完整描述

4 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

問題發(fā)生是因為數(shù)據(jù)轉換


com.mysql.cj.jdbc.ClientPreparedStatement:選擇 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作為 operatin7_0_ 來自航班 flight0_,其中 flight0_.departure_city='AUS' and flight0_.arrival_city='NYC' and flight0_.date_of_departure='2018-02-04 18:30:00.0'


如您所見,日期未轉換為正確的格式,因此我們推出了自己的轉換器來解決此問題。


flightreservationapp/converters/LocalDateAttributeConverter.java


package com.dgs.flightreservationapp.converters;


import javax.persistence.AttributeConverter;

import javax.persistence.Converter;

import java.time.LocalDate;


@Converter(autoApply = true)

public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, String> {


    @Override

    public String convertToDatabaseColumn(LocalDate locDate) {

        return locDate == null ? null : locDate.toString();

    }


    @Override

    public LocalDate convertToEntityAttribute(String sqlDate) {

        return sqlDate == null ? null : LocalDate.parse(sqlDate);

    }

}

添加此文件后,您將開始獲得結果

http://img1.sycdn.imooc.com//645b4a66000109eb06500309.jpg

您還有其他選擇。為 JPA 方法添加注解


    List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to,

                                                                     @DateTimeFormat(iso= DateTimeFormat.ISO.DATE)

                                                                     LocalDate departureDate);

然后您需要確保將時區(qū)也設置為 UTC


public class FlightReservationAppApplication {


    public static void main(String[] args) {

        SpringApplication.run(FlightReservationAppApplication.class, args);

    }


    @PostConstruct

    void init() {

        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    }

}

另一種選擇是在你的程序中使用更高版本的 hibernatepom.xml


        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-java8</artifactId>

            <version>5.1.0.Final</version>

        </dependency>

并設置時區(qū)


public class FlightReservationAppApplication {


    public static void main(String[] args) {

        SpringApplication.run(FlightReservationAppApplication.class, args);

    }


    @PostConstruct

    void init() {

        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    }

}


查看完整回答
反對 回復 2023-05-10
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

我認為您需要在實體中注釋日期字段@Temporal(TemporalType.DATE)


喜歡


@Entity

public class Flight extends AbstractEntity {


    private String flightNumber;

    private String operatingAirlines;

    private String departureCity;

    private String arrivalCity;


    @Temporal(TemporalType.DATE)

    private Date dateOfDeparture;


    private Timestamp estimatedDepartureTime;

}


查看完整回答
反對 回復 2023-05-10
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

在你的非工作存儲庫中,你有這個額外的配置application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/reservation?useSSL=false&serverTimezone=UTC

該部分serverTimezone=UTC導致您輸入的日期和數(shù)據(jù)庫中的日期不匹配=>錯誤的結果

所以現(xiàn)在,要么將兩者設置為相同的時區(qū),要么避免在 Java 中使用 Local*** 類


查看完整回答
反對 回復 2023-05-10
?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

MySqlDate類型不包含任何時間部分,因此Date您在實體上使用的類型Flight及其時間部分可能會破壞您的查詢。嘗試Date用LocalDateJava8 Date/Time API 替換,它也只包含沒有任何時間信息的日期。


private LocalDate dateOfDeparture;

&


@Param("dateOfDeparture") LocalDate departureDate

最重要的是,您不需要@Query為這樣一個簡單的邏輯使用 a 。您可以使用方法名稱作為查詢,這是 Spring Data JPA 的一個很好的特性。


public interface FlightRepository extends JpaRepository<Flight, Long>  {


    List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to, LocalDate departureDate);

}

并從您的控制器中調用它;


@PostMapping("/processFlights")

public String processFlights(..) {


    List<Flight> flights = flightRepository.findByDepartureCityAndArrivalCityAndDateOfDeparture(from, to, departureDate);


    modelMap.addAttribute("flights", flights);

    return "displayFlights";

}

雖然它看起來很糟糕,因為名字變得太長了。您可以添加一個默認方法來包裝它以隱藏丑陋的命名。


public interface FlightRepository extends JpaRepository<Flight, Long>  {


    List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to, LocalDate departureDate);


    // you can use this from your controller

    default List<Flight> findFlights(String from, String to, LocalDate departureDate) {

        return findByDepartureCityAndArrivalCityAndDateOfDeparture(from, to, departureDate);

    }

}

這樣做將隱藏由于在其中使用@Query注釋和硬編碼 JPQL 而導致的復雜性和可能的錯誤,除非對于更高級的情況絕對必要。


我在你的中添加了以下代碼flightreservationapp;


@SpringBootApplication

public class FlightReservationAppApplication {


    public static void main(String[] args) {

        ConfigurableApplicationContext run = SpringApplication.run(FlightReservationAppApplication.class, args);

        FlightRepository flightRepository = run.getBean(FlightRepository.class);


        Flight flight = new Flight();

        flight.setDepartureCity("AUS");

        flight.setArrivalCity("NYC");

        flight.setDateOfDeparture(LocalDate.of(2018, 1, 5));

        flightRepository.save(flight);


        List<Flight> list = flightRepository.findByDepartureCityAndArrivalCityAndDateOfDeparture("AUS", "NYC", LocalDate.of(2018, 1, 5));

        System.out.println(list.size());  // prints out 1

    }

}

存儲庫有效,我無法運行其他端點等,但查詢/模式等沒有問題。


查看完整回答
反對 回復 2023-05-10
  • 4 回答
  • 0 關注
  • 394 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號