zoukankan      html  css  js  c++  java
  • scalikejdbc 学习笔记(5)

    常用增删改查操作:

    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()
      }
    }
    

      

  • 相关阅读:
    剑指offer 把字符串转换成整数 python
    剑指offer 重建二叉树 python
    LeetCode 82 删除排序链表中的重复元素 II python
    LeetCode 142 环形链表 II python
    hashlib
    configparser
    正则
    logging
    模块
    文件操作
  • 原文地址:https://www.cnblogs.com/AK47Sonic/p/7076216.html
Copyright © 2011-2022 走看看