use*_*113 8 scala playframework squeryl playframework-2.0
我正在尝试学习如何使用Play和Squeryl创建一个简单的数据库应用程序.我已经从Play教程制作了任务应用程序,但我想更改模型/架构,以便它使用Squeryl而不是Anorm.我一直在寻找不同的教程,示例和答案,但我还没有弄清楚如何做到这一点.
所以,给出Play Tutorial(ScalaTodoList)的源代码; 我如何继续使用Squeryl?
all(),create()以及delete()在我的模型的方法呢?(我想为任务使用自动递增ID)Build.scala和Global.scala(见下文).我怎样才能让它自动使用H2进行开发/测试和Heroku上的Postgres,就像Play教程中的Anorm一样?我已经完成了Play ScalaTodoList教程.
在project/Build.scala,object ApplicationBuild我添加了依赖项:
// From the "Squeryl Getting Started tutorial"
val posgresDriver = "postgresql" % "postgresql" % "8.4-702.jdbc4"
val h2 = "com.h2database" % "h2" % "1.2.127"
// From the "Squeryl Getting Started tutorial"
libraryDependencies ++= Seq(
"org.squeryl" %% "squeryl" % "0.9.5",
h2
)
// From the Play tutorial
val appDependencies = Seq(
// Add your project dependencies here,
"org.squeryl" %% "squeryl" % "0.9.5", // Copied from above so that it compiles (?)
"postgresql" % "postgresql" % "8.4-702.jdbc4"
)
Run Code Online (Sandbox Code Playgroud)
添加app/Global.scala(取自上面提到的SO答案,只是将适配器更改为H2):
import play.db.DB
import play.api.Application
import play.api.GlobalSettings
import org.squeryl._
import org.squeryl.adapters._
object Global extends GlobalSettings {
override def onStart(app: Application): Unit =
{
SessionFactory.concreteFactory = Some(
() => Session.create(DB.getDataSource().getConnection(),
dbAdapter));
}
override def onStop(app: Application): Unit =
{
}
val dbAdapter = new H2Adapter(); // Hard coded. Not good.
}
Run Code Online (Sandbox Code Playgroud)
在app/models/Task.scala我添加的进口和删除ANORM implemetations all(),create()和delete().Play教程中的控制器期望all()返回该方法List[Task].
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Schema
import org.squeryl.annotations.Column
case class Task(id: Long, label: String)
object Task extends Schema {
val tasks = table[Task] // Inspired by Squeryl tutorial
def all(): List[Task] = {
List[Task]() // ??
}
def create(label: String) {
// ??
}
def delete(id: Long) {
// ??
}
}
Run Code Online (Sandbox Code Playgroud)
其余文件保留在Play教程结尾处.
以下是Squeryl的Play 2项目示例:https:
//github.com/jamesward/play2bars/tree/scala-squeryl