Play2.6 導入方法
install†
java†
JAVA_HOME と PATH で javac が使えるようにしておくこと
download†
ebean 版
https://developer.lightbend.com/start/?group=play&project=play-java-ebean-example
rename†
mv play-java-ebean-example play26
build.sbt
name := "play26"
eclipse plugin†
project/plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")
./sbt eclipse
初回sbt は時間がかかるので気長に待つ
eclipse から既存プロジェクトの import
postgres†
build.sbt
libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41"
psql -U postgres -c "create database play26"
conf/application.conf
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/play26"
db.default.username="postgres"
db.default.password="pass"
.bashrc
export SBT_OPTS="-Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M"
./sbt run
http://localhost:9000
velocity†
scala の html template が慣れないので velocity を導入
build.sbt
libraryDependencies += "org.apache.velocity" % "velocity" % "1.7"
libraryDependencies += "velocity-tools" % "velocity-tools" % "1.4"
./sbt eclipse
conf/templates/default/hello.html
hello $name
app/controllers/HomeController.java
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public HomeController (){
Velocity.setProperty("file.resource.loader.path", "conf/templates/");
Velocity.init();
}
public Result hello(){
StringWriter writer = new StringWriter();
Template template = Velocity.getTemplate("default/hello.html", "UTF-8");
VelocityContext vc = new VelocityContext();
vc .put("name", "scott");
template.merge(vc, writer);
String result = writer.toString();
return Results.ok(result).as("text/html; charset=UTF-8");
}
conf/routes
GET /hello controllers.HomeController.hello()
localhost:9000/hello
ebean†
ebean の ORM (model を書き換えると自動的に DB のテーブルを書き換える )を活性化する
これをやるとDBの中身が消えるので注意
evolutions/default/1.sql
一行目に追加
# --- Created by Ebean DDL
ベストプラクティス的なもの†
- models 以下にあるclass (@Entity付き)のフィールドを追加すると自動で変わる
- ある程度開発が終わったら、自動生成をやめる(先ほどの1行目を消す)
- 2.sql, 3.sql ... を作って自分で DDL 作成する