Create a MySQL connection in Playframework with slick

主宰稳场 提交于 2019-12-04 12:00:09

问题


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.


回答1:


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/




回答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/




回答3:


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



来源:https://stackoverflow.com/questions/16569050/create-a-mysql-connection-in-playframework-with-slick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!