gpt4 book ai didi

java - 在 Set 中获取多个连接数据

转载 作者:行者123 更新时间:2023-11-30 08:38:18 25 4
gpt4 key购买 nike

我正在尝试获取专辑详细信息以及与该专辑关联的所有图像以设置 piSet 但它并未获取所有图像以设置 piSet

我喜欢

 albumname   in al object     
ABC
images in piSet
image1
image2
image3
....

数据获取代码是

 con = ConnectionFactory.getConnection();
List<Album> aList = new ArrayList<Album>();
try {
ps = con.prepareStatement("select distinct album.*,pi.* from "
+ "atom as a "
+ "left join album as album "
+ "on "
+ "a.id=album.aid "
+ "left join post_images as pi "
+ "on "
+ "album.id=pi.album_id "
+ "where "
+ "a.id=? "
+ "and a.status=? ");
ps.setLong(1, aid);
ps.setString(2, "Active");
rs = ps.executeQuery();

while (rs.next()) {
Set<PostImages> piSet = new HashSet<PostImages>();
Album al = new Album();
al.setId(rs.getLong("album.id"));
al.setName(rs.getString("album.name"));
PostImages pi = new PostImages();
pi.setImageId(rs.getLong("pi.image_id"));
pi.setLargePicPath(rs.getString("pi.large_pic_path"));

piSet.add(pi);
al.setPostImageses(piSet);
aList.add(al);
}
} catch (SQLException e) {}

如何修改上面的代码以获取所有图像到“PiSet”。

最佳答案

假设idalbum表中是唯一的,上面的代码可以满足你的要求:

rs = ps.executeQuery();
HashMap<Long, Album> AlbumMap = new HashMap<Long, Album>();
while (rs.next()) {
Long albumId = rs.getLong("album.id");//assume album.id is unique.
Album al = AlbumMap.get(albumId);
if (al == null) {
al = new Album();
al.setId(albumId);
al.setName(rs.getString("album.name"));
Set<PostImages> piSet = new HashSet<PostImages>();
al.setPostImageses(piSet);
aList.add(al);
AlbumMap.put(albumId, al);
}
PostImages pi = new PostImages();
pi.setImageId(rs.getLong("pi.image_id"));
pi.setLargePicPath(rs.getString("pi.large_pic_path"));
al.getPostImageses().add(pi);
}

关于java - 在 Set 中获取多个连接数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com