您可以使用Spring的规范API,它是JPA标准API的包装。确保您的存储库从
JpaSpecificationExecutor<JobEntity>
规范示例为:
public class JobEntitySpecifications { public static Specification<JobEntity> withProject(Project project) { if (project == null) { return null; } else { return (root, query, cb) -> cb.equal(root.get("project"), project); } } public static Specification<JobEntity> withJobType() { ... } public static Specification<JobEntity> withJobStatus() { ... } public static Specification<JobEntity> withCreateTime() { ... }}
确保
null在没有给出项目代码/职位类型/ …的情况下返回,以便在查询中将其忽略。
现在您可以使用:
repository.findAll(Specifications.where(JobEntitySpecifications.withJobType(jobType)) .and(JobEntitySpecifications.withJobStatus(jobStatus)) .and(JobEntitySpecifications.withProject(project)) .and(JobEntitySpecifications.withCreateTime(createTime)));
如果在此处使用静态导入,则可以使其看起来更好。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)