我正在尝试使用JDBI编写对象查询,该对象查询将从左表中获取一整行并将右表中的所有匹配行作为一个列表连接(一个作者可以有很多书)。
作者id, name, createdAt, updatedAt, email, phoneNumber
书id, title, authorId, categories, createdAt, updatedAt
我要创建的最终对象的结构如下:
class AuthorWithBooks() {
int id,
String name,
List<String> categories,
long createdAt,
long updatedAt,
String email,
String phoneNumber
List<Book> books
}
书在哪里:
class Book {
id,
title,
authorId,
categories,
createdAt,
updatedAt
}
这是我正在尝试的查询(不按原样抓取图书清单)
@SqlQuery("SELECT " + AUTHOR_COLUMN MAMES + ", " + BOOK_COLUMN_NAMES + " FROM authors as author" +
" LEFT JOIN books AS book" +
" ON author.id = book.authorId" +
" WHERE id = :authorId")
List<AuthorWithBooks> getAuthorWithBooks(@Bind("authorId") int authorId);
非常感谢您的帮助/有人向我指出正确的方向!
谢谢!
最佳答案
似乎您需要@UseRowReducer
您的示例的实现如下所示:
@SqlQuery("SELECT a." + AUTHOR_COLUMN MAMES + ", b." + BOOK_COLUMN_NAMES + " FROM authors as author" +
" LEFT JOIN books AS book" +
" ON author.id = book.authorId" +
" WHERE id = :authorId")
@RegisterBeanMapper(value = Book.class, prefix = "b")
@RegisterBeanMapper(value = AuthorWithBooks.class, prefix = "a")
@UseRowReducer(AuthorBookReducer.class)
List<AuthorWithBooks> getAuthorWithBooks(@Bind("authorId") int authorId);
class AuthorBookReducer implements LinkedHashMapRowReducer<Integer, Author> {
@Override
public void accumulate(Map<Integer, Author> map, RowView rowView) {
Author author = map.computeIfAbsent(rowView.getColumn("a_id", Integer.class),
id -> rowView.getRow(Author.class));
if (rowView.getColumn("b_id", Integer.class) != null) {
author.getBooks().add(rowView.getRow(Book.class));
}
}
}
关于java - MySql-一对多联接,将带有JDBI的JOIN映射到列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54202685/