如何使用JdbcTemplate执行Join查询

时间:2013-02-26 08:30:15

标签: spring jdbctemplate

我有以下数据库模型

       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模板框架有更简单的方法。

1 个答案:

答案 0 :(得分:3)

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

现在,不是进行多个查询(每个页面一个),而是使用外部联接进行单个查询,以连接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

编辑:

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