How to use SORM framework with Play Framework?

大城市里の小女人 提交于 2019-12-01 22:27:37
Nikita Volkov
  1. Install Play >= 2.1.0.
  2. Generate a project using Play's guides
  3. Add appropriate SORM's and chosen database's dependencies to the generated project/Build.scala, e.g.:

    val appDependencies = Seq(
      "org.sorm-framework" % "sorm" % "0.3.8",
      "com.h2database" % "h2" % "1.3.168"
    )
    
  4. In the same file make sure that your project depends on the same Scala version, on which SORM depends (for SORM 0.3.8 it's Scala 2.10.1):

    val main = play.Project(appName, appVersion, appDependencies).settings(
      scalaVersion := "2.10.1"
    )
    

    If you miss that step, you may bump into this issue.

  5. In app/models/package.scala place all your case classes and SORM's instance declaration, e.g.:

    package models
    
    case class A( name : String )
    case class B( name : String )
    
    import sorm._
    object Db extends Instance(
      entities = Set(Entity[A](), Entity[B]()),
      url = "jdbc:h2:mem:test"
    )
    

    Note that there is no requirement to follow these naming and location conventions - e.g., you can put your SORM instances in your controllers or elsewhere if you want.

  6. In app/controllers/Application.scala place some controller actions utilizing SORM, e.g.:

    package controllers
    
    import play.api.mvc._
    import models._
    
    object Application extends Controller {
    
      def index = Action {
        val user = Db.save(A("test"))
        Ok(user.id.toString)
      }
    
    }
    

    This will print out a generated id of the saved A case class value.

  7. Run your server using play run or play start command.

oguzhan

Play was updated to use a new build file format

Ref : Build.scala is not created in play

You can continue from build.sbt file anymore

ForEx:

libraryDependencies ++= Seq(
  jdbc,
  cache,
  "org.sorm-framework" % "sorm" % "0.3.8",
  ws,
  specs2 % Test
)

For new usage :

Using SORM with Play Framework 2.3.8

libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.sorm-framework" % "sorm" % "0.3.22",
"com.h2database" % "h2" % "1.3.168",
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!