Play Framework Study Memo Database ②Read

(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

About search method

In the play framework, the search function is grouped in a class called Model.Finder. The basic usage is to create an instance in Model and retrieve it as needed.

About Finder class

How to set up a Finder instance

new Finder<Primary key,entity>(Primary key.class,entity.class);

Save the created instance in the static field, and retrieve the entity with that method if necessary.

How to set the toString class

	public String toString() {
		return ("[id:"+id+",name:"+name+",mail:"+mail+",message:"+message+",date:"+postdate+"]");
	}

Override the toString method of the object class for clarity (Reference about object class) https://docs.oracle.com/javase/jp/7/api/java/lang/Object.html

Whole Model class

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;

	public static Finder<Long,Message>find = new Finder<Long,Message>(Long.class,Massage.class);

	@Override
	public String toString() {
		return ("[id:"+id+",name:"+name+",mail:"+mail+",message:"+message+",date:"+postdate+"]");
	}

}

About Controller class

Describe the settings so that the entity can be retrieved from the controller class.

Description method

List<entity>Variable = Finder instance.all();

A Java List is an ordered collection that can contain duplicate elements. (What is Reference List) https://eng-entrance.com/java-array-list#List

Items to add to the Application class

List<Message>datas=Message.find.all();

You are calling the all method of the find instance. The all method returns all the stored methods in a List.

Whole Application class

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {
    //Action when accessing the route
    public static Result index(){
      List<Message>datas=Message.find.all();
      return ok(index.render("Database sample"));
    }
}

About index.scala.html

Change the received list so that it can be output as it is.

whole index.scala.html

@(msg:String,datas:List[Message])

@main("Sample page") {
  <h1>Hello!</h1>
  <p>@msg</p>
  <pre>@datas</pre>
}

Recommended Posts

Play Framework Study Memo Database ②Read
Play Framework Study Memo Database ①
Play Framework study
spring framework Simple study memo (2): AOP
Play Framework Study Momo DB Update
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
Play Framework2.5 (Java) Tips
[Java ~ Method ~] Study memo (5)
Dot installation study memo 01
[Java ~ Array ~] Study memo 4
Play Framework studying test
play framework personal notes
[Personal memo] About Spring framework
Ruby study memo (conditional branching)
Spring Framework self-study memo series_1
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Introduction to JUnit (study memo)
First Play Framework personal notes
Java study memo 2 with Progate
Introduced Dozer to Play Framework
Ruby study memo (recursive function)
Validation function in Play Framework
[Ruby ~ Iterative processing ~] Study memo 4