6

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.

Archaeron
  • 767
  • 1
  • 7
  • 15
  • Can you qualify "doesn't work"? What kinds of unexpected behaviors or exceptions are you seeing? – cmbaxter May 15 '13 at 16:50
  • thank you for answering. the problem was all that boilerplate that @johanandren talks about. I didn't know how to tell slick to use my config file and all that. With the plugin it works now :) – Archaeron May 15 '13 at 20:24

3 Answers3

5

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/

johanandren
  • 11,249
  • 1
  • 25
  • 30
  • thank you very much :) It works great. Now I just have to figure out the details. You're great! – Archaeron May 15 '13 at 20:21
  • am I the only one to be a little concerned by this? https://www.dropbox.com/s/6vakhhlik4msy1o/Screenshot%202015-10-21%2016.18.48.png?dl=0 I find all things Play Framework to be quite poorly documented. – Ashesh Oct 21 '15 at 14:19
2

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/

Chris
  • 2,057
  • 2
  • 16
  • 20
1

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

Christopher Hunt
  • 2,071
  • 1
  • 16
  • 20