English | 简体中文

api-docs / org.ktorm.support.mysql / bulkInsert

bulkInsert

fun <T : BaseTable<*>> Database.bulkInsert(
    table: T,
    block: BulkInsertStatementBuilder<T>.() -> Unit
): Int
(source code)

Construct a bulk insert expression in the given closure, then execute it and return the effected row count.

The usage is almost the same as batchInsert, but this function is implemented by generating a special SQL
using MySQL’s bulk insert syntax, instead of based on JDBC batch operations. For this reason, its performance
is much better than batchInsert.

The generated SQL is like: insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)....

Usage:

database.bulkInsert(Employees) {
item {
set(it.name, "jerry")
set(it.job, "trainee")
set(it.managerId, 1)
set(it.hireDate, LocalDate.now())
set(it.salary, 50)
set(it.departmentId, 1)
}
item {
set(it.name, "linda")
set(it.job, "assistant")
set(it.managerId, 3)
set(it.hireDate, LocalDate.now())
set(it.salary, 100)
set(it.departmentId, 2)
}
}

Parameters

table - the table to be inserted.

block - the DSL block, extension function of BulkInsertStatementBuilder, used to construct the expression.

Since
2.7

Return
the effected row count.

See Also

batchInsert

fun <T : BaseTable<*>> T.bulkInsert(
    block: BulkInsertStatementBuilder<T>.() -> Unit
): Int
(source code)

Construct a bulk insert expression in the given closure, then execute it and return the effected row count.

The usage is almost the same as batchInsert, but this function is implemented by generating a special SQL
using MySQL’s bulk insert syntax, instead of based on JDBC batch operations. For this reason, its performance
is much better than batchInsert.

The generated SQL is like: insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)....

Usage:

Employees.bulkInsert {
item {
set(it.name, "jerry")
set(it.job, "trainee")
set(it.managerId, 1)
set(it.hireDate, LocalDate.now())
set(it.salary, 50)
set(it.departmentId, 1)
}
item {
set(it.name, "linda")
set(it.job, "assistant")
set(it.managerId, 3)
set(it.hireDate, LocalDate.now())
set(it.salary, 100)
set(it.departmentId, 2)
}
}

Parameters

block - the DSL block, extension function of BulkInsertStatementBuilder, used to construct the expression.

Return
the effected row count.

See Also

batchInsert