Home > Mobile >  How to fatch data from database by using Date in Spring boot
How to fatch data from database by using Date in Spring boot

Time:04-11

I create a project on spring boot and project connected with H2 database . Save the data in a database through the post Mapping rest API but parameter of our class name ,id, address and LocalDate date.

I fatch the data data through using method getOrderById but i can't fatch the data between to dates.

Help me to fatch the data between to dates by using spring boot REST API.

Entity Class Order

CodePudding user response:

If you want to write a custom query to fetch data from database, in your OrderRepository interface, you have to declare a function with annotation @Query and write your query in paranthesis. You can also user your function parameters in query by using :parameter and annotating parameter with @Param.

@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {

@Query("SELECT o FROM Order o WHERE o.date BETWEEN :fromDate AND :toDate")
List<Order> getPostsBetweenDates(@Param("fromDate") LocalDate fromDate,
                                 @Param("ToDate") LocalDate toDate);

CodePudding user response:

This query might help you to get the right result.

@Query(nativeQuery = true, value="select  from Order o where o.date between :startDate and :endDate")
List<Order> getOrderById(@Param("startDate") LocalDate date1, @Param("endDate") LocalDate date2);

CodePudding user response:

You can edit this query to get your data:

@Query(value ="select * from table_name where date = :date, nativeQuery = true)

List<Entity> findByDate(@Param("date") LocalDate date);

or

List<Entity> findByDate(LocalDateTime date);

this method you can put it directly in your Repository to get data by date.

Here, findBy"Date" Date is your column name.

  • Related