码迷,mamicode.com
首页 > 数据库 > 详细

scalikejdbc 学习笔记(4)

时间:2017-06-25 11:05:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:import   cal   rgs   操作   user   app   apply()   lis   operation   

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)

标签:import   cal   rgs   操作   user   app   apply()   lis   operation   

原文地址:http://www.cnblogs.com/AK47Sonic/p/7076211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!