Java 使用springboot从数据库检索列表
我有一个名为Java 使用springboot从数据库检索列表,java,spring-mvc,spring-boot,spring-repositories,Java,Spring Mvc,Spring Boot,Spring Repositories,我有一个名为findall_db的数据库,其中一个表名为person。。。我已经手动将一些值添加到此表中,并希望使用SpringBoot检索它们。这就是我所拥有的,但我的控制器中不断出现此错误:类型不匹配:无法从Iterable转换为List 实体类是: @Entity @Table(name = "Person") public class PersonInfo implements Serializable { private static final long serialVer
findall_db
的数据库,其中一个表名为person
。。。我已经手动将一些值添加到此表中,并希望使用SpringBoot检索它们。这就是我所拥有的,但我的控制器中不断出现此错误:类型不匹配:无法从Iterable转换为List
实体类是:
@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 > {
}
@存储库
公共界面PersonInfoRepository扩展了crudepository{
}
控制器为:
@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;
}
}
@控制器
公共类个人控制器{
@自动连线
PersonInfo存储库personRepo;
@应答器
@GetMapping(“/people”)
公共列表printPersonInfo(){
Listpeople=personRepo.findAll();
System.out.println(people.toString());
还人,;
}
}
让您的存储库扩展,而不是
@存储库
公共界面PersonInfo存储库扩展了JpaRepository{
}
这是因为返回的是,而不是。因此,您可以(1)更改方法签名以同时返回Iterable
,或者(2)将元素复制到列表中并返回它
(1) 返回Iterable
:
public Iterable<PersonInfo> printPersonInfo() {
return personRepo.findAll();
}
@Autowired
PersonInfo存储库personRepo;
@应答器
@GetMapping(“/people”)
公共列表printPersonInfo(){
Listpeople=personRepo.findAll();
System.out.println(people.toString());
还人,;
}
在这部分代码中,您没有初始化(最好是强制转换)控制器变量
您可以在people=
公共列表printPersonInfo()之后使用List
轻松地强制转换它{
public List<PersonInfo> printPersonInfo() {
List<PersonInfo> people = personRepo.findAll()
.stream()
.collect(Collectors.toList());
System.out.println(people.toString());
return people ;
}
List people=personRepo.findAll()
.stream()
.collect(Collectors.toList());
System.out.println(people.toString());
还人,;
}
谢谢第一个答案提出的建议,效果很好。非常感谢
public List<PersonInfo> printPersonInfo() {
List<PersonInfo> list = new ArrayList<>();
personRepo.findAll().forEach(list::add);
return list;
}
@Autowired
PersonInfoRepository personRepo;
@ResponseBody
@GetMapping("/people")
public List printPersonInfo() {
List < PersonInfo > people = personRepo.findAll();
System.out.println(people.toString());
return people;
}
public List<PersonInfo> printPersonInfo() {
List<PersonInfo> people = personRepo.findAll()
.stream()
.collect(Collectors.toList());
System.out.println(people.toString());
return people ;
}