seratch's weblog in Japanese

About Scala, Java and Ruby programming in Japaense. If you need English information, go to http://blog.seratch.net/

ScalikeJDBC と Anorm の連携

Scala で ScalikeJDBC という JDBC ラッパーを開発しています。

http://ls.implicit.ly/seratch/scalikejdbchttp://github.com/seratch/scalikejdbc

Anorm と連携はできるのですが、以下のようにしないといけないという問題があってイマイチでした。

import anorm._
import anorm.SqlParser._

case class Emp(id: Int, name: Option[String])
val empAllColumns = get[Int]("id") ~ get[Option[String]]("name") map { case id ~ name => Emp(id, name) }

val empList: List[Emp] = DB readOnly { session =>
  implicit val conn: java.sql.Connetion = session.conn
  SQL("select * from " + tableName).as(empAllColumns.*)
}

そこで version 0.5.3 から 〜WithConnection という新しい API をつくりました。こちらを使うと以下のようにスムーズに Anorm を使うことができます。

val empList: List[Emp] = DB readOnlyWithConnection { implicit conn =>
  SQL("select * from " + tableName).as(empAllColumns.*)
}

ScalikeJDBC と Anorm を連携する g8 テンプレートもあるのでこちらもお試しください。

https://github.com/seratch/scalikejdbc-with-anorm20.g8https://github.com/seratch/scalikejdbc-with-anorm20.g8/blob/master/src/main/g8/src/main/scala/$package$/AnormExample.scala