[JAVA] play framework personal notes

Initial setting

** application.conf file **

//Add the following to db(Initially it is h2, so rewrite it)
  default.driver = org.postgresql.Driver
  default.url = "jdbc:postgresql://localhost:5433/projectName"
  default.username = postgres
  default.password = postgres

//Add below to the last line
ebean.default="models.*"

plugins.sbt

//Add the following to the last line
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

//Also, since the following is initially set as a comment, delete the comment
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.2")

build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayJava)To
lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)To

//Add the following to the last line
libraryDependencies += "org.postgresql" % "postgresql" % "42.0.0.jre7"
EclipseKeys.preTasks := Seq(compile in Compile)
EclipseKeys.projectFlavor := EclipseProjectFlavor.Java
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)

cache

Controller.java


//DI
@Inject
private CacheApi cache;

//Set cache (can set non-string objects)
cache.set("key", item);
//Set the cache with an expiration date of only 15 minutes
cache.set("key", item, 60 * 15);
//Get cache
Item item = (Item) cache.get("key");
//Delete cache
cache.remove("key");

session Note that only String can be entered

Controller.java


//Set the value in session
session("email", "[email protected]");
//Get the value set in session
String user = session("email");
//Clear the value of session
session().remove("email");

index.html


<!--How to retrieve values in html-->
@session.get("email");

<!--Embedded in hidden-->
<input type="hidden" value="@session.get("id")" id = "idInHidden">

rendering

return ok(views.html.(html file name).render();

redirect

return redirect("/item/");

** Validation **

RegisterForm.java


@Required(message = "Enter your email")
private String email;

index.html


@if(form.error("email") != null) {
      <span style="color: red">
        @form.error("email").message()
       </span>
}

finder When using finder, add the following to the model class

Item.java


package models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "items")
public class Item extends Model {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  private String name;
  private String brand;
  private Integer category;
  private double price;

  public static Finder<Integer, Item> finder = new Finder<Integer, Item>(Integer.class, Item.class);

  //Getter below, setter
}

ItemService.java


public List<Item> getBrandByGrandChildId(String grandChildId){
  Integer id = Integer.parseInt(grandChildId);
  List<Item> brandList = Item.finder.select("brand").where().eq("category", id).ne("brand", null).setDistinct(true).findList();
  return brandList;

//eq(field,value);    「field =value」のレコードを検索する
//ne(field,value);    「field !=value」のレコードを検索する
//lt(field,value);    「field <value」のレコードを検索する
//gt(field,value);    「field >value」のレコードを検索する
//le(field,value);    「field <=value」のレコードを検索する
//ge(field,value);    「field>=value」のレコードを検索する
//ilike(field,value); 「field likevalue」のレコードを検索する
}

** Raw SQL **

itemService.java


public Item findById(Integer id) {
  String sql = "select * from items where a.id = ?";
  SqlRow sqlRow = Ebean.createSqlQuery(sql).setParameter(1, id).findUnique();
  return sqlRow
}

Recommended Posts

play framework personal notes
First Play Framework personal notes
Play Framework study
DDD personal notes
Play Framework2.5 (Java) Tips
[Personal memo] About Spring framework
Play Framework Study Memo Database ①
Java Collections Framework Review Notes
apache POI personal notes crossfish21
Introduced Dozer to Play Framework
Validation function in Play Framework
Play Framework Study Memo Database ②Read
Play Framework 2.6 (Java) development environment creation
Play Framework Study Momo DB Update
Double submit measures with Play Framework
Personal play that no one gets
Sort in List, for personal notes
Play framework scheduled jobs and crontab
How to install Play Framework 2.6 for Mac
[Personal notes] Frequently used IntelliJ IDEA shortcuts
Authentication function in Play Framework [Access restrictions]
How to use BootStrap with Play Framework
Play Framework 2.6 (Java) environment construction in Eclipse
Spring Framework study notes [Part 1] DI container
Authentication function with Play Framework [Registration and authentication]
Play Framework studying value passing, form helper
Rails validation and null: false Personal notes