Java 如何使用jdbi获取批插入的生成密钥?
所以我尝试使用JDBI库将许多对象插入数据库。 我需要获取插入的生成ID,因为我需要保存子对象集合。 我使用的代码如下:Java 如何使用jdbi获取批插入的生成密钥?,java,jdbi,Java,Jdbi,所以我尝试使用JDBI库将许多对象插入数据库。 我需要获取插入的生成ID,因为我需要保存子对象集合。 我使用的代码如下: public void batchInsert(List<Feature> features) { jdbi.useHandle(handle -> { PreparedBatch batch = handle.prepareBatch("insert into test.features (type) values (:t
public void batchInsert(List<Feature> features) {
jdbi.useHandle(handle -> {
PreparedBatch batch = handle.prepareBatch("insert into test.features (type) values (:type)");
for (Feature f : features) {
batch
.bindBean(f)
.add();
}
batch.executeAndReturnGeneratedKeys().mapToBean(Feature.class).forEach(System.out::println);
});
}
但是在属性
和几何体
中有数据
那么,如何将源集合项与生成的键相匹配呢
Feature(id=27, type=Feature, properties=null, geometry=null)
Feature(id=28, type=Feature, properties=null, geometry=null)
Feature(id=29, type=Feature, properties=null, geometry=null)
Feature(id=30, type=Feature, properties=null, geometry=null)
....