【问题标题】:How to use JdbcTemplate to perform Join queries如何使用 JdbcTemplate 执行 Join 查询
【发布时间】:2013-02-26 08:30:15
【问题描述】:

我有以下数据库模型

       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 连接查询,或者使用 Spring JDBC 模板框架是否有更简单的方法。

【问题讨论】:

    标签: spring jdbctemplate


    【解决方案1】:

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

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

    编辑:

    修改为返回成员的所有日记、页面、cmets。

    【讨论】:

    • 谢谢贝尼。我必须对此进行测试。
    • 我刚刚写了一个很简单的查询-> select d.id , d.creationDate , d.name , d.description , d.viewtype from Member m inner join Diary d on d.member = m .id 其中 m.id="4915018864030990478";这将返回给我一个会员的所有日记。现在每本日记都可以有 n 页,所以如果我对此查询进行多个子内连接,您会怎么想。你的当然看起来不错,但我想知道这是否也是可能的..
    • @SaurabhKumar:修改为处理成员的所有日记。
    • 再次感谢。我可以把这个 FullDiaryRowCallbackHandler 作为一个泛型类。因为我需要在层次结构中做同样的事情,例如我的 pages 、 cmets 等等。我会很高兴制作某种通用代码,否则我必须多次输入相同的代码。
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多