gpt4 book ai didi

kotlin-exposed - Kotlin 暴露的批量插入无法按文档工作

转载 作者:行者123 更新时间:2023-12-04 13:40:41 27 4
gpt4 key购买 nike

我正在尝试使用 Kotlin Exposed 批量插入记录到 SQL 表中。我已经按照 Exposed 文档设置了代码,但是,正在执行的 SQL 语句是单独的插入语句,而不是 1 个批量插入语句。

位于此处的文档:https://github.com/JetBrains/Exposed/wiki/DSL
批量插入有以下几点:

批量插入

批量插入允许在一个 sql 语句中将实体列表映射到数据库原始数据。它比一个一个插入更有效,因为它只启动一个语句。下面是一个例子:

    val cityNames = listOf("Paris", "Moscow", "Helsinki")
val allCitiesID = cities.batchInsert(cityNames) { name ->
this[cities.name] = name
}

我的代码如下:

    val mappings: List<Triple<String, String, String>> = listOf(triple1, triple2, triple3)
transaction {
TableName.batchInsert(mappings) {
this[TableName.value1] = it.first
this[TableName.value2] = it.second
this[TableName.value3] = it.third
}
}

我希望看到打印出来的是 1 个批处理插入语句,它遵循以下语法

INSERT INTO TableName (value1, value2, value3) values
(triple1value1, triple1value2, triple1value3),
(triple2value1, triple2value2, triple2value3),
(triple3value1, triple3value2, triple3value3), ...

而是使用以下语法打印 3 个单独的插入语句

INSERT INTO TableName (value1, value2, value3) values (triple1value1, triple1value2, triple1value3)
INSERT INTO TableName (value1, value2, value3) values (triple2value1, triple2value2, triple2value3)
INSERT INTO TableName (value1, value2, value3) values (triple3value1, triple3value2, triple3value3)

由于这似乎是批量插入的记录正确方法,我在这里做错了什么?

最佳答案

文档解释:

NOTE: The batchInsert function will still create multiple INSERTstatements when interacting with your database. You most likely wantto couple this with the rewriteBatchedInserts=true (orrewriteBatchedStatements=true) option of your relevant JDBC driver,which will convert those into a single bulkInsert. You can find thedocumentation for this option for MySQL here and PostgreSQL here.


https://github.com/JetBrains/Exposed/wiki/DSL#batch-insert

关于kotlin-exposed - Kotlin 暴露的批量插入无法按文档工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57612163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com