使用 Spring Boot 从数据库中检索列表

我有一个名为的数据库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;

    }

}


阿晨1998
浏览 260回答 3
3回答

慕标琳琳

那是因为CrudRepository#findAll返回 anIterable而不是 a List。因此,您要么(1)更改方法签名以返回 anIterable或者您 (2)将元素复制到 aList并返回它。(1) 返回Iterable:public Iterable<PersonInfo> printPersonInfo() {&nbsp; &nbsp; return personRepo.findAll();}(2) 将元素复制到 aList并返回列表。public List<PersonInfo> printPersonInfo() {&nbsp; &nbsp; List<PersonInfo> list = new ArrayList<>();&nbsp; &nbsp; personRepo.findAll().forEach(list::add);&nbsp; &nbsp; return list;}

喵喵时光机

@AutowiredPersonInfoRepository personRepo;@ResponseBody@GetMapping("/people")public List printPersonInfo() {&nbsp; &nbsp; List < PersonInfo > people = personRepo.findAll();&nbsp; &nbsp; System.out.println(people.toString());&nbsp; &nbsp; return people;}在这部分代码中,您没有初始化(强制转换更好)您的控制器变量你可以很容易地用List<PersonInfo>afterpeople =

红颜莎娜

public List<PersonInfo> printPersonInfo() {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;List<PersonInfo> people = personRepo.findAll()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(people.toString());&nbsp; &nbsp; &nbsp; &nbsp;return people ;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java