记录下原来用的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);
}
}