WARNING: This page is a guide for 2.x series.

Play Framework Support

ScalikeJDBC provides two Play plugins to integrate Play2 and ScalikeJDBC. Choose your preferred one.


Migration Guide

Unfortunately, Play 2.4 is basically incompatible with Play plugins. Since Play 2.4, you need to switch to use Play modules instead.

ScalikeJDBC integration with Play 2.0 - 2.3

  • scalikejdbc-play-plugin
  • scalikejdbc-play-dbplugin-adapter
  • scalikejdbc-play-fixture-plugin

ScalikeJDBC integration with Play 2.4 or higher

  • scalikejdbc-play-initializer
  • scalikejdbc-play-dbapi-adapter
  • scalikejdbc-play-fixture

Getting Started with Play 2.4 or higher


scalikejdbc-play-initializer


This module supports connection pooling too.

build.sbt

libraryDependencies ++= Seq(
  "com.h2database"  %  "h2"                           % "1.4.200", // your jdbc driver here
  "org.scalikejdbc" %% "scalikejdbc"                  % "2.5.2",
  "org.scalikejdbc" %% "scalikejdbc-config"           % "2.5.2",
  "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.5.1"
)

conf/application.conf

# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play;DB_CLOSE_DELAY=-1"
# NOTE: sclaikejdbc-config 2.2.6 doesn't support username, use 2.2.7 or higher
db.default.username=sa
db.default.password=sa

# ScalikeJDBC original configuration
#db.default.poolInitialSize=10
#db.default.poolMaxSize=10
#db.default.poolValidationQuery=

scalikejdbc.global.loggingSQLAndTime.enabled=true
scalikejdbc.global.loggingSQLAndTime.singleLineMode=false
scalikejdbc.global.loggingSQLAndTime.logLevel=debug
scalikejdbc.global.loggingSQLAndTime.warningEnabled=true
scalikejdbc.global.loggingSQLAndTime.warningThresholdMillis=5
scalikejdbc.global.loggingSQLAndTime.warningLogLevel=warn

play.modules.enabled += "scalikejdbc.PlayModule"
# scalikejdbc.PlayModule doesn't depend on Play's DBModule
play.modules.disabled += "play.api.db.DBModule"

scalikejdbc-dbapi-adapter


build.sbt

libraryDependencies ++= Seq(
  "com.h2database"  %  "h2"                             % "1.4.200", // your jdbc driver here
  "org.scalikejdbc" %% "scalikejdbc"                    % "2.5.2",
  "org.scalikejdbc" %% "scalikejdbc-config"             % "2.5.2",
  "org.scalikejdbc" %% "scalikejdbc-play-dbapi-adapter" % "2.5.1"
)

conf/application.conf

This plugin is an adapter to connect play.api.db.DBApi and ScalikeJDBC.

# others are same as Play's defaults

play.modules.enabled += "scalikejdbc.PlayDBApiAdapterModule"

Fixtures - scalikejdbc-play-fixture


db.default.fixtures.test=["users.sql", "project.sql", "project_member.sql", "task.sql"]
#db.secondary.fixtures.test=["users.sql", "project.sql", "project_member.sql", "task.sql"]

play.modules.enabled += "scalikejdbc.PlayFixtureModule"

conf/db/fixtures/default/project.sql

# --- !Ups

INSERT INTO project (id, name, folder) VALUES (1, 'Play 2.0', 'Play framework');
INSERT INTO project (id, name, folder) VALUES (2, 'Play 1.2.4', 'Play framework');
INSERT INTO project (id, name, folder) VALUES (3, 'Website', 'Play framework');
INSERT INTO project (id, name, folder) VALUES (4, 'Secret project', 'Zenexity');
INSERT INTO project (id, name, folder) VALUES (5, 'Playmate', 'Zenexity');
INSERT INTO project (id, name, folder) VALUES (6, 'Things to do', 'Personal');
INSERT INTO project (id, name, folder) VALUES (7, 'Play samples', 'Zenexity');
INSERT INTO project (id, name, folder) VALUES (8, 'Private', 'Personal');
INSERT INTO project (id, name, folder) VALUES (9, 'Private', 'Personal');
INSERT INTO project (id, name, folder) VALUES (10, 'Private', 'Personal');
INSERT INTO project (id, name, folder) VALUES (11, 'Private', 'Personal');
ALTER SEQUENCE project_seq RESTART WITH 12;

