我想运行Spring 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>??
答案 0 :(得分:1)
您将不得不使用另一种方法执行更改,您将无法更改由您的extends CrudRepository<Student, Integer>
答案 1 :(得分:0)
您可以做到。仅在以下一个字段中创建StudentD: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.