gpt4 book ai didi

spring - 如何使用JdbcTemplate执行Join查询

转载 作者:行者123 更新时间:2023-12-03 03:18:02 34 4
gpt4 key购买 nike

我有以下数据库模型

       create table Diary (id bigint NOT NULL AUTO_INCREMENT,
creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name varchar(255) not null,
description text,
viewtype varchar(255) not null,
member bigint,
primary key (id),
foreign key (member) references Member(id));


create table Page (id bigint NOT NULL AUTO_INCREMENT,
creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
viewtype varchar(255) not null,
diary bigint,
member bigint,
primary key (id),
foreign key (diary) references Diary(id),
foreign key (member) references Member(id));

create table Comment (id bigint NOT NULL AUTO_INCREMENT,
postingDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
comment text not null,
page bigint,
member bigint,
primary key (id),
foreign key (page) references Page(id)
foreign key (member) references Member(id));

我正在使用 Spring JDBC 模板。

      My interface looks like follows: accountid is the memeberid in the database.

Collection<Diary> getDiaries(Long accountId);

我的日记如下所示:

           public class Diary {
private Collection<Page> pages;
private Long id;
private LocalTime creationDate;
private String name;
private String description;
private ViewType type;
}

如果我想使用 jdbc 模板准备日记对象,我想知道查询现在会是什么样子。也可以只触发一个查询并准备这个 Diary 对象,因为我将避免同一请求多次触发查询。对于上面的接口(interface),我很可能会使用 s join 查询,或者是否有使用 spring JDBC 模板框架的更简单的方法。

最佳答案

您将能够使用外连接创建单个查询(我在这里假设日记可能没有任何页面并且页面没有评论)。

现在,不再执行多个查询(每页一个),而是使用外部联接来连接 DiaryPageComment。如下所示,这意味着日记和页面信息会被多次返回,但我认为在多次数据库调用和一些冗余信息之间存在权衡。

 class FullDiaryRowCallbackHandler implements RowCallbackHandler {
private Collection<Diary> diaries = new ArrayList<Diary>();
private Diary currentDiary = null;
private Page currentPage = null;

public void processRow(ResultSet rs) {
long diaryId = rs.getLong("d.id");
if (currentDiary == null || diaryId != currentDiary.getId()) {
currentDiary = new Diary();
currentPage = null;
diaries.add(currentDiary);
currentDiary.setId(diaryId);
currentDiary.setCreationDate(toLocalTime(rs.getTimestamp("d.creationDate")));
currentDiary.setDescription(rs.getString("d.description"));
...
}
long pageId = rs.getLong("p.id");
if (!rs.wasNull() && currentPage != null && currentPage.getId() != pageId) {
currentPage = new Page();
if (currentDiary.getPages() == null) {
currentDiary.setPages(new ArrayList<Page>());
}
currentDiary.getPages().add(currentPage);
currentPage.setId(pageId);
currentPage.setCreationDate(toLocalTime(rs.getTimestamp("p.creationDate")));
...
}
long commentId = rs.getLong("c.id");
if (!rs.wasNull() && currentPage != null) {
Comment comment = new Comment();
if (currentPage.getComments() == null) {
currentPage.setComments(new ArrayList<Comment>());
}
currentPage.getComments().add(comment);
comment.setId(commentId);
comment.setPostingDate(toLocalTime(rs.getTimestamp("c.postingDate")));
comment.setComment(rs.getString("c.comment"));
}
}

public Collection<Diary> getDiaries() {
return diaries;
}
}

FullDiaryRowCallbackHandler rowCallbackHandler = new FullDiaryRowCallbackHandler();
Collection<Diary> result = jdbcTemplate.query(
"select d.id, " +
"d.creationDate, " +
"d.description, " +
"p.id, " +
"p.creationDate, " +
"c.id, " +
"c.postingDate, " +
"c.comment " +
"from Diary d " +
"left outer join Page p on d.id = p.diary " +
"left outer join Comment c on p.id = c.page " +
"where d.member = ? " +
"order by d.id, p.id, c.id",
rowCallbackHandler,
myMemberId);
Collection<Diary> diariesForMember = rowCallbackHandler.getDiaries();

请注意,代码不是特别漂亮,因为您必须处理结果集并注意返回新页面的时间(这就是 order by 子句很重要的原因),但是这就是 Hibernate 之类的东西在急切地获取数据时会在幕后为您提供帮助(我并不是说 Hibernate 更好或不好,因为我喜欢使用 JdbcTemplate 来控制它对我的查询提供的控制)我发布了,但是 Hibernate(或 JPA)在填充对象图时做了很多繁重的工作)。

另请参阅JdbcTemplate#query

编辑:

修改为返回成员(member)的所有日记、页面、评论。

关于spring - 如何使用JdbcTemplate执行Join查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084516/

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