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

scalikejdbc 学习笔记(5)

时间:2017-06-25 11:08:41      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:new   conf   commit   apply()   generated   ati   and   ase   common   

常用增删改查操作:

import scalikejdbc._
import scalikejdbc.config._

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

    case class Emp(id: Int, name: String)

    DB autoCommit { implicit session =>
      sql"create table emp ( id int(20) not null AUTO_INCREMENT, name varchar(30),   primary key (id))".execute.apply()
    }

    val id = 1
    val name = "sky"
    val newName = "bill"

    DB localTx { implicit session =>
      sql"""insert into emp (name) values (${name})"""
        .update.apply()
      val idd = sql"insert into emp (name) values (${name})"
        .updateAndReturnGeneratedKey.apply()
      println("new insert: " + idd)
      sql"update emp set name = ${newName} where id = ${id}".update.apply()

      sql"delete emp where id = ${id}".update.apply()

      val emps: List[Emp] = sql"select id, name from emp".map(
        (rs: WrappedResultSet) => Emp(
          id = rs.int("id"),
          name = rs.string("name"))).list.apply()

      for (emp <- emps) {
        println(emp.id + "," + emp.name)
      }
    }

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

  

scalikejdbc 学习笔记(5)

标签:new   conf   commit   apply()   generated   ati   and   ase   common   

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

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