zoukankan      html  css  js  c++  java
  • 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()
      }
    }
    

      

  • 相关阅读:
    常用内建函数
    函数作用域
    异常处理语句
    迭代器---待延申扩展
    流程控制语句
    字典
    集合
    数据类型的可变与不可变
    Openstack keystone组件详解
    云计算openstack介绍(001)
  • 原文地址:https://www.cnblogs.com/AK47Sonic/p/7076211.html
Copyright © 2011-2022 走看看