繁体   English   中英

如何使用JdbcTemplate执行Join查询

[英]How to use JdbcTemplate to perform Join queries

我有以下数据库模型

       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对象,因为我会避免同一个请求多次触发查询。 对于上面的接口,我很可能会使用s join命令,或者使用spring JDBC模板框架有更简单的方法。

您将能够使用外部联接创建单个查询(我在这里假设可以有一个没有任何页面的日记,并且页面没有注释)。

现在,不是进行多个查询(每个页面一个),而是使用外部联接来连接DiaryPageComment 正如您在下面看到的,这将意味着日志和页面信息被多次返回,但我认为在进行多个DB调用和一些冗余信息之间存在权衡。

 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

编辑:

修改后返回会员的所有日记,页面和评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM