7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SlickでのSession周り

Last updated at Posted at 2014-02-20

こんなのがあった場合、

Model.scala
import play.api.db.DB
import play.api.Play.current
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc._
import scala.language.postfixOps

case class Model(id:Int, name:String)

object Models extends Table[Model]("models") {

  def id      = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name", O.NotNull, O DBType "varchar(100)")
  def * = id ~ name <> (Model.apply _, Model.unapply _)
  
  /** DBコネクション */
  val database = Database.forDataSource(DB.getDataSource())
 
  /** 登録 */
  def create(e: Model) = database.withTransaction { implicit session: Session =>
    Models.insert(e)
  }
}

Database.threadLocalSessionをimportするとimplicit ....がいらないらしい

Model.scala
import play.api.db.DB
import play.api.Play.current
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc._
import Database.threadLocalSession
import scala.language.postfixOps

case class Model(id:Int, name:String)

object Models extends Table[Model]("models") {

  def id      = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name", O.NotNull, O DBType "varchar(100)")
  def * = id ~ name <> (Model.apply _, Model.unapply _)
  
  /** DBコネクション */
  val database = Database.forDataSource(DB.getDataSource())
 
  /** 登録 */
  def create(e: Model) = database.withTransaction {
    Models.insert(e)
  }
}
7
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?