gpt4 book ai didi

java - Spring Boot使用jpa sql和pageable

转载 作者:行者123 更新时间:2023-11-30 07:10:36 24 4
gpt4 key购买 nike

我是java新手,我想从数据库中选择数据并使用Pageable。而且我必须在jpa中使用hql。
RestController.java中的代码:

@RestController
@RequestMapping("/api")
public class ReportController {

private static final Logger logger = LoggerFactory.getLogger(ReportController.class);
private static Validator validator;

@Autowired
private ReportService reportService;

@InitBinder
protected void initBinder(WebDataBinder binder) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}

@RequestMapping(value = "/reports", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
public JSONObject getReports(@RequestBody ReportQueryRequest reportQueryRequest){
logger.info("[Monitor List] partial / Params: {}", reportQueryRequest);
Set<ConstraintViolation<ReportQueryRequest>> errors = validator.validate(reportQueryRequest);
String name = reportQueryRequest.getReportName();
int status = reportQueryRequest.getReportStatus();
String createTime = reportQueryRequest.getCreateTime();
int statusArray[];
if (status == 3) {
statusArray = new int[2];
statusArray[0] = 0;
statusArray[1] = 1;
}else {
statusArray = new int[1];
statusArray[0] = status;
}
if (name == null){
name = "";
}
if (createTime == null || !createTime.contains(",")){
return ResponseWrapper.buildResponse(RTCodeEnum.C_PARAM_ERROR, "createTime error");
}
createTime = createTime.replace(" ", "");
String[] createTimeArray = createTime.split(",");
long startTime = Long.valueOf(createTimeArray[0]);
long endTime = Long.valueOf(createTimeArray[1]);
List<Report> reportList = reportService.getReportList(startTime, endTime, name, statusArray);
return ResponseWrapper.buildResponse(RTCodeEnum.C_OK, reportList);
}
}

Service.java中的代码:

@Service
public class ReportService extends CrudService<Report, ReportRepository> {
@Override
@Autowired
public void setRepo(ReportRepository repo) {
this.repo = repo;
}

@Override
public Report copy(Report from, Report to) {
to = from;
return to;
}

@Autowired
private ReportRepository reportRepository;

public List<Report> getReportList(long startTime, long endTime, String name, int[] status) {
return reportRepository.findByCondition(startTime, endTime, name, status);
}
}

Repository.java中的代码:

@Repository
public interface ReportRepository extends CrudRepository<Report,Long>{
@Query(value = "select r from Report r where r.createTime >= :startTime and r.createTime <= :endTime and r.name like %:name% and r.status in :status order by r.updateTime DESC")
List<Report> findByCondition(
@Param("startTime") long startTime,
@Param("endTime") long endTime,
@Param("name") String name,
@Param("status") int[] status
);
}

我的程序运行成功。现在,我想使用 Pageable 但我不知道如何对名为 findByCondition 的方法返回的数据进行分页。

最佳答案

使用 spring-data-jpa,您只需使用 org.springframework.data.domain.Page 返回类型和 org.springframework.data.domain.Pageable方法参数。

@Repository
public interface ReportRepository extends CrudRepository<Report,Long>{
@Query(value = "select r from Report r where r.createTime >= :startTime and r.createTime <= :endTime and r.name like %:name% and r.status in :status order by r.updateTime DESC")
Page<Report> findByCondition(
@Param("startTime") long startTime,
@Param("endTime") long endTime,
@Param("name") String name,
@Param("status") int[] status,
Pageable pageParameters
);
}

spring-data-jpa 识别该返回类型并将相应地编码(marshal)您的数据。 Pageable 的各个成员将帮助您进行限制和偏移。请注意,查询中的 ORDER BY 子句可能与 Pageable 中的排序选项冲突。

关于java - Spring Boot使用jpa sql和pageable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com