java JDBC 模板 - 一对多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16461735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC Template - One-To-Many
提问by EdgeCase
I have a class that looks like this. I need to populate it from two database tables, which are also shown below. Is there any preferred way to do this?
我有一个看起来像这样的课程。我需要从两个数据库表中填充它,这也如下所示。有没有首选的方法来做到这一点?
My thought is to have a service class to select a List<>
via a ResultSetExtractor
from a DAO. Then do a foreach
on that list, and select a List<>
of emails for the individual person via another ResultSetExtractor
, and attach it from with the foreach
loop.
我的想法是有一个服务类来从 DAO 中选择一个List<>
via a ResultSetExtractor
。然后foreach
在该列表上执行一个,并List<>
通过另一个 为个人选择一封电子邮件ResultSetExtractor
,并从foreach
循环中附加它。
Is there a better way, or is this as good as it gets?
有没有更好的方法,或者这已经达到了最好的效果?
public class Person {
private String personId;
private String Name;
private ArrayList<String> emails;
}
create table Person (
person_id varchar2(10),
name varchar2(30)
);
create table email (
person_id varchar2(10),
email varchar2(30)
);
回答by JB Nizet
This is best solved by an ORM. With JDBC, you have to do by hand what an ORM would do for you. Executing N + 1 queries is very inefficient. You should execute a single query, and build your objects manually. Cumbersome, but not hard:
这最好由 ORM 解决。使用 JDBC,您必须手动完成 ORM 将为您完成的工作。执行 N+1 次查询的效率非常低。您应该执行单个查询,并手动构建您的对象。繁琐但不难:
select person.id, person.name, email.email from person person
left join email on person.id = email.person_id
...
Map<Long, Person> personsById = new HashMap<>();
while (rs.next()) {
Long id = rs.getLong("id");
String name = rs.getString("name");
String email = rs.getString("email");
Person person = personsById.get(id);
if (person == null) {
person = new Person(id, name);
personsById.put(person.getId(), person);
}
person.addEmail(email);
}
Collection<Person> persons = personsById.values();
回答by landbit
I was looking for something similar, and although the answer is perfectly valid I went with this nice library instead https://simpleflatmapper.org/0203-joins.html
我正在寻找类似的东西,虽然答案完全有效,但我还是选择了这个不错的库https://simpleflatmapper.org/0203-joins.html
It also integrates perfectly with Spring boot.
它还与 Spring boot 完美集成。
main advantage is that you have a clean repository layer, it uses your pojo and makes refactoring much easier, and like hibernate you can still map deep nested and complex one to many and still be in control of what is executed.
主要优点是你有一个干净的存储库层,它使用你的 pojo 并使重构更容易,并且像休眠一样,你仍然可以映射深层嵌套和复杂的一对多,并且仍然可以控制执行的内容。
It also has a nice jdbctemplate CRUD and Java 13 finally brings support for multi-line string literals which is very good for sql statements readability. hope this helps someone :)
它还有一个很好的 jdbctemplate CRUD,Java 13 终于支持多行字符串文字,这对 sql 语句的可读性非常好。希望这对某人有所帮助:)