//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">
return ok(views.html.(html file name).render();
redirect
return redirect("/item/");
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」のレコードを検索する
}
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