问题描述
文档描述了使用 sql 对象 API 中的 sqlBatch 批注进行批处理:
http://jdbi.org/jdbi2/sql_object_api_batching/
@sqlBatch("insert into something (id,name) values (:id,:first || ' ' || :last)")
void insertFamily(@Bind("id") List<Integer> ids,@Bind("first") Iterator<String> firstNames,@Bind("last") String lastName);
是否也可以使用 fluent API 执行批量 sql 插入?
String name = h.createquery("select name from something where id = :id")
.bind("id",1)
.map(StringColumnMapper.INSTANCE)
.first();
我找不到任何示例。
解决方法
我不确定 JDBI v2,但在 v3 中它是在 docs 中完成的:
PreparedBatch batch = handle.prepareBatch("INSERT INTO user(id,name) VALUES(:id,:name)");
for (int i = 100; i < 5000; i++) {
batch.bind("id",i).bind("name","User:" + i).add();
}
int[] counts = batch.execute();
我对 v2 代码库不是很熟悉,但 SqlObject 插件通常只是在底层使用通常的流畅 API,所以很可能也有某种 PreparedBatch
。