首页 > 代码库 > scalikejdbc 学习笔记(4)

scalikejdbc 学习笔记(4)

Batch 操作

import scalikejdbc._
import scalikejdbc.config._

object BatchOperation {
  def main(args: Array[String]): Unit = {
    DBsWithEnv("dev").setupAll()

    case class User(id: Int, name: String, age: Int)
    val ls = List[User](
      User(5, "aa", 2),
      User(6, "bb", 8),
      User(7, "cc", 9))

    val lsi = List[User](
      User(5, "aau", 2),
      User(6, "bbu", 8),
      User(7, "ccu", 9))

    val batchInsertParams = for (el <- ls) yield (List(el.id, el.name, el.age))


    val batchUpdateParams = for (el <- lsi) yield (List(el.name, el.id))


    DB localTx { implicit session =>

      sql"insert into user (id, name, age) values (?, ?, ?)".batch(batchInsertParams: _*).apply()
    }

    DB localTx { implicit session =>

      sql"update user set name = ? where id = ?".batch(batchUpdateParams: _*).apply()
    }

    DBsWithEnv("dev").closeAll()
  }
}

  

scalikejdbc 学习笔记(4)