JavaMySQL一对多连接,将带有JDBI的连接映射到列表
我正试图用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);
如果有人能为我指出正确的方向,我将不胜感激
谢谢
# 1 楼答案
看来你需要@UseRowReducer
您的示例的实现如下所示: