LoginSignup
1
0

More than 5 years have passed since last update.

play framework個人メモ

Last updated at Posted at 2018-01-19

初期設定

application.confファイル

//dbに以下を追加(初期はh2になっているのでそこを書き換える)
  default.driver = org.postgresql.Driver
  default.url = "jdbc:postgresql://localhost:5433/projectName"
  default.username = postgres
  default.password = postgres

//最後の行に下を追加
ebean.default="models.*"

plugins.sbt

//一番最後の行に下記を追加
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

//また、初期では以下がコメントに設定されているのでコメントを消す
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.2")

build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayJava)
lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)にする

//最後の行に以下を追加
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;

// キャッシュをセット(文字列じゃないオブジェクトをセットできる)
cache.set("key", item);
// 15分間だけ有効期限をつけてキャッシュをセット
cache.set("key", item, 60 * 15);
// キャッシュを取得
Item item = (Item) cache.get("key");
// キャッシュを削除
cache.remove("key");

session

Stringしか入れられない点に注意

Controller.java
// sessionに値をセット
session("email", "user@user.co.jp");
// sessionにセットした値を取得
String user = session("email");
// sessionの値をクリア
session().remove("email");
index.html
<!-- htmlでの値の取り出し方 -->
@session.get("email");

<!-- hiddenに埋め込み -->
<input type="hidden" value="@session.get("id")" id = "idInHidden">

レンダリング

return ok(views.html.(htmlのファイル名).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

finderを使う場合はmodelクラスに以下を追加

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, 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(フィールド, 値);    「フィールド = 値」のレコードを検索する
//ne(フィールド, 値);    「フィールド != 値」のレコードを検索する
//lt(フィールド, 値);    「フィールド < 値」のレコードを検索する
//gt(フィールド, 値);    「フィールド > 値」のレコードを検索する
//le(フィールド, 値);    「フィールド <= 値」のレコードを検索する
//ge(フィールド, 値);    「フィールド>= 値」のレコードを検索する
//ilike(フィールド, 値); 「フィールド like 値」のレコードを検索する
}

生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
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0