21
20

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.

play2.4.3 + slick 3.0備忘録

Posted at
  • dao packageを使う
  • diを使う
  • dbはmysql

の場合、設定備忘録

build.scala
name := "activator-play-slick"

version := "1.0-SNAPSHOT"

scalaVersion := "2.11.7" // or "2.10.4"

libraryDependencies ++= Seq(
  "org.webjars" %% "webjars-play" % "2.3.0-2",
  "com.typesafe.play" %% "play-slick" % "1.1.0",
  "com.typesafe.play" %% "play-slick-evolutions" % "1.1.0",
  "mysql" % "mysql-connector-java" % "5.1.36"
)

fork in Test := false

lazy val root = (project in file(".")).enablePlugins(PlayScala)

routesGenerator := InjectedRoutesGenerator
plugin.sbt
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3")

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
CatDao
package dao

import models.Models.Cat

import scala.concurrent.Future

import javax.inject.Inject

import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfigProvider

import slick.driver.JdbcProfile

import scala.concurrent.ExecutionContext.Implicits.global

class CatDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
  extends HasDatabaseConfigProvider[JdbcProfile] {

  import driver.api._

  private val cats = TableQuery[CatsTable]

  // ddl
  {
    val schema = cats.schema
    db.run(schema.create)
  }

  def all(): Future[Seq[Cat]] = db.run(cats.result)

  def insert(cat: Cat): Future[Unit] = db.run(cats += cat).map { _ => () }

  private class CatsTable(tag: Tag) extends Table[Cat](tag, "CAT") {
    def name  = column[String]("name", O.PrimaryKey)
    def color = column[String]("color")

    def * = (name, color) <> (Cat.tupled, Cat.unapply _)
  }
}
Application.scala
package controllers

import javax.inject.Inject

import play.api.libs.concurrent.Execution.Implicits.defaultContext

import dao.CatDao
import models.Models.Cat

import play.api.data._
import play.api.data.Forms._
import play.api.mvc._

class Application @Inject() (cats: CatDao) extends Controller {
  val catForm = Form(
    mapping(

  "name" -> text(),
      "color" -> text()
    )(Cat.apply)(Cat.unapply)
  )
  def index = Action.async {
    cats.all.map { case cs => Ok(views.html.index(cs)) }
  }

  def insert = Action.async { implicit rs =>
    val cat: Cat = catForm.bindFromRequest.get
    cats.insert(cat).map(_ => Redirect(routes.Application.index))
  }
}
21
20
0

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
21
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?