How to use SORM framework with Play Framework?

后端 未结 3 1354
清酒与你
清酒与你 2021-01-20 05:20

I find SORM very Interesting and promising but I cant find a way to Integrate It with play any guides?

3条回答
  •  野的像风
    2021-01-20 05:58

    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.

提交回复
热议问题