This is a project to enable using ScalikeJDBC in Play Framework apps seamlessly.
libraryDependencies ++= Seq(
"com.h2database" % "h2" % "2.2.224", // your jdbc driver here
"org.scalikejdbc" %% "scalikejdbc" % "4.3.0",
"org.scalikejdbc" %% "scalikejdbc-config" % "4.3.0",
"org.scalikejdbc" %% "scalikejdbc-play-initializer" % "3.0.0-scalikejdbc-4.3"
)
# 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.loggingSQLErrors=true
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"
Now you can access ScalikeJDBC everywhere in your Play app!
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import scalikejdbc._
@Singleton
class Application @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
implicit val session: DBSession = AutoSession
def index = Action {
val accounts = {
try sql"select * from accounts".toMap.list.apply()
catch { case e: Exception =>
sql"create table accounts(name varchar(100) not null)".execute.apply()
Seq("Alice", "Bob", "Chris").foreach { name =>
sql"insert into accounts values ($name)".update.apply()
}
sql"select * from accounts".toMap.list.apply()
}
}
Ok(accounts.toString)
}
}
Unfortunately, Play 2.4 is basically incompatible with Play plugins. Since Play 2.4, you need to switch to use Play modules instead.
- scalikejdbc-play-plugin
- scalikejdbc-play-dbplugin-adapter
- scalikejdbc-play-fixture-plugin
- scalikejdbc-play-initializer
- scalikejdbc-play-dbapi-adapter
- scalikejdbc-play-fixture
Copyright ScalikeJDBC committers
Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.html