# --- !Downs
ALTER SEQUENCE project_seq RESTART WITH 1;
DELETE FROM project;

conf/db/fixtures/defaut/project_member.sql

# --- !Ups

INSERT INTO project_member (project_id, user_email) VALUES (1, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (1, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (1, 'sadek@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (1, 'erwan@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (2, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (2, 'erwan@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (3, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (3, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (4, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (4, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (4, 'sadek@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (4, 'erwan@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (5, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (6, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (7, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (7, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (8, 'maxime@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (9, 'guillaume@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (10, 'erwan@sample.com');
INSERT INTO project_member (project_id, user_email) VALUES (11, 'sadek@sample.com');

# --- !Downs

DELETE FROM project_member;

Getting Started with Play 2.0 - 2.3


scalikejdbc-play-plugin


This plugin supports connection pooling too. You don’t need dbplugin and evolutionplugin when using this plugin.

project/Build.scala

libraryDependencies = Seq(
  "org.scalikejdbc" %% "scalikejdbc"                       % "2.3.5",
  "org.scalikejdbc" %% "scalikejdbc-config"                % "2.3.5",
  "org.scalikejdbc" %% "scalikejdbc-play-plugin"           % "2.3.10",
  "org.scalikejdbc" %% "scalikejdbc-play-fixture-plugin"   % "2.3.10", // optional
  // substitute this for whatever DB driver you're using:
  "com.h2database"  %  "h2"                                % "1.4.200"
)

conf/play.plugins

10000:scalikejdbc.PlayPlugin

If you use fixture-plugin too, PlayFixturePlugin should be loaded after PlayPlugin:

10000:scalikejdbc.PlayPlugin
11000:scalikejdbc.PlayFixturePlugin

conf/application.conf

# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.user="sa"
db.default.password="sa"

db.secondary.driver=org.h2.Driver
db.secondary.url="jdbc:h2:mem:play2"
db.secondary.user="sa"
db.secondary.password="sa"

# ScalikeJDBC original configuration

#db.default.poolInitialSize=10
#db.default.poolMaxSize=10
#db.default.poolValidationQuery=

scalikejdbc.global.loggingSQLAndTime.enabled=true
scalikejdbc.global.loggingSQLAndTime.logLevel=debug
scalikejdbc.global.loggingSQLAndTime.warningEnabled=true
scalikejdbc.global.loggingSQLAndTime.warningThresholdMillis=1000
scalikejdbc.global.loggingSQLAndTime.warningLogLevel=warn

#scalikejdbc.play.closeAllOnStop.enabled=true

# You can disable the default DB plugin
# Otherwise, don't disable dbplugin when you use scalikejdbc-play-dbplugin-adapter
dbplugin=disabled
evolutionplugin=disabled

# scalikejdbc logging
logger.scalikejdbc=DEBUG

scalikejdbc-play-dbplugin-adapter


This plugin is an adapter to connect dbplugin and ScalikeJDBC.

project/Build.scala

libraryDependencies = Seq(
  "org.scalikejdbc" %% "scalikejdbc"                       % "4.2.1",
  "org.scalikejdbc" %% "scalikejdbc-config"                % "4.2.1",
  "org.scalikejdbc" %% "scalikejdbc-play-dbplugin-adapter" % "2.5.1",
  "org.scalikejdbc" %% "scalikejdbc-play-fixture-plugin"   % "2.5.1", // optional
  // substitute this for whatever DB driver you're using:
  "com.h2database"  %  "h2"                                % "1.4.200"
)

conf/play.plugins

10000:scalikejdbc.PlayDBPluginAdapter

conf/application.conf

Basically do same as play-plugin.

NOTICE: Don’t disable dbplugin.


Fixtures - PlayFixturePlugin


Fixtures are optional. If you don’t nee, no need to use them.

conf/application.conf

db.default.fixtures.test=[ "project.sql", "project_member.sql" ]

With MySQL, you may need to append allowMultiQueries=true to JDBC url.

db.default.url="jdbc:mysql://localhost/testdb?allowMultiQueries=true"

conf/db/fixtures/default/project.sql

Same as scalikejdbc-play-fixture.

conf/db/fixtures/defaut/project_member.sql

Same as scalikejdbc-play-fixture.


More Examples


Check the code on GitHub: scalikejdbc/hello-scalikejdbc


If this webpage has a typo or something wrong, Please report or fix it. How?