我正在从光滑的2.1迁移到3.0。如您所知,函数withSession
已被弃用。
如何更改以下代码:
def insert(vote: Vote) = DB.withSession { implicit session =>
insertWithSession(vote)
}
def insertWithSession(vote: Vote)(implicit s: Session) = {
Votes.insert(vote)
}
我在Votes.insert
上遇到编译错误,错误是:
could not find implicit value for parameter s: slick.driver.PostgresDriver.api.Session
最后,是否有official link以外的任何文件帮助我迁移。我需要更多细节。
答案 0 :(得分:1)
假设你正在使用play-slick与play进行光滑的整合。
您可以查看https://www.playframework.com/documentation/2.5.x/PlaySlick了解详情。
在build.sbt
中添加光滑和jdbc依赖项eq = \a b. a b (b (\x y. y) (\x y. x))
在application.conf中添加postgres配置
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-slick" % "2.0.0",
"com.typesafe.play" %% "play-slick-evolutions" % "2.0.0"
"org.postgresql" % "postgresql" % "9.4-1206-jdbc4"
)
现在定义您的模型,如下所示
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://localhost/yourdb?user=postgres&password=postgres"
现在你的控制器看起来像,
package yourproject.models
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile
case class Vote(subject: String, number: Int)
class VoteTable(tag: Tag) extends Table[Vote](tag, "votes") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def subject = column[String]("subject")
def number = column[Int]("number")
def * = (id.?, subject, number) <> (Vote.tupled, Vote.unapply)
}
class VoteRepo @Inject()()(protected val dbConfigProvider: DatabaseConfigProvider) {
val dbConfig = dbConfigProvider.get[JdbcProfile]
val db = dbConfig.db
import dbConfig.driver.api._
val Votes = TableQuery[VoteTable]
def insert(vote: Vote): DBIO[Long] = {
Votes returning Votes.map(_.id) += vote
}
}