通过弹簧数据的日期恢复对象

时间:2017-04-18 09:27:45

标签: java spring jpa spring-boot

我通过使用spring数据的日期恢复对象时遇到问题问题是对象返回为null,尽管此对象不为null 谢谢你的回复 亲切

@Repository
public interface ProspectStatistiqueRepository extends JpaRepository<ProspectStatique, Long> {
    @Query("select p from ProspectStatique p where p.date_consultation =:date_consultation")
    public ProspectStatique getProspectCountByDate(@Param("date_consultation") Date date_consultation);

}

@Autowired
ProspectStatistiqueRepository prospectStatistiqueRepository;
@GetMapping("/products")
public ProductStatique findProductNumberBydate(@RequestParam String date) throws CustomerException, ParseException {
    DateFormat format = new SimpleDateFormat("MMMM d, yyyy 'at' hh:mm a", Locale.ENGLISH);
    Date dateConvert = format.parse(date);
    System.out.println("date param " + date);
    System.out.println("dateConvert variable" + dateConvert);
    ProductStatique pp = productStatiqueService.getProductCount(dateConvert);
    return pp;
}

1 个答案:

答案 0 :(得分:1)

您使用 JpaRepository ,因此您可以使用“Between”,“After”和“Before”来处理日期:

@Repository
public interface ProspectStatistiqueRepository extends JpaRepository<ProspectStatique, Long> {

//1-Between
    public List<ProspectStatique> findByDate_consultationBetween(Date date1,Date date2);

//2-After
    public List<ProspectStatique> findByDate_consultationAfter(Date date);

//3-Befor
    public List<ProspectStatique> findByDate_consultationBefore(Date date);

}

有关详细信息,请参阅spring doc http://docs.spring.io/spring-data/jpa/docs/1.3.4.RELEASE/reference/html/jpa.repositories.html