DBFluteを使ってみた。
DBFluteは前から名前は知っていたが、S2Daoで満足しているのであんまり気にとめてなかった。
が、カンファレンス行った人の多くのブログで好評なのでいよいよ使ってみた。
0.概要を知る
ドキュメントを読んでみたが、完全に位置づけが理解できていない。
S2Daoで利用するDaoやEntitを自動生成する機能のみと思っていた。
つまり、DBFluteはソースの生成のみでDBアクセスはあくまでもS2Daoのみで行うもの
と思っていたが、ドキュメントを見るとどうやら違うっぽい?
DBアクセスはDBFluteのクラスを利用して行うのか?
まあ、この辺は今後理解していくとして今回は
S2Daoの“外だしSQL”(SQLファイル)から対応するEntityを自動生成する
機能を試してみよう。以下手順。
1.JRE1.5インストール
1.5でないと動かないそうなので、インストールしてパス通す。
2.Antインストール
インストールしてパス通す。パスを反映させるためここで再起動。
4.環境変数の設定
目的の機能を利用するには、このディレクトリの中のファイルを
少し修正するだけでいけるみたい。
dbflute-0.4.7\etc\client_directory-template\otherORMapper
まず、このなかの「_project.bat」のDBFLUTE_HOME・MY_PROJECT_NAMEを設定
@echo off sset MY_PROJECT_NAME=otherORMapper set DBFLUTE_HOME=D:\work\java\dbflute-0.4.7
5.データベース接続情報の設定
次に「build-otherORMapper.properties」を修正。(情報は適当にしてます)
torque.database.driver = oracle.jdbc.driver.OracleDriver torque.database.url = jdbc:oracle:thin:@210.168.11.13:1521:TEST torque.database.schema = TEST torque.database.user = TEST torque.database.password = TEST
6.Sql2Entityの設定
SQLファイルが存在するディレクトリを指定。
これもファイルは、「build-otherORMapper.properties」を修正。
ディレクトリの区切り文字はWindowsでも「/」で区切る。
torque.sql2EntityDefinitionMap = map:{ \ ; sqlDirectory = C:/eclipse/workspace/SeasarSample/WEB-INF/src/sample/dao \ ; isPlainEntity = true \ ; outputDirectory = ./output/src \ ; packageString = sample.entity \ }
8.実行
sql2entity.batを起動するだけ。
9.結果
指定のフォルダ配下にファイルが2つできた。
exentity.customize.TestDbflute.java
package exentity.customize; /** * The entity of TestDbflute. * * @author DBFlute(AutoGenerator) */ public class TestDbflute extends sample.entity.BsTestDbflute { /** Serial version UID. (Default) */ private static final long serialVersionUID = 1L; }
これは普通のエンティティ?。プロパティがないけど。
sample.entity.BsTestDbflute.java
package sample.entity; /** * The entity of TestDbflute. * * @author DBFlute(AutoGenerator) */ public class BsTestDbflute implements java.io.Serializable { /** Serial version UID. (Default) */ private static final long serialVersionUID = 1L; // ===================================================================================== // Attribute // ========= /** The value of testNum. */ protected String _testNum; // ===================================================================================== // Accessor // ======== /** * Get the value of testNum. * * @return The value of testNum. (Nullable) */ public String getTestNum() { return _testNum; } /** * Set the value of testNum. * * @param testNum The value of testNum. (Nullable) */ public void setTestNum(String testNum) { _testNum = testNum; } // ===================================================================================== // Basic Override // ============== /** * This method overrides the method that is declared at super. * If the all-column value of the other is same as this one, returns true. * * @param other Other entity. (Nullable) * @return Comparing result. If other is null, returns false. */ public boolean equals(Object other) { if (other == null) { return false; } if (!(other instanceof BsTestDbflute)) { return false; } final BsTestDbflute otherEntity = (BsTestDbflute)other; if (getTestNum() == null || !getTestNum().equals(otherEntity.getTestNum())) { return false; } return true; } /** * This method overrides the method that is declared at super. * Calculates hash-code from all-columns. * * @return Hash-code from all-columns. */ public int hashCode() { int result = 0; if (this.getTestNum() != null) { result = result + this.getTestNum().hashCode(); } return result; } /** * This method overrides the method that is declared at super. * * @return Column-value map-string. (NotNull) */ public String toString() { final String delimiter = ","; final StringBuffer sb = new StringBuffer(); sb.append(delimiter).append(getTestNum()); sb.delete(0, delimiter.length()); sb.insert(0, "{").append("}"); return sb.toString(); } }
これはDBFlute用の独自クラスか?
こっちにSQLファイルから抽出した内容が定義されている。
でも、わざわざこのファイルが作成される意味はなんなんだろう。。
1つ目のエンティティにプロパティを定義してはだめなんだろうか。明日以降調べてみよう。
全然関係ないけど、やっぱジェネレートするプログラムは便利。
自分でも作ってみるか。これはVelocityつかってるのかな。