【问题标题】:Retrieve list from database using Spring Boot使用 Spring Boot 从数据库中检索列表
【发布时间】:2018-10-26 15:58:38
【问题描述】:

我有一个名为 findall_db 的数据库和一个名为 person 的表...我已经手动向该表添加了一些值,并希望使用 Spring Boot 检索它们。这就是我所拥有的,但我的控制器中不断出现此错误:Type mismatch: cannot convert from Iterable<PersonInfo> to List<PersonInfo>

实体类是:

@Entity
@Table(name = "Person")
public class PersonInfo implements Serializable {

    private static final long serialVersionUID = 1 L;

    @Id
    @SequenceGenerator(name = "PERSON_GENERATOR", sequenceName = "PERSON_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PERSON_GENERATOR")
    private Long id;

    @Column(name = "ssn")
    private String socialSecurityNumber;

    private String name;

    public PersonInfo() {}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSocialSecurityNumber() {
        return socialSecurityNumber;
    }

    public void setSocialSecurityNumber(String socialSecurityNumber) {
        this.socialSecurityNumber = socialSecurityNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "PersonInfo [id=" + id + ", socialSecurityNumber=" + socialSecurityNumber + ", name=" + name + "]";
    }

}

存储库类是:

@Repository
public interface PersonInfoRepository extends CrudRepository < PersonInfo, Long > {

 }

控制器是:

@Controller
public class PersonController {

    @Autowired
    PersonInfoRepository personRepo;

    @ResponseBody
    @GetMapping("/people")
    public List printPersonInfo() {

        List < PersonInfo > people = personRepo.findAll();

        System.out.println(people.toString());

        return people;
    }
}

【问题讨论】:

    标签: java spring-mvc spring-boot spring-repositories


    【解决方案1】:
    public List<PersonInfo> printPersonInfo() {    
           List<PersonInfo> people = personRepo.findAll()
                                               .stream()
                                               .collect(Collectors.toList());
           System.out.println(people.toString());
           return people ;
    }
    

    【讨论】:

      【解决方案2】:

      这是因为 CrudRepository#findAll 返回的是 Iterable 而不是 List。因此,您要么(1) 更改方法签名以返回Iterable,要么(2) 将元素复制到List 并返回它。

      (1) 返回Iterable:

      public Iterable<PersonInfo> printPersonInfo() {
          return personRepo.findAll();
      }
      

      (2) 将元素复制到List 并返回列表。

      public List<PersonInfo> printPersonInfo() {
          List<PersonInfo> list = new ArrayList<>();
          personRepo.findAll().forEach(list::add);
          return list;
      }
      

      【讨论】:

      • 感谢第一个答案的建议,它有效..非常感谢您
      【解决方案3】:

      使您的存储库扩展 JpaRepository 而不是 CrudRepository

      @Repository
      public interface PersonInfoRepository extends JpaRepository < PersonInfo, Long > {
      
       }
      

      【讨论】:

        【解决方案4】:
        @Autowired
        PersonInfoRepository personRepo;
        
        @ResponseBody
        @GetMapping("/people")
        public List printPersonInfo() {
        
            List < PersonInfo > people = personRepo.findAll();
        
            System.out.println(people.toString());
        
            return people;
        }
        

        在这部分代码中你没有初始化(强制转换更好)你的控制器变量

        您可以在people = 之后使用List&lt;PersonInfo&gt; 轻松投射它

        【讨论】:

          猜你喜欢
          • 2015-07-24
          • 2020-08-07
          • 2016-07-09
          • 1970-01-01
          • 2021-03-03
          • 1970-01-01
          • 2016-05-17
          • 1970-01-01
          • 2015-06-04
          相关资源
          最近更新 更多