Search code examples
spring-bootspring-data-jpajpa-2.0jpqljava-ee-8

How to find within a date interval in Spring Boot?


Here is my entity:

enter image description here

Here is my repository:

@Query(value="SELECT * FROM operation WHERE date_operation  BETWEEN 'dat1' AND 'dat2'", nativeQuery =true)
public Page<Operation> AlllistByDate(@Param("dat1")Date dt1, @Param("dat2")Date dat2, Pageable page);   

Here is my controller:

@RequestMapping(value="operationbyDate",  method = RequestMethod.GET)
public String  operationbyDaten(Model model, @RequestParam(name="page", defaultValue =  "0")int p,
        @RequestParam(value="dat1",required=false) Date dat1,
        @RequestParam(value="dat2",required=false) Date dat2) throws ParseException {

    try {
        Page<Operation> operation = operationDao.AlllistPageOp(dat1, dat2, p, 150);
        int pageCount = operation.getTotalPages();
        int [] pages =  new  int [pageCount];
        for (int i = 0; i < pageCount; i++) 
            pages[i]=i;
        model.addAttribute("pages", pages);
        model.addAttribute("pagecourente", p);
        model.addAttribute("dat1",dat1);
        model.addAttribute("dat2",dat2);

        model.addAttribute("listOperbydate", operation);
        
    } catch (Exception e) {
        // TODO: handle exception
        return  "redirect:/operationbyDate?dat1="+dat1+"&error="+e.getMessage();
    }
    return "empl/seacbydate";
}

Here is html search form based on the thymeleaf:

<form class="form-inline ml-3"  th:action="@{operationbyDate}" method="get">
                      <div class="input-group input-group-sm">
                        <input class="form-control form-control-navbar"  type="date"   name="dat1" th:value="${dat1}" placeholder="Search" aria-label="Search">
                        <input class="form-control form-control-navbar"  type="date"   name="dat2" th:value="${dat2}" placeholder="Search" aria-label="Search">
                     
                        <div class="input-group-append">
                          <button class="btn btn-navbar" type="submit">
                            <i class="fas fa-search"></i>
                          </button>
                        </div>
                      </div>
                    </form>
                    

In my database, I have the data which is between 25-08-2020 and 28-08-2020.

enter image description here

When I enter the values:

enter image description here

I get this result after execution:

enter image description here

Can anyone help me?


Solution

  • after correction

    Hibernate: SELECT * FROM operation WHERE date_operation BETWEEN 'dat1' AND 'dat2' limit ?

    after correction, he accepts and performs the search but the deal is not displayed

    enter image description here