【发布时间】:2015-12-29 13:56:20
【问题描述】:
我有一组对象,我很想将它们作为批次插入。 一开始我想到的是“for-loops”,但后来我意识到这应该可以用流来实现。
BatchBindStep userLoginBatch = create
.batch(
create.insertInto(USERLOGIN, USERLOGIN.USERNAME, USERLOGIN.IP, USERLOGIN.MAC, USERLOGIN.LOGIN, USERLOGIN.STATUS, USERLOGIN.APPLICATION, USERLOGIN.ENTERTAINMENT_CREDENTIALS_ID, USERLOGIN.VERSION)
.values(null, null, null, (Timestamp) null, null, null, (Integer) null, null)
);
userLoginsToPersist
.stream()
.map(login ->
Arrays.asList(login.getUsername(), login.getIp(), login.getMac(), login.getLogin(), login.getStatus(), login.getApplication(), login.getEntertainmentCredentialsId(), login.getVersion())
).reduce(userLoginBatch, (a, b) -> a.bind(b));
userLoginBatch.execute();
这是我目前拥有的,它抱怨我无法减少那个对象......
【问题讨论】:
-
我从未使用过 jOOQ,但从您的源代码中猜测,也许您可以将 reduce 调用替换为
forEach(b -> userLoginBatch.bind(b));