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")
}
}
SORM的CURD以及事务
最新推荐文章于 2024-11-13 15:51:57 发布