我在我的数据库中有一个任务列表我希望按状态和优先级获取任务,如果我没有传递status和priority,同样的查询应返回相应的结果。我写了一些东西,但这不是好方法
状态和优先级是任务实体的变量
如果有任何想法,请帮助我对spring感到很新。请提前告诉你
服务:
public List<Task> getTasks(String status, String priority, String projectId) {
if (!status.equals("undefined") && !priority.equals("undefined")
&& !projectId.equals("undefined")) {
return taskRepository
.findByStatusAndPriorityAndProjectIdAndIsDelete(status,
priority, projectId, false);
} else if (!(status.equals("undefined"))
&& (priority.equals("undefined"))
&& !(projectId.equals("undefined"))) {
return taskRepository.findByStatusAndProjectIdAndIsDelete(status,
projectId, false);
} else if (!(priority.equals("undefined"))
&& (status.equals("undefined"))
&& !(projectId.equals("undefined"))) {
return taskRepository.findByPriorityAndProjectIdAndIsDelete(
priority, projectId, false);
} else if ((priority.equals("undefined"))
&& (status.equals("undefined"))
&& !(projectId.equals("undefined"))) {
return taskRepository.findByUsersAndProjectIdAndIsDelete(
authUtilService.getLoggedInUser(), projectId, false);
} else {
System.out.println("else called");
return taskRepository.findByUsersAndIsDelete(
authUtilService.getLoggedInUser(), false);
}
}
存储库:
List<Task> findByUsersAndProjectIdAndIsDelete(User loggedInUser, String id,
boolean b);
List<Task> findByUsersAndIsDelete(User loggedInUser, boolean b);
List<Task> findByTitleIgnoreCaseContainingAndIsDelete(String taskName,
boolean b);
List<Task> findByTenant(Tenant loggedInUsersTenant);
List<Task> findByStatusAndPriorityAndProjectIdAndIsDelete(String status,
String priority, String projectid, boolean b);
List<Task> findByPriorityAndProjectIdAndIsDelete(String priority,
String id, boolean b);
List<Task> findByStatusAndProjectIdAndIsDelete(String status,
String projectId, boolean b);
控制器:
@RequestMapping("/gettasks")
@JsonView({ TaskJsonView.Summary.class })
public List<Task> getTasks(@RequestParam("status")String status,@RequestParam("priority")String priority, @RequestParam("projectId") String projectId){
return taskControllerService.getTasks(status,priority,projectId);
}
任务实体:
@Entity
@Table(name = "tasks")
@EntityListeners(TaskEntityListener.class)
public class Task extends AbstractTenantEntity {
private static final long serialVersionUID = 1L;
@Column(name = "task_id", updatable = false)
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
long taskId;
@Column(name = "title")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
private String title;
@Column(name = "status")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
private String status;
@Column(name = "priority")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
private String priority;
@Column(name = "description")
@JsonView({ TaskJsonView.class, ProjectJsonView.class })
String description;
@Column(name = "start_date")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
Date startDate;
@Column(name = "complete_date")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
Date completeDate;
@Column(name = "deadline")
@JsonView({ TaskJsonView.class, ProjectJsonView.class,TaskJsonView.Summary.class })
Date deadline;
@Column(name = "isdelete")
@JsonView({ TaskJsonView.class, ProjectJsonView.class })
boolean isDelete = false;
@OneToMany(mappedBy = "task", fetch = FetchType.LAZY)
@JsonView({ TaskJsonView.class })
List<Comment> comments;
@ManyToMany
@JoinTable(name = "tasks_users", joinColumns = @JoinColumn(name = "task_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
@JsonView({ TaskJsonView.class })
List<User> users;
@ManyToOne(fetch = FetchType.EAGER)
@JsonView({ TaskJsonView.class,TaskJsonView.Summary.class })
Project project;
@OneToMany(mappedBy = "task", fetch = FetchType.EAGER)
@JsonView({ TaskJsonView.class })
List<SubTask> subtasks;
@ManyToMany
@JoinTable(name = "tasks_tags", joinColumns = @JoinColumn(name = "taskid"), inverseJoinColumns = @JoinColumn(name = "tagid"))
@JsonView({ TaskJsonView.class })
List<Tag> tags;
public boolean isDelete() {
return isDelete;
}