【问题标题】:Use SQLBatch with fluent API?将 SQLBatch 与流利的 API 一起使用?
【发布时间】:2021-04-12 15:37:13
【问题描述】:

文档描述了使用 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();

我找不到任何示例。

【问题讨论】:

    标签: java sql jdbc jdbi


    【解决方案1】:

    我不确定 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

    【讨论】:

      猜你喜欢
      • 2011-02-04
      • 2014-10-12
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2011-08-18
      相关资源
      最近更新 更多