【发布时间】:2019-08-21 08:11:30
【问题描述】:
我有一个用例,我想显示实体的内容但隐藏某些字段。我的实体如下-
实体
public class StudentDetail {
@Id
private Long ID;
private String firstName;
private String middleName;
private String lastName;
@JsonFormat(pattern="dd-MMM-yyyy", timezone="IST")
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
}
它还有许多其他属性,我在这里没有展示。
存储库-
@Repository
public interface StudentDetailsRepository extends JpaRepository<StudentDetail, Integer> {
@Query("select d from StudentDetail d where month(d.dateOfBirth) = ?1 ")
List<StudentDetail> getStudentListBasedOnDateOfBirth(int month);
}
服务类 -
public List<StudentDetail> getStudentBirthdayDetails(int month) {
List<StudentDetail> StudentDetail = StudentDetailsRepository.getStudentListBasedOnDateOfBirth(month);
return StudentDetail;
}
还有一个控制器类调用带有month参数的Service类来过滤数据集。
我想要做的是修改 Repository 类中的查询,并且只包含 firstname、middleName 和 lastName 属性。 Repository 类应该隐藏dateOfBirth 字段。我意识到以下查询将返回过滤后的项目-
select d.firstName, d.middleName, d.lastName from StudentDetail d where month(d.dateOfBirth) = ?1
但是,Repository 类的返回类型是实体类型 StudentDetail 。仅从中选择几个字段将导致错误。所以,我想知道我应该在repo/service 和controller 类中进行哪些更改(假设只有类的返回类型会改变)?
【问题讨论】:
标签: java spring spring-boot spring-mvc spring-data-jpa