SORM的CURD以及事务

4 篇文章 0 订阅
4 篇文章 0 订阅
package scala_mysql_BasicCURD

case class Person(name: String)

import sorm.Dsl._
import sorm._

object db extends Instance(
  entities = Set(Entity[Person]()),
  url = "jdbc:mysql://localhost/testscala",
  user = "root",
  password = "",
  initMode = InitMode.DropAllCreate
)


/**
 * Created by aminic on 2014-11-18.
 */
object Main {
  def main(args: Array[String]) {
    println("OK")

    //create
    val lucy = Person("lucy")
    db.save(lucy)
    println("lucy saved")

    //update
    val resultLucy = db.query[Person].whereEqual("name", "lucy").fetchOne().get
    val maven = resultLucy.copy(name = "maven")
    db.save(maven)
    val lucycount = db.query[Person].whereEqual("name", "lucy").count()
    if (lucycount == 0) {
      println("lucy not exist")
      val resultMaven = db.query[Person].whereEqual("name", "maven").fetchOne().get
      if (resultMaven.name == "maven")
        println("lucy name changed to maven")
      else
        println("maven not exist")
    }
    else {
      println("lucy name don't changed to maven")
    }

    val dev = Person("dev")
    db.save(dev)
    println("dev saved")

    //delete
    val result = db.query[Person].where(
      (("name" equal "lucy") or ("name" equal "maven") or ("name" equal "dev"))
    ).fetch()

    val task = db.transaction {
      for (p <- result)
        db.delete(p)
    }
    println("all deleted")

  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值