Spring data jpa, pageable 向自定义查询添加自己的规范

问题描述

我有问题。我正在尝试通过使用时间戳按日期过滤来进行自定义查询,但我使用的是 Pageable 并且它在逗号 id desc 限制之后添加查询中,并导致如下错误。我不知道如何解决这个问题。

select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id LIMIT 999999,id desc limit ?
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']
2021-03-05 15:49:59.556  WARN 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlExceptionHelper   : sql Error: 1064,sqlState: 42000
2021-03-05 15:49:59.556 ERROR 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlExceptionHelper   : You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near 'desc limit 300' at line 1

自定义查询

    @Query(nativeQuery = true,value = "select * from lights Where timestamp BETWEEN :startDate AND :endDate ORDER BY id LIMIT 999999")
    Page<Lights> findBetweenDates(@Param("startDate") String startDate,@Param("endDate") String endDate,Pageable pageable);

控制器类

    @GetMapping("/lights")
    public String viewHomePage(Model model,String startDate,String endDate) {
        startDate = "'2021-03-04 15:27:07'";
        endDate = "'2999-12-31 23:59:59'";
        return findPaginated(1,"id","desc",startDate,endDate,model);
    }

    @RequestMapping({"/lights/page/{pageNo}"})
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,@RequestParam("sortField" ) String sortField,@RequestParam("sortDir") String sortDir,@RequestParam(value = "startDate",defaultValue = "'2021-03-04 15:27:07'") String startDate,@RequestParam(value = "endDate",defaultValue = "'2999-12-31 23:59:59'") String endDate,Model model){
        int pageSize = 200;


        Page<Lights> page = lightsMapService.findPaginated(pageNo,pageSize,sortField,sortDir,endDate);
        List<Lights> lightsList = page.getContent();

        model.addAttribute("currentPage",pageNo );
        model.addAttribute("totalPages",page.getTotalPages());
        model.addAttribute("totalItems",page.getTotalElements() );

        model.addAttribute("sortField",sortField );
        model.addAttribute("sortDir",sortDir );
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        model.addAttribute("reverseSortDir",sortDir.equals("asc")?"desc":"asc");


        model.addAttribute("lightsList",lightsList );

        return "sensorsPages/lights";

    }

在我从查询删除 LIMIT 99999 后,我没有再收到错误消息,但是得到了这个并且它不起作用:

2021-03-05 16:09:47.789 DEBUG 3540 --- [nio-8080-exec-7] org.hibernate.sql                        : select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id,id desc limit ?
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)