Modelの作成
2015年一発目はGAEのModelを作成してみます。ControllerとViewを作成した時のようにbuild.xmlを使用してAntで作成していくみたいす。
build.xml右クリック > 「Ant Build...」
いつものように、ダイアログが表示されるのでModel名を入力
今回は「MyData」と入力
すると「パッケージ/model」と「パッケージ/meta」の配下にそれぞれ
MyData.javaとMyDataMeta.javaが作成される。
※metaに関してはAndroidのgenっぽい?
↓作成されたMyData.java
@Model(schemaVersion = 1)
public class MyData implements Serializable {
    private static final long serialVersionUID = 1L;
    @Attribute(primaryKey = true)
    private Key key;
    @Attribute(version = true)
    private Long version;
    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }
    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }
    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }
    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MyData other = (MyData) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
クラスに@Modelのアノテーションが付いている。Seasarっぽい?
slim3が用意したアノテーションでこのクラスがModelであることを宣言している。
Keyはプライマリーキー、これもアノテーションで宣言している。
VersionはこのModelのバージョンをGAE側が管理する為に必要らしい。
このMyData.javaに必要なフィールドを追加してみる。
@Model(schemaVersion = 1)
public class MyData implements Serializable {
    private static final long serialVersionUID = 1L;
    @Attribute(primaryKey = true)
    private Key key;
    @Attribute(version = true)
    private Long version;
    /* 名前           */
    private String name;
    /* メールアドレス   */
    private String mail;
    /* 年齢           */
    private int age;
    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }
    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }
    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }
    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }
    /**
     * 名前の取得
     * 
     * @return  名前
     */
    public String getName() {
        return name;
    }
    /**
     * 名前の設定
     * 
     * @param name  名前
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * メールアドレスの取得
     * 
     * @return      メールアドレス
     */
    public String getMail() {
        return mail;
    }
    /**
     * メールアドレスの設定
     * 
     * @param mail  メールアドレス
     */
    public void setMail(String mail) {
        this.mail = mail;
    }
    /**
     * 年齢の取得
     * 
     * @return      年齢
     */
    public int getAge() {
        return age;
    }
    /**
     * 年齢の設定
     * 
     * @param age   年齢
     */
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MyData other = (MyData) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
この状態でビルドするとMyDataMeta.javaも自動で更新されているっぽい。
んで、このModelを使う場合。
↓Controller側で使用してみる。
public class IndexController extends Controller {
    @Override
    public Navigation run() throws Exception {
        String msg = "データの入力";
        if (isPost()) {
            String name = asString("name");
            String mail = asString("mail");
            int age = asInteger("age");
            MyData myData = new MyData();
            myData.setName(name);
            myData.setMail(mail);
            myData.setAge(age);
            // slim3 エンティティをデータストアに保存
            Datastore.put(myData);
        }
        // slim3 エンティティ一覧の取得
        List<MyData> myDatas = Datastore.query(MyDataMeta.get()).asList();
        request.setAttribute("msg", msg);
        request.setAttribute("mydatas", myDatas);
        return forward("index.jsp");
    }
}
GAE側で用意されているDataStoreをslim3がラップしたDatastoreクラスを使用する。
Datastore#put : insert
Datastore#query() : slim3側で用意されているModelQueryオブジェクトを生成する
ModelQuery#asListメソッドでListへ変換している。
↓index.jsp
<body>
<div id="header">
<h1>テスト</h1>
</div>
<p>${msg}</p>
<form method="post" action="/">
<table>
<tr><td>名前</td><td><input type="text" ${f:text("name")}></td></tr>
<tr><td>メール</td><td><input type="text" ${f:text("mail")}></td></tr>
<tr><td>年齢</td><td><input type="text" ${f:text("age")}></td></tr>
<tr><td></td><td><input type="submit"></td></tr>
</table>
</form>
<hr>
<table border="1">
<c:forEach var="mydata" items="${mydatas}">
<tr>
<td>${mydata.name}</td>
<td>${mydata.mail}</td>
<td>${mydata.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
c:forEachでリストをループし、表示している。
↓実行結果
データストアに登録してみる
ちなみに、ローカルのDBの内容参照や編集は
 http://localhost:8888/_ah/admin
から参照/編集ができる。





 
0 件のコメント:
コメントを投稿