jpa Query查询,时间查询,in查询(Spring Data Jpa 3)

记录下原来用的jpa Query查询;

解决问题:

使用Query从entry查询数据填充到DTO;

先看entry:

package com.test.entity;

@Entity
@Data
public class EntityDemo implements Serializable {


  private static final long serialVersionUID = 1L;

  @Column(name = "id")
  @JsonProperty("id")
  private String id;

  @Column(name = "start_time")
  @JsonProperty("start_time")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Date startTime;

  @Column(name = "end_time")
  @JsonProperty("end_time")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Date endTime;


  @Column(name = "status")
  @JsonProperty("status")
  private int status;

  @Column(name = "progress")
  @JsonProperty("progress")
  private double progress;

}

再看DTO

import com.test.DTO;

@Data
public class DemoDTO {

    private String userId;

    private String userName;

    private String executionId;

    private String scenarioName;

    private String taskId;

    private String ipAddress;

    private String triggerType;

    private String scenarioId;

    private Date startTime;

    private Date endTime;

    private int repeatCount;

    private int repeatInterval;


    public DemoDTO( String scenarioId, int status, Date startTime, Date endTime) {
        this.scenarioId = scenarioId;
        this.status = status;
        this.startTime = startTime;
        this.endTime = endTime;
    }



}

jpa

public interface DemoJpa extends JpaRepository<EntityDemo, String> {


    @Query("select new com.test.DTO.DemoDTO(s.id, s.status ,s.startTime,s.endTime) from com.test.entity.EntityDemo  s where  s.status = :status and s.startTime >= :searchDate and s.id in (:ids)")
    Page<ScheduleDTO> methodName(
            Pageable page, @Param(value = "status") int status ,  @Param(value = "searchDate") Date searchDate ,@Param(value = "ids") List<String> ids);

controller

@RequestMapping(value = "/xxx", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBody getJobs(@RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "dayNum", required = false) Integer dayNum,
            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {

        Pageable page = PageUtil.createNoErrorPageRequest(pageNumber, pageSize);

        return sth;
    }

page工具类

public class PageUtil {

    public static PageRequest createNoErrorPageRequest(final Integer pageNumber, final Integer pageSize) {
        if(null == pageNumber || null == pageSize ||  pageSize <= 0) {
            return new PageRequest(0, 20);
        }
        if(pageNumber - 1 <= 0) {
            return new PageRequest(0, pageSize);
        }
        return new PageRequest(pageNumber - 1, pageSize);
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

water___Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值