0

我想从多个表中选择并使用 ActiveJDBC 将结果转换为 json

http://javalite.io/record_selection

我可以执行以下操作,但它当然只会返回模型书中的列,我看不到作者模型中的列。怎么可能做到这一点?

LazyList<Book> book = Book.findBySQL("select books.*, authors.* from books, authors where books.author_id = author.id");
jsonOutput = book.toJson(true);
4

1 回答 1

2

首先,您显然具有一对多关联,因此您不需要使用findBySQL(). 此方法用于框架无法提供帮助的情况,但您的情况是标准的。根据这个页面: http: //javalite.io/one_to_many_associations以及这个:http: //javalite.io/generation_of_json 你需要写这样的东西:

LazyList<Author> authors = Author.findAll().include(Book.class);
String json = authors.toJson(true);

或者,您可以在一行中编写相同的内容:

String json = Author.findAll().include(Book.class).toJson(true);

当然,这会将所有作者及其书籍加载到内存中。您可能希望将 替换为Author.findAll()适当Author.where(..)的条件。

我希望这有帮助!

于 2014-10-22T02:12:26.023 回答