Page History
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// main.scala.html @(title: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")"/> </head> <body> @content </body> </html> // person.scala.html @(person: Form[CreatePersonForm])(implicit request: MessagesRequestHeader) @import helper._ @request.flash.get("success").map { key => @request.messages(key) } @main("Welcome to Play") { @form(routes.PersonController.addPerson()) { @inputText(person("name")) @inputText(person("age")) @CSRF.formField <div class="buttons"> <input type="submit" value="Add Person"/> </div> } } |
Model
ORM 과 SQL쿼리방식의 개발 차이는 여기에있습니다. 쿼리 방식에서는 해당 모델을 SQL문으로 한번더 맵핑처리를하여야하며
쿼리를 사용해야합니다. ORM에서는 엔티티와 모델이 일치함으로 추가적인 쿼리개발이 필요가 없습니다.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
//person.scala // package models import play.api.libs.json._ case class Person(id: Long, name: String, age: Int) object Person { implicit val personFormat = Json.format[Person] } // personRepository.scala // package models import javax.inject.{ Inject, Singleton } import play.api.db.slick.DatabaseConfigProvider import slick.jdbc.JdbcProfile import scala.concurrent.{ Future, ExecutionContext } /** * A repository for people. * * @param dbConfigProvider The Play db config provider. Play will inject this for you. */ @Singleton class PersonRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) { // We want the JdbcProfile for this provider private val dbConfig = dbConfigProvider.get[JdbcProfile] // These imports are important, the first one brings db into scope, which will let you do the actual db operations. // The second one brings the Slick DSL into scope, which lets you define the table and other queries. import dbConfig._ import profile.api._ /** * Here we define the table. It will have a name of people */ private class PeopleTable(tag: Tag) extends Table[Person](tag, "people") { /** The ID column, which is the primary key, and auto incremented */ def id = column[Long]("id", O.PrimaryKey, O.AutoInc) /** The name column */ def name = column[String]("name") /** The age column */ def age = column[Int]("age") /** * This is the tables default "projection". * * It defines how the columns are converted to and from the Person object. * * In this case, we are simply passing the id, name and page parameters to the Person case classes * apply and unapply methods. */ def * = (id, name, age) <> ((Person.apply _).tupled, Person.unapply) } /** * The starting point for all queries on the people table. */ private val people = TableQuery[PeopleTable] /** * Create a person with the given name and age. * * This is an asynchronous operation, it will return a future of the created person, which can be used to obtain the * id for that person. */ def create(name: String, age: Int): Future[Person] = db.run { // We create a projection of just the name and age columns, since we're not inserting a value for the id column (people.map(p => (p.name, p.age)) // Now define it to return the id, because we want to know what id was generated for the person returning people.map(_.id) // And we define a transformation for the returned value, which combines our original parameters with the // returned id into ((nameAge, id) => Person(id, nameAge._1, nameAge._2)) // And finally, insert the person into the database ) += (name, age) } /** * List all the people in the database. */ def list(): Future[Seq[Person]] = db.run { people.result } } |
...