Adding Slick Support to your Project using PlaySlick

First import your project into an IDE. This tutorial makes use of IntelliJ IDEA . In case of using any other IDE check here for the required steps.

Step 1 : Importing your first Play application into IntelliJ IDEA

  1. Select File > New > Project from Existing Resources 
  2. Choose your newly created Play Project and click Ok 
  3. Select Import project from External Model and choose sbt and click Next .
  4. Select the required JDK( jdk 1.8) and Click Finish.
This completes importing of your first play application into your IDE !! Your Project structure should now look like this


Step 2 : Configuring MySql to your Project

This tutorial makes use of MySQL database for data persistence. So now let’s first add the required dependencies for MySQL. As you know Play application makes use of SBT , the dependencies now should be added in build.sbt file

Your default build file should look like this
name := """MyFirstPlay"""

version := "1.0-SNAPSHOT"

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

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

Now , add the MySql driver dependency to the libraryDependencies list .

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test,
  "mysql" % "mysql-connector-java" % "5.1.34"
)
Instead of using the in-memory MySql DB , let us configure our application to an external MySql DB , as we never get to use in-memory while developing a real time web application .
Add these lines to your application.conf
slick.dbs.default.driver = "slick.driver.MySQLDriver$"
slick.dbs.default.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.default.db.url = "jdbc:mysql://localhost/userdb"
slick.dbs.default.db.user = <username>
slick.dbs.default.db.password = "<password>"


Step 3 : Adding Slick to your project

Slick is an efficient and modern data access library written for Scala.
Play comes with a inbuilt module called Play Slick, which auto integrates Slick into the Play application’s life cycle !
So you need not worry of things like updating the slick to the latest version, managing the database session , database connections etc.
To add Play-slick to your application, add the following dependency:
"com.typesafe.play" %% "play-slick" % "1.1.0"

Play Slick also has support for Evolutions
Evolutions is an effective solutions provided by Play framework to handle all your database migrations . Our application makes use of evolutions to create tables and provide default data(if required).Evolutions are anyway independent of Slick implementation.
In case you need Evolutions support for your play application , then you need to add it to your dependencies list.
You can now choose to add evolutions to your application in two ways :
Independent of Play-Slick :
libraryDependencies += evolutions

Or that comes with play-slick :
"com.typesafe.play" %% "play-slick-evolutions" % "1.1.0"


Additional Step : Removing the default jdbc plugin dependency

The default play application comes with the jdbc dependency, since now we plan to make use of play-slick , the default configured jdbc should be removed .
libraryDependencies ++= Seq(
  jdbc, // jdbc dependency to be removed
...
... 
)
Ignoring this step would throw the following error :
 A binding to play.api.db.DBApi was already configured at play.api.db.slick.evolutions.EvolutionsModule.bindings:
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton).
 at play.api.db.DBModule.bindings(DBModule.scala:25):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider))
So now your final set of dependencies should look like this :
libraryDependencies ++= Seq(
  cache,
  ws,
  specs2 % Test,
  "mysql" % "mysql-connector-java" % "5.1.34",
  "com.typesafe.play" %% "play-slick" % "1.1.0",
  "com.typesafe.play" %% "play-slick-evolutions" % "1.1.0"
)
We are now done with the project build . Just Compile your application to install those dependencies. And its time to move onto the actual code !!

1 comment: