Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a MySQL connection in Playframework with slick

I'm trying to connect to a mysql database with slick 1.0.0.

What I've done so far:

in Build.scala I've added

val appDependencies = Seq(
    anorm,
    "mysql" % "mysql-connector-java" % "5.1.24",
    "com.typesafe.slick" % "slick_2.10" % "1.0.0",
    "org.slf4j" % "slf4j-nop" % "1.6.4"
)

in application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="url to mysql db"
db.default.user=user
db.default.pass=password

and now I'm trying to read an Entry from the DB. For this I have a model

package models

import scala.slick.driver.MySQLDriver.simple._
import Database.threadLocalSession

object Organisations extends Table[(Int, String)]("Organisation")
{
    def id = column[Int]("id", O.PrimaryKey)
    def name = column[String]("name")
    def * = id ~ name
}

and now I'd like to just output the entries

val orgs =
    for { o <- Organisations } yield o.name

println("Length" + orgs.toString())

But it doesn't work. I'm sure I've made plenty of errors, but there don't seem to be andy slick tutorials with mysql.

Thank you for your patience and I hope my explanations are clear.

like image 612
Archaeron Avatar asked May 15 '13 15:05

Archaeron


3 Answers

Using Slick requires a bit of boilerplate, creating session and all that, checkout the Play-Slick plugin written by Fredrik Ekholdt (typesafe)!

It does all that plumbing for you and there are good examples on the wiki on how to use it.

https://github.com/freekh/play-slick/

like image 164
johanandren Avatar answered Oct 21 '22 10:10

johanandren


The new Slick 2.0 also features a Code Generator that can be used together with Play Framework evolutions.

This means you don't have to write the boilerplate for Slick anymore. Just write your database changes using evolutions files, and immediately access the new tables from your code.

You can find a complete example using MySQL here:

https://github.com/papauschek/play-slick-evolutions

And more information on how it works:

http://blog.papauschek.com/2013/12/slick-2-0-code-generator-play-framework-evolutions/

like image 44
Chris Avatar answered Oct 21 '22 10:10

Chris


The Play team have also been working on a slick benchmark for Techempower. It is a work in progress but we'll shortly be raising a PR on the completed version (next 24 hours I suspect):

https://github.com/nraychaudhuri/FrameworkBenchmarks/tree/adding_missing_slickness/play-slick

like image 1
Christopher Hunt Avatar answered Oct 21 '22 08:10

Christopher Hunt