改变弹簧的findAll()返回类型

问题描述 投票:0回答:2

我想运行春天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>??
spring interface spring-data-jpa override spring-projections
2个回答
1
投票

你将不得不执行另一种方法的改变,你不能改变它是由您定义的库返回类型:extends CrudRepository<Student, Integer>


0
投票

你能行的。只有创造一个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.
© www.soinside.com 2019 - 2024. All rights reserved.