有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Springboot数据JPA findByDate()

我试图从mysql数据库中检索今天的记录

我尝试使用springboot数据jpa和jpa存储库中的findByDate(Date-Date,Pageable-Pageable)方法,它总是返回空结果

以下是创建的实体代码:

@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_synop")
    private Long id;

    @Column(name="contenu")
    private String contenu;

    @Column(name="date", columnDefinition="TIMESTAMP(0)")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @ManyToOne(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},
            fetch=FetchType.LAZY)
    @JoinColumn(name="id_station")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Station station;

下面是使用Mysql WorkBench创建的Mysql表:

CREATE TABLE `synop` (
  `id_synop` bigint(20) NOT NULL AUTO_INCREMENT,
  `id_station` bigint(20) NOT NULL,
  `contenu` varchar(255) NOT NULL,
  `date` timestamp NOT NULL,
  PRIMARY KEY (`id_synop`),
  UNIQUE KEY `idmsgsynop_UNIQUE` (`id_synop`),
  KEY `fk_synope_station_idx` (`id_station`),
  CONSTRAINT `fk_synope_station` FOREIGN KEY (`id_station`) REFERENCES `station` (`id_station`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

jpa存储库代码:

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SynopRepository extends JpaRepository<Synop, Long> {
    public Page<Synop> findByStationId(Long stationId, Pageable pageable);

    public Page<Synop> findByDate(Date date, Pageable pageable);
}

以及控制器代码:

@GetMapping("/synops-today")
    public Page<Synop> findToday(@RequestParam(defaultValue="1") int page,Pageable pageable) {
        return synopRepository.findByDate(new Date(),PageRequest.of(page-1, 10, Sort.by("date").ascending()));
    }

我希望通过使用findByDate(…)获得今天的记录方法,但它似乎不起作用。我还注意到从MysqlWorkbench查看数据时出现了一个小问题:日期似乎提前了2小时(例如:现在是11:57,但在MysqlWorkbench软件中显示的是9:57,当我查询服务器时间时,显示的是irght tme…) 我其实不在乎小时/分/秒,我只想检索今天的记录


共 (3) 个答案

  1. # 1 楼答案

    我认为,你应该在任何地方从java.util.Date切换到java.sql.Date。 有一些方法可以在这两者之间进行转换

    虽然我更喜欢在逻辑之间而不是在findByDate中。逻辑之间的关系将更加准确

    java.sql.DateJavaDoc

    A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

    To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

    java.util.DateJavaDoc-这个JavaDoc很长,但是相关的部分

    The class Date represents a specific instant in time, with millisecond precision.

  2. # 3 楼答案

    我为我的问题找到了一个有效的解决方案,显然,当使用时间戳数据类型时,JPA数据“findByDate()”方法无法开箱即用。实际上,我需要通过比较时间戳的日期部分来查找记录,所以我使用了如下查询注释:

    @Query("from Synop s where DATE(s.date) = :date")
    public Page<Synop> findByDate(@Param("date") Date date, Pageable pageable);
    

    sql DATE()函数在这里很有帮助,因为在将时间戳的日期部分与我为搜索提供的日期进行比较时,它只考虑时间戳的日期部分。 有了它,我可以在保持时间戳类型的同时搜索比较日期的数据(显示小时、分钟、秒..) 谢谢你的帮助