JSP 如何在Sping Boot 中按日期列获取数据或记录?

z2acfund  于 2023-04-03  发布在  其他
关注(0)|答案(3)|浏览(213)

实际上,我有一个Orders实体(表),它有一列,我想根据createddatetime列过滤订单数据。基本情况是,当用户选择任何日期时,例如:createdate是03-06-2021,有3-4条记录存在于该日期,所以它将能够显示总记录存在于该日期从mysql数据库。

@Builder.Default
    @Column(updatable=false)
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd hh:mm:ss")
    private Date createddatetime=new Date();

我在Orders JPA repositiry中创建了如下函数:

List<Orders> findByCreateddatetimeOrderByCreateddatetimeDesc(Date createddate);

下面是我的API控制器代码来获取特定日期的记录。但是在点击这个时,我没有得到任何响应(错误响应)。

@RequestMapping(value = AkApiUrl.onedayorder, method = { RequestMethod.POST, RequestMethod.GET }, produces = {MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<?> onedayorder(HttpServletRequest request, @RequestParam("limit") int limit, @RequestParam("createddate") String createddate ) {
        CustomResponse = ResponseFactory.getResponse(request);
        int min = limit - 1;
        int max = Constants.limitResult;
        try {
            List<Orders> order = null;
            List<Orders> totalorders = null;
            Pageable pageable = new PageRequest(min, max, Sort.Direction.ASC, "orderid");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date onedayorder = sdf.parse(createddate);
            if(createddate != null && createddate != "") {
                order = ordersdao.findByCreateddatetimeOrderByCreateddatetimeDesc(onedayorder, pageable);
                totalorders = ordersdao.findByCreateddatetimeOrderByCreateddatetimeDesc(onedayorder);
            }else {
                order = ordersdao.findAllByStatus(1, pageable);
                totalorders = ordersdao.findAllByStatus(1);
            }
            if (order != null) {
                CustomResponse.setResponse(order);
                CustomResponse.setMaxlimit(Constants.limitResult);
                CustomResponse.setTotalorders(totalorders.size());
                CustomResponse.setStatus(CustomStatus.OK);
                CustomResponse.setStatusCode(CustomStatus.OK_CODE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CustomResponse.setResponse(null);
            CustomResponse.setStatus(CustomStatus.Error);
            CustomResponse.setStatusCode(CustomStatus.Error_CODE);
        }
        return new ResponseEntity<ResponseDao>(CustomResponse, HttpStatus.OK);
    }

我尝试使用@Query创建自定义查询,使用like运算符,但再次遇到类型不匹配错误。我如何修复这个问题?任何建议都是值得赞赏和有价值的。

v1uwarro

v1uwarro1#

在你的数据库中,日期模式是“yyyy-MM-dd hh:mm:ss”,所以执行如下的范围查询:

List<Orders> findByCreateddatetimeBetweenOrderByCreateddatetimeDesc(Date createddateBegin, Date createddateEnd);

给予一天,你想从午夜到午夜。

bcs8qyzn

bcs8qyzn2#

数据似乎以与您搜索的格式不同的格式持久化。您可以应用以下任何解决方案:
1-确保使用相同的日期格式
2-要忽略日期的时间部分,可以使用大于值并传递两个日期,如(createdDate〉= 2021-06-14 and createdDate〈2021-06-15)
3-拆分您搜索日期输入,并使用函数进行搜索,如下例所示

@Query("select e from Event e where year(e.eventDate) = ?1 and month(e.eventDate) = ?2")
List<Entity> getByYearAndMonth(int year, int month);
nfg76nw0

nfg76nw03#

我不确定这是否能解决你的问题,但我用下面的代码成功地检索了数据,希望这能有所帮助:)
我的Repository代码,我给予这段代码的原因是我想让你注意到findAllByCreatedDateBetween()方法的参数类型LocalDateTime

public interface TemperatureRepository extends CrudRepository<Temperature, Integer> {
    List<Temperature> findAllByCreatedDateBetween(LocalDateTime startTime, LocalDateTime endTime);
}

Controller代码如下:

// Get data on a specific day, e.g., from `2023-04-01 00:00:00` to `2023-04-01 23:59:59`
@GetMapping("/find/temperature/{date}")
    public Iterable<Temperature> findCustomerByCreatedDate(@PathVariable LocalDate date) {
        return tempRepository.findAllByCreatedDateBetween(date.atStartOfDay(),
                date.atTime(23, 59, 59, 999999999));
    }

    @GetMapping("/find/temperature")
    public Iterable<Temperature> findCustomerBetweenDate(@RequestParam LocalDate from, @RequestParam LocalDate to) {
        return tempRepository.findAllByCreatedDateBetween(from.atStartOfDay(),
                to.atTime(23, 59, 59, 999999999));
    }

http url

# Get the data on a specific date
GET http://localhost:8080/find/temperature/2023-04-02

# Get the data between two dates
GET http://localhost:8080/find/temperature?from=2023-04-02&to=2023-04-02

相关问题