Play-Java / 2.6 / セットアップ(LinuxMint編)

Play-Java / 2.6 / セットアップ(LinuxMint編)

2021-01-11 Linux Mint 20.1 Mate で確認

Play は独力でビルドテスト実行までもっていけるので、ある意味 IDE と相性が悪い。

sbt のインストール

sbt Reference Manual — Linux への sbt のインストール

公式に従う。

$ echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
$ curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add
$ sudo apt-get update
$ sudo apt-get install sbt
$ sbt --version
sbt version in this project: 1.4.6
sbt script version: 1.4.6

Play-Java 2.6 PJ の作成

ディレクトリを掘る

$ mkdir helloplay
$ cd helloplay

このバージョン x の部分は本当にこのように表記する。

$ sbt new playframework/play-java-seed.g8 --branch=2.6.x

アプリ本体名(今回はディレクトリと同じ helloplay)と作成者のパッケージ(com.example)を聞かれるので入力する。

確認

今回は生成ディレクトリとアプリディレクトリが同じ名前になっているので

$ cd helloplay

つまり helloplay/helloplay ディレクトリにアプリ本体が生成されている。

起動

$ sbt run

起動したら(初回起動はビルド実行環境をまるごと用意するため時間がかかる)、

http://localhost:9000

で確認できる。

Ebean を有効化

build.sbt に追記して Ebean を有効化する。 古い Play では Ebean は同梱されていたが、2.6 版ではプラグイン化されているのでそのやり方に従う。

まず plugins.sbt に記述する。これは pom.xml みたいな設定ファイルとなる

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "6.0.0")

build.sbt でそれを設定する

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

MySQL のコネクタも build.sbt に追記する

libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.22"

application.conf に DB への接続設定を書く

ebean.default="models.*"
play.evolutions.db.default.enabled=false

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/hello?characterEncoding=UTF-8"
db.default.username=root
db.default.password=hogehoge

Ebean の動作確認

DB 作成

CREATE DATABASE hello CHARACTER SET utf8 COLLATE utf8_general_ci;

テーブル作成

USE hello;
CREATE TABLE IF NOT EXISTS `hoge` (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `age` INT(3) NOT NULL,
  `memo` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

データ挿入

INSERT INTO hoge VALUES(1, 'yamada', 20, 'memomemo');

Entity 作成(超手抜き)

@Entity
@Table(name="hoge")
public class Hoge extends Model{
    @Id
    long id;
 
    String name;
 
    public long getId() {
        return id;
    }
 
    public static final Finder<Long, Hoge> find = new Finder<>(Hoge.class);
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

実行用テスト作成

public class HogeTest extends WithApplication {
    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder().build();
    }
    @Test
    public void testFind() {
        List<Hoge> hoge = Hoge.find.all();
        System.out.println(hoge.get(0).getName());
    }
}

こうなる。WithApplication を使うことで Play の環境内での実行を考慮したテストとなる。

あとは sbt とかで実行すれば動くかどうか確かめられる。

java/play_java/26/setup_on_linuxmint/start.txt · 最終更新: 2021-01-11 19:35 by ore