(Reference) Books you are studying https://www.amazon.co.jp/Play-Framework-2%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-Java%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E3%82%A2%E3%82%B8%E3%83%A3%E3%82%A4%E3%83%ABWeb%E9%96%8B%E7%99%BA-%E6%B4%A5%E8%80%B6%E4%B9%83/dp/4798133922/ref=cm_cr_srp_d_product_top?ie=UTF8
What is a database in the first place? See below. https://www.sejuku.net/blog/8763#i-4 Database design is very important when creating an application.
The play framework incorporates a database program called H2 (H2 Data Base Engine).
First, allow the application to use the DB. The setting method is as follows.
Setting item = value
・ Comments starting with # ・ Add the following code
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"
Explanation including the parts that have not been added.
db.default.driver
→ Specify the driver to be used for database access. Here, Java classes are generally specified.
db.default.url
→ Specify the URL that indicates the database access destination. Specify in the form of "jdbc. 〇〇".
db.default.user
→ Not used in H2, but used when using a client-server database. Enter the user name.
db.default.password
→ Not used in H2, but used when using a client-server database. Enter the password.
ebean.default
→ Describe the contents related to the O / R mapper.
There are two main patterns for calling the database. -How to write in the driver file → Files to access the database http://www.atmarkit.co.jp/ait/articles/0106/26/news001.html
-How to use the build tool → A tool that automatically activates the application, including access to the driver (What is a build) https://qiita.com/Mura-Mi/items/225825cc3715dc04d923
** The Play Framework uses a build tool called "sbt" to download the tools needed to access the database. ** **
Describe the items used in the application after seq, separated by commas, as shown below.
libraryDependencies ++= Seq(Item 1,Item 2 ...)
name := "PlayApp"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache
)
play.Project.playJavaSettings
In the play application, by defining the concrete table contents in the Model class The table can be prepared automatically at the time of execution.
Create a folder called models in a folder called apps. Create a file called Message.java in the folder.
public class class name extends Model{
//Definition of fields to store
}
package models;
import java.util.Date;
import javax.persistence.*;
import javax.validation.*;
import play.data.validation.*;
import play.db.ebean.*;
@Entity//①
public class Message extends Model{//②
@ID//③
public Long id;
public String name;
public String mail;
public String message;
public Date postdate;
}
(1) Describe that it is an entity class. (Reference for entity classes) http://itdoc.hitachi.co.jp/manuals/link/cosmi_v0870/APKC/EU070254.HTM (2) By inheriting the class called Model, it is possible to retain the function as a database. ③ The field below @ID is treated as the primary key.
Application.java
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
//Action when accessing the route
public static Result index(){
return ok(index.render("Database sample"));
}
}
index.scala.html
@(msg:String)
@main("Sample page") {
<h1>Hello!</h1>
<p>@msg</p>
}
Recommended Posts