spring - 日期范围之间的 Spring Data JPA 返回 null
问题描述
我正在使用本机查询作为运算符之间的日期,但它返回 null。当我用静态日期运行它时
@Query(value ="SELECT a.name,m.title from meetings m ,attendies a where m.id =a.m_aid and (start_time BETWEEN '2021-04-03 11:00:16' AND '2021-04-03 11:30:15' )",nativeQuery = true)
public List<Object[]> findConflictMettinds( Date startTime, Date endTime );
能够获取数据,但是当尝试使用动态值返回时,它总是返回 null。以下是我迄今为止尝试过的不同方法
@Query(value ="SELECT a.name,m.title from meetings m ,attendies a where m.id =a.m_aid and (m.start_time BETWEEN ?1 AND ?2 )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") Date startTime,@Param("endTime") Date endTime ); @Query(value ="SELECT a.name,m.title from meetings m ,attendies a where m.id =a.m_aid and (m.start_time BETWEEN :startTime AND :endTime )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") Date startTime,@Param("endTime") Date endTime );
解决方案
@Query(value ="SELECT a.name,m.title from meetings m ,attendies a where m.id =a.m_aid and (m.start_time > :startTime AND m.start_time <:endTime )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") String startTime,@Param("endTime") String endTime );
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
repository.findConflictMettinds(dateFormatter.format(startTime), dateFormatter.format(endTime))
此外,您不必使用本机查询,但如果有必要,您可以使用给定的示例。您还可以使用 Spring Data JPA 本机查询查找两个日期之间的搜索记录吗?
推荐阅读
- python - 如何使用 webscraping 来抓取 Inspect elemrnt 中的数据或查看页面源
- c# - 是否可以检索委托函数数据库并执行它?
- sql - 如何列出sybase模式中的所有表?
- mysql - 如何修复mysql访问被拒绝?适用于家庭服务器但不适用于生产服务器
- macos - 在 mac os 上更新 ca 证书
- r - 使用 withProgress() 时如何动态配置 incProgress()?
- ruby-on-rails - RSpec 测试单独通过,整个套件失败
- xml - 将具有西里尔字符的数据帧写入 HDFS 正在更改编码
- dart - 如何在组中创建 ListView?
- codeigniter - 在codeigniter中打印带有条形码的表格的pdf