Created
March 8, 2012 03:06
-
-
Save seratch/1998313 to your computer and use it in GitHub Desktop.
Using Anorm 2.0 without Play Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.9.1" | |
resolvers += "typesafe" at "http://repo.typesafe.com/typesafe/releases" | |
libraryDependencies ++= Seq( | |
"play" %% "anorm" % "2.0-RC4", | |
"com.github.seratch" %% "scalikejdbc" % "[0.5,)", | |
"org.hsqldb" % "hsqldb" % "[2,)" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection | |
import scalikejdbc._ | |
import anorm._ | |
/** | |
* @see "https://github.com/playframework/Play20/wiki/ScalaAnorm" | |
* @see "https://github.com/seratch/scalikejdbc" | |
*/ | |
object WithScalikeJDBC extends App { | |
Class.forName("org.hsqldb.jdbc.JDBCDriver") | |
ConnectionPool.singleton("jdbc:hsqldb:mem:hsqldb:WithAnorm", "", "") | |
implicit val conn: Connection = ConnectionPool.borrow() | |
SQL("create table emp (id integer primary key, name varchar(30))").execute() | |
SQL("insert into emp (id, name) values ({id}, {name})").on("id" -> 1, "name" -> "name1").executeUpdate() | |
SQL("insert into emp (id, name) values ({id}, {name})").on("id" -> 2, "name" -> "name2").executeUpdate() | |
val selectAll: SqlQuery = SQL("select id, name from emp") | |
val all: Stream[SqlRow] = selectAll.apply | |
all.foreach { | |
row => println(row) | |
} | |
val selectAll2: SqlQuery = SQL("select * from emp") | |
selectAll2() foreach println | |
case class Emp(id: Int, name: String) | |
val res: Stream[Emp] = SQL("select id,name from emp")().collect { | |
case Row(id: Int, Some(name: String)) => Emp(id, name) | |
} | |
res foreach println | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use https://github.com/giabao/play-jdbc-standalone