【发布时间】:2019-02-13 13:16:38
【问题描述】:
这是我第一次发布问题希望有人可以帮助我将结果映射到:
List<Clients>
这是来自数据库的示例结果:
NameID | Name | Notes | Date
1 | Client1 | Get this on monday. | null
1 | Client1 | Get this on wednesday. | null
2 | Client2 | Meet on saturday. | null
这是我的模式(java 类)。
Name.java
private int NameId;
private String ClientName;
.. getter and setter
Notes.java
private int NotesId;
private String ClientNote;
.. getter and setter
Clients.java
private Name name;
private List<Notes> notes;
.. getter and setter
ClientResultSet.java
class ClientResultSet implements ResultSetExtractor<List<Clients>>{
@Override
public List<Clients> extractData(ResultSet rs) throws SQLException, DataAccessException {
Map<int, Clients> map = new HashMap<int, Clients>();
while(rs.next()) {
int NameId= rs.getInt("NameId");
Clients clients= map.get(contactId);
if (clients== null) {
// I'm losing my thoughts here. :'(
}
}
return null;
}
}
我想要达到的结果:
[
{
name:{
NameId: 1,
Name: "Client1"
},
notes[
{
NotesId: 1,
ClientNote: "Get this on monday",
Date: null
},
{
NoteId: 2,
ClientNote: "Get this on wednesday",
Date: null
}
]
},
{
name:{},
notes:[{},{}]
}
... and so on
]
我正在阅读 ResultSetExtractor 但我不知道如何实现它。提前致谢,祝您有美好的一天。
【问题讨论】:
-
你是否在使用任何 ORM,如 hibernate 或 eclipselink?
-
我真的不知道 orm 是什么,但我在 Eclipse-EE 中使用 Spring。
标签: java spring spring-boot jdbc