gpt4 book ai didi

java - ActiveJDBC 从多个表中选择

转载 作者:行者123 更新时间:2023-12-01 12:23:49 25 4
gpt4 key购买 nike

我想从多个表中进行选择并使用 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);

最佳答案

首先,您显然具有一对多关联,因此您不需要使用 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(..)

希望这会有所帮助!

关于java - ActiveJDBC 从多个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26497283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com