LoginSignup
1
1

More than 5 years have passed since last update.

PlayFlamework2.6でslickをコンパイル時DIしたときのメモ

Posted at

依存性の追加

build.sbt
libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-slick" % "3.0.0",
  "mysql" % "mysql-connector-java" % "5.1.16",
  "com.typesafe.play" %% "play-slick-evolutions" % "3.0.0"
)

プロパティの定義

application.conf
play.application.loader = "com.exsample.MyApplicationLoader"

slick {
    dbs.default {
        profile = "slick.jdbc.MySQLProfile$"
        db {
            connectionPool = disabled
            driver = "com.mysql.jdbc.Driver"
            url = "jdbc:mysql://localhost/database_name?characterEncoding=UTF-8"
            user = "user"
            password = "password"
        }
    }
}

アプリケーションローダー

MyApplicationLoader.scala
package com.exsample

import play.api._
import play.api.ApplicationLoader.Context
import _root_.controllers.AssetsComponents
import com.exsample.controllers._
import com.exsample.repositories._
import play.filters.HttpFiltersComponents
import slick.jdbc.JdbcBackend.Database
import router.Routes

class MyApplicationLoader extends ApplicationLoader {
  def load(context: Context) = {
    new MyApplicationComponents(context).application
  }
}

class MyApplicationComponents(context: Context)
    extends BuiltInComponentsFromContext(context) with AssetsComponents
      with HttpFiltersComponents {

  // SlickController
  lazy val db = Database.forConfig("slick.dbs.default.db")
  lazy val slickRepository = new SlickRepositoryImpl(db)
  lazy val slickController = new SlickController(controllerComponents, slickRepository)

  // router
  lazy val router = new Routes(httpErrorHandler, slickController, assets)
  // ついでのメモ
  // ↑のnew Routesについて、第一引数はhttpErrorHandler、それ以降はroutesでの定義順に渡すっぽい
}
1
1
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
1
1