如何在 Spring Boot 中的日期间隔内查找?

How to find within a date interval in Spring Boot?

这是我的实体:

这是我的存储库:

@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);   

这是我的控制器:

@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";
}

这是html基于百里香叶的搜索表单:

<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>
                    

在我的数据库中,我有 25-08-2020 和 28-08-2020 之间的数据。

当我输入值时:

我执行后得到这个结果:

谁能帮帮我?

您需要使用 @DateTimeFormat 注释并提供格式化模式参数以将字符串转换为 Date

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

修正后

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

更正后,他接受并执行搜索,但交易未显示

我找到了解决方法,这里是解决方法

1.控制器

@RequestMapping(value="/consulteroperation")
public String findbetwenn(Model model, @RequestParam(name="page", defaultValue =  "0")int p,
@RequestParam(value="dat1",required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") Date dat1, 
@RequestParam(value="dat2",required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") Date dat2) {
    
    try {
        Page<Operation> operation = operationDao.AlllistPageOp(dat1, dat2, p, 150);
        //List<Operation> operation  = operationDao.listSimple();
        model.addAttribute("op", operation);
    } catch (Exception e) {
        return  "redirect:/operationbyDate?dat1="+dat1+"&error="+e.getMessage();
    }
    
    return "empl/seacbydate";
    
}

2。存储库(道)

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

                         <div class="input-group-append">
                           <button class="btn btn-navbar" type="submit">
                             <i class="fas fa-search"></i>
                           </button>
                         </div>
                       </div>
                     </form>