我想运行春天JPA的的findAll()方法,只是返回的所有记录,但随后没有返回的所有记录。我想从一个投影返回值。喜欢
public interface StudentRepository extends CrudRepository<Student, Integer>{
//How can I achieve this
List<NamesOnly> findAll();
//Which returns list of only student names;
}
public interface NamesOnly{
@Value("#{target.firstName+ ' ' + target.lastName}")
String getFullName();
}
或替代地,
how to convert List<Student> given by default findAll() to List<NamesOnly>??
你将不得不执行另一种方法的改变,你不能改变它是由您定义的库返回类型:extends CrudRepository<Student, Integer>
你能行的。只有创造一个StudentDto领域:studentName和StudentRepository做到这一点:
List<StudentDto> findAllStudent();
StudentDTO.class
public class StudentDTO {
private String studentName //this is field you want return
}
或者你可以使用注解@JsonIgnore上述领域你不想秀时效应初探眼线:
**Student.class**
@JsonIgnore
private String firstName //this is field you don't want spring reponse.