用“id”和“name”显示成功。我试图显示与用户数据有一对多关系的地址数据。但是,当呈现与javascript有一对多关系的数据时,我都没有成功。请帮帮我,我会感激你的。我有这样的数据
user.sql address
id name address_id id city
1 John 1 1 Washington
2 Matt 2 2 California
用户.java
private long id;
private String name;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "address_id")
private Address address;
//constructor
// getter and setter
地址.java
private long id;
private String city;
@JsonManagedReference
@OneToMany(mappedBy = "address", cascade = CascadeType.ALL)
private Set<User> users= new HashSet<>();
//constructor
//getter and setter
restcontroller.java文件
@GetMapping("/user")
public ResponseEntity<List<User>> getCompanyList() {
return new ResponseEntity<List<User>>(userService.findAll(), HttpStatus.OK);
}
@GetMapping("/address")
public ResponseEntity<List<Address>> getCompanyLists() {
return new ResponseEntity<List<Address>>(addressService.findAll(), HttpStatus.OK);
}
编码javascript以显示数据
$(document).ready(function() {
$.getJSON('http://localhost:5588/user', function(json) {
var tr=[];
for (var i = 0; i < json.length; i++) {
tr.push('<tr>');
tr.push('<td>' + json[i].id + '</td>');
tr.push('<td>' + json[i].name+ '</td>');
(.............................);
tr.push('<td><button class=\'edit\'>Edit</button> <button class=\'delete\'
id=' + json[i].id + '>Delete</button></td>');
tr.push('</tr>');
}
$('table').append($(tr.join('')));
});
});
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Id</th>
<th>Name</th>
<th>City</th>
</tr>
</table>
我不知道如何写代码来显示“城市”的“名称”,但像上面的数据表(代码(……………………)是我想写代码,显示“城市”的权利,我尝试了许多拼写方法,但都是错误的。请帮帮我,谢谢
暂无答案!
目前还没有任何答案,快来回答吧!