如何使Squeryl与Play配合使用!框架? [英] How to make Squeryl work with the Play! Framework?

查看:126
本文介绍了如何使Squeryl与Play配合使用!框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用Play和Squeryl创建一个简单的数据库应用程序.我已经从Play教程制作了Tasks应用程序,但是我想更改模型/架构,以便它使用Squeryl而不是Anorm.我一直在查看其他教程,示例和

I'm trying to learn how to make a simple database app with Play and Squeryl. I've made the Tasks app from the Play tutorial, but I want to change the model / schema so that it uses Squeryl instead of Anorm. I've been looking at different tutorials, examples and answers, but I haven't really figured out how to do this.

因此,鉴于 Play教程(ScalaTodoList)的源代码;如何继续使其与Squeryl兼容?

So, given the source code from the Play Tutorial (ScalaTodoList); how do I proceed to make it work with Squeryl?

  • 如何在模型中实现all()create()delete()方法? (我想为任务使用自动递增的ID)
  • 当前要使用的数据库适配器在Build.scalaGlobal.scala中进行了硬编码(请参见下文).我如何才能使其像在Play教程中对Anorm一样,在Heroku上自动使用H2进行开发/测试和Postgres?
  • 如何确保它会自动创建我的表?
  • How do I implement the all(), create(), and delete() methods in my model? (I'd like to use auto-incrementing ID's for the Tasks)
  • Which database adapter to use is currently hard coded in Build.scala and Global.scala (see below). How can I make it such that it automatically uses H2 for dev/testing and Postgres on Heroku, like it does for Anorm in the Play tutorial?
  • How do I make sure that it automatically creates my tables?

我已经完成了Play ScalaTodoList教程.

I've completed the Play ScalaTodoList Tutorial.

project/Build.scalaobject ApplicationBuild中,我添加了依赖项:

In project/Build.scala, object ApplicationBuild, I've added the dependencies:

// 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"
)

添加了app/Global.scala(摘自上述 SO答案,只是将适配器更改为H2):

added app/Global.scala (taken from the SO answer mentioned above, just changed the adapter to 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.

  }

app/models/Task.scala中的

中,我添加了导入,并删除了all()create()delete()中的Anorm实现. Play教程中的控制器希望all()方法返回List[Task].

in app/models/Task.scala I've added imports and removed the Anorm implemetations in all(), create(), and delete(). The controller from the Play tutorial expects the all() method to return 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) {
// ??
  }
}

其余文件保留在Play教程结尾处.

The rest of the files are left as they were at the end of the Play tutorial.

推荐答案

以下是使用Squeryl的Play 2项目示例:
https://github.com/jamesward/play2bars/tree/scala-squeryl

Here is an example Play 2 project with Squeryl:
https://github.com/jamesward/play2bars/tree/scala-squeryl

这篇关于如何使Squeryl与Play配合使用!框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