SAStruts(Super Agile Struts)あれこれ
MySQLからS2JDBC-GenでEntityを自動生成しましょう(コマンドライン編)
目次
- 序
- 造
- 1.Apache-Antをダウンロード&セットアップ
- 2.S2JDBC-GenやSeasar2(S2JDBC)のJARファイルをダウンロード&セットアップ
- 3.MySQL Connector/J (JDBCドライバ)を配置
- 4.Seasar2とS2JDBC用のDICONファイルを作成
- convention.dicon
- s2jdbc.dicon
- jdbc.dicon
- 5.ビルドコンフィグファイル作成
- 6.Entityクラスの自動生成
- 結
- おまけ的
- フォルダ/ファイル構成
序
今回はS2JDBC-GenのEntityクラスの自動生成機能を試してみます。EclipseのAnt機能でも生成可能なのですが、ここではコマンドラインでAntコマンドを使用する方法で行います。
データベースはMySQLのインストール後の設定で構築したMySQL環境を使用します。
エンティティクラスの生成も併せて参考にして下さい。
造
1.Apache-Antをダウンロード&セットアップ
こちらからApache-Antをダウンロードします(本稿執筆時2008.09.03の最新はapache-ant-1.7.1-bin.zipです)。apache-ant-1.7.1-bin.zipを解凍して出来た、apache-ant-1.7.1フォルダを任意の場所に移動します。本稿ではC:\usr\localに移動します。
PATHに"C:\usr\local\apache-ant-1.7.1\bin"を追加します。
2.S2JDBC-GenやSeasar2(S2JDBC)のJARファイルをダウンロード&セットアップ
作業ディレクトリ作成
以下のフォルダを作成します。C:\usr\local\s2jdbc
C:\usr\local\s2jdbc\lib
C:\usr\local\s2jdbc\conf
S2JDBC-GenのJARファイル
こちらからS2JDBC-Gen(本稿執筆時2008.09.03の最新はS2JDBC-Gen 0.9.0です)をダウンロードしましょう。
ダウンロードしたファイルを任意の場所で解凍して、生成した"s2jdbc-gen\lib"配下にあるJARファイルを、"C:\usr\local\s2jdbc\lib"フォルダにコピーします。
Seasar2(S2JDBC)のJARファイル
本当はSeasar2セットアップに従って、必要なモジュールをセットアップするのですが、ここではサボっちゃいます。
こちらからS2JDBCのチュートリアル(本稿執筆時2008.09.03の最新はS2JDBC Tutorial 2.4.28です)をダウンロードしましょう。
ダウンロードしたファイルを任意の場所で解凍して、生成した"s2jdbc-tutorial\lib"配下にあるJARファイルを、"C:\usr\local\s2jdbc\lib"フォルダにコピーします。
3.MySQL Connector/J (JDBCドライバ)を配置
S2JDBC-GenがEntityクラスを自動生成する際には、MySQLデータベースに接続してカタログ情報を取得します。なので、実行時にはJDBCドライバが必要になります。環境・必要なモジュール準備でダウンロードした"mysql-connector-java-5.1.6-bin.jar"を "C:\usr\local\s2jdbc\lib"フォルダにコピーします。
4.Seasar2とS2JDBC用のDICONファイルを作成
"C:\usr\local\s2jdbc\conf\convention.dicon"を以下の内容で作成します。<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components> <component class="org.seasar.framework.convention.impl.NamingConventionImpl"> <initMethod name="addRootPackageName"> <arg>""</arg> </initMethod> </component> <component class="org.seasar.framework.convention.impl.PersistenceConventionImpl"/> </components>
"C:\usr\local\s2jdbc\conf\s2jdbc.dicon"を以下の内容で作成します。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components> <include path="jdbc.dicon"/> <include path="s2jdbc-internal.dicon"/> <component name="jdbcManager" class="org.seasar.extension.jdbc.manager.JdbcManagerImpl"> <property name="maxRows">0</property> <property name="fetchSize">0</property> <property name="queryTimeout">0</property> <property name="dialect">mysqlDialect</property> </component> </components>
"C:\usr\local\s2jdbc\conf\jdbc.dicon"を以下の内容で作成します。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components namespace="jdbc"> <include path="jta.dicon"/> <component name="xaDataSource" class="org.seasar.extension.dbcp.impl.XADataSourceImpl"> <property name="driverClassName"> "com.mysql.jdbc.Driver" </property> <property name="URL"> "jdbc:mysql://localhost:3306/sastruts" </property> <property name="user">"sastruts"</property> <property name="password">"sastruts"</property> </component> <component name="connectionPool" class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl"> <property name="timeout">600</property> <property name="maxPoolSize">10</property> <property name="allowLocalTx">true</property> <destroyMethod name="close"/> </component>
<component name="DataSource" class="org.seasar.extension.dbcp.impl.DataSourceImpl" /> </components>
5.ビルドコンフィグファイル作成
"C:\usr\local\s2jdbc\s2jdbc-gen-build.xml"を以下の内容で作成します。
<?xml version="1.0"?>
<project name="sample" default="gen-entity" basedir=".">
<path id="classpath">
<fileset dir="lib"/>
</path>
<taskdef resource="s2jdbc-gen-task.properties" classpathref="classpath"/>
<target name="gen-entity">
<gen-entity
rootpackagename="mysql"
classpathref="classpath"
classpath="conf"
overwrite="true"
/>
</target>
</project>
6.Entityクラスの自動生成
Windowsのコマンドプロンプトを起動して、"C:\usr\local\s2jdbc"にチェンジディレクトリ(移動)して、以下のコマンドを実行します。ant -f s2jdbc-gen-build.xml gen-entity
以下のような表示がされ、"src/main/mysql/entity"フォルダにエンティティクラスファイルが生成されます。
C:\usr\local\s2jdbc>ant -f s2jdbc-gen-build.xml gen-entity Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.6. 0_07\lib\tools.jar Buildfile: s2jdbc-gen-build.xml"Unable to locate tools.jar."というメッセージは、JREのjavaコマンドを使用しているために出力されているだけなので、ここでは問題ないので無視して構いません。"tools.jar"は、JDKに同梱されています(C:\Program Files\Java\jdk1.6.0_07\libなどにあります)。antは開発環境で使用されることを前提としているためでしょうか?
gen-entity:
BUILD SUCCESSFUL Total time: 3 seconds
結
以下の3つのエンティティクラスファイルが生成されます。src/main/mysql/entity/Address.java src/main/mysql/entity/Department.java src/main/mysql/entity/Employee.java中を見ると↓のようにテーブル結合のアノテートもされています。
Employee.java抜粋
/** addressIdプロパティ */
@Column(precision = 10, nullable = true, unique = false)
public Integer addressId;
/** address関連プロパティ */
@ManyToOne
public Address address;
おまけ的
フォルダ/ファイル構成
作業しているフォルダ/ファイルの構成は、以下のようになります。 srcフォルダ配下が生成されたエンティティクラスファイルです。───────────────────────────────────── C:\usr\local\s2jdbc s2jdbc < Dir > ├ conf < Dir > │ ├ convention.dicon 442 2008/09/03 14:37:57 │ ├ jdbc.dicon 1,306 2008/09/03 10:29:28 │ └ s2jdbc.dicon 542 2008/09/03 13:52:31 ├ lib < Dir > │ ├ ant-1.7.0.jar 1,289,806 2008/09/02 17:58:48 │ ├ aopalliance-1.0.jar 4,467 2008/06/26 23:53:12 │ ├ commons-logging-1.1.jar 52,915 2008/06/26 23:54:08 │ ├ freemarker-2.3.13.jar 870,292 2008/09/02 17:58:44 │ ├ geronimo-annotation_1.0_spec-1.0.jar 12,085 2008/06/26 23:53:50 │ ├ geronimo-ejb_3.0_spec-1.0.jar 31,752 2008/06/26 23:54:14 │ ├ geronimo-interceptor_3.0_spec-1.0.jar 8,954 2008/06/26 23:54:14 │ ├ geronimo-jpa_3.0_spec-1.0.jar 52,555 2008/06/26 23:54:00 │ ├ geronimo-jta_1.1_spec-1.0.jar 14,972 2008/06/26 23:54:04 │ ├ geronimo-jta_1.1_spec-1.0-tsr.jar 7,329 2008/06/26 23:54:08 │ ├ hsqldb-1.8.0.1.jar 625,096 2008/06/26 23:54:02 │ ├ javassist-3.4.ga.jar 471,005 2008/06/26 23:54:12 │ ├ junit-3.8.2.jar 120,640 2008/06/26 23:54:12 │ ├ log4j-1.2.13.jar 358,180 2008/06/26 23:54:10 │ ├ mysql-connector-java-5.1.6-bin.jar 703,265 2008/03/05 17:27:00 │ ├ ognl-2.6.9-patch-20070908.jar 166,995 2008/06/26 23:54:00 │ ├ poi-3.0-FINAL.jar 1,767,938 2008/06/26 23:53:56 │ ├ s2-extension-2.4.28.jar 385,584 2008/08/26 16:50:36 │ ├ s2-framework-2.4.28.jar 654,191 2008/08/26 17:34:36 │ ├ s2jdbc-gen-0.9.0.jar 366,917 2008/09/02 19:41:58 │ ├ s2-tiger-2.4.28.jar 569,177 2008/08/26 17:34:36 │ └ servlet-api.jar 97,703 2008/06/26 23:54:08 ├ src < Dir > │ └ main < Dir > │ └ java < Dir > │ └ mysql < Dir > │ └ entity < Dir > │ ├ Address.java 743 2008/09/04 09:03:35 │ ├ Department.java 752 2008/09/04 09:03:35 │ └ Employee.java 1,300 2008/09/04 09:03:35 └ s2jdbc-gen-build.xml 447 2008/09/04 08:53:57 ─────────────────────────────────────
アップロードしたファイル
- tomcat_eclipse.jpg
- tomcat_eclipse2.jpg
- after_import.jpg
- start_tomcat.jpg
- import_1.jpg
- create_db_user.jpg
- create_table.jpg
- employee_mod1.jpg
- er1.jpg
- joincolumnser.jpg
- employee_add1.jpg
- employee_add2.jpg
- employee_add3.jpg
- employee_add4.jpg
- employee_del1.jpg
- employee_del2.jpg
- employee_del3.jpg
- employee_token1.jpg
- employee_token2.jpg
- employee_token3.jpg
- employee_page1.jpg
- index_page.jpg
- minus_action_1.jpg
- minus_action_2.jpg
- employee_hsqldb1.jpg
- emp_blk_add1.jpg
- emp_blk_add2.jpg
- emp_blk_add3.jpg
- emp_blk_add4.jpg
- emp_blk_add5.jpg
- hello_project1.jpg
- hello_project2.jpg
- hello_project3.jpg
- hello_project4.jpg
- hello_project5.jpg
- tiles1_1.jpg
- tiles1_2.jpg
- tiles1_3.jpg
- tiles1_4.jpg
- tutorial_db_er.jpg
- mysql_entity1.jpg
- mysql_entity2.jpg
- mysql_entity3.jpg
- mysql_entity4.jpg
- mysql_entity5.jpg
- mysql_entity6.jpg
- mysql_entity7.jpg
- mysql_entity8.jpg
- mysql_entity9.jpg
- mysql_entity10.jpg
- download_file1.jpg
- up.gif
- ajax_pattern1_1.jpg
- ajax_pattern1_2.jpg
- ajax_pattern2_2.jpg
- ajax_pattern2_1.jpg
- emp_blk_upd1.jpg
- emp_blk_upd2.jpg
- emp_blk_upd3.jpg
- emp_blk_upd4.jpg
- emp_blk_upd5.jpg
- ajax_json_1.jpg
- ajax_json_2.jpg
- ajax_json_3.jpg
- ajax_json_4.jpg
- ajax_json_5.jpg
- gen_eclipse1_1.jpg
- gen_eclipse1_2.jpg
- gen_eclipse1_3.jpg
- gen_eclipse1_4.jpg
- gen_eclipse1_5.jpg
- blk_add_vali_1.jpg
- blk_add_vali_2.jpg
- blk_add_vali_3.jpg
- blk_add_vali_4.jpg
- super1_1__1.jpg
- super1_1__2.jpg
- super1_1__0.jpg
- super1_1__3.jpg
- gen_fileheader1.jpg
- super1_1__4.jpg
- supersample2-1_1.jpg
- supersample2-1_2.jpg
- supersample2-1_3.jpg
- supersample2-1_4.jpg
- previewimage.jpg
- previewimage2.jpg
- supersample2-1_5.jpg
- supersample2-2_1.jpg
- supersample2-2_4.jpg
- supersample2-2_2.jpg
- supersample2-2_3.jpg
- supersample2-2_5.jpg
- supersample2-4_1.jpg
- supersample2-4_2.jpg
- supersample2-4_3.jpg
- supersample2-4_4.jpg
- supersample2-4_5.jpg
- supersample2-5_1.jpg
- supersample2-5_2.jpg
- supersample2-5_3.jpg
- supersample2-5_4.jpg
- supersample2-5_5.jpg
- supersample2-5_6.jpg
- supersample2-6_1.jpg
- supersample2-6_2.jpg
- supersample2-6_4.jpg
- supersample2-6_5.jpg
- supersample2-6_3.jpg
- supersample2-7_1.jpg
- supersample2-7_2.jpg
- blk_upd_reset_1.jpg
- blk_upd_reset_2.jpg
- blk_upd_reset_3.jpg
- blk_upd_reset_4.jpg
- blk_upd_reset_5.jpg
- supersample1-6_1.jpg
- supersample1-6_2.jpg
- supersample1-6_3.jpg
- supersample1-6_4.jpg
- supersample1-6_5.jpg
- supersample1-6_6.jpg
- supersample1-7_1.jpg
- supersample1-7_2.jpg
- supersample1-7_3.jpg
- supersample1-7_4.jpg
- supersample1-7_5.jpg
- supersample1-7_6.jpg
- supersample1-4_1.jpg
- supersample1-4_2.jpg
- supersample1-4_3.jpg
- supersample1-4_4.jpg
- supersample1-4_6.jpg
- supersample1-4_7.jpg
- supersample1-4_5.jpg
- pre_db_sample1.jpg
- pre_db_sample2.jpg
- pre_db_sample3.jpg
- pre_db_sample4.jpg
- pre_db_sample5.jpg
- supersample1-8_1.jpg
- supersample1-8_2.jpg
- supersample1-8_3.jpg
- supersample1-8_4.jpg
- supersample1-8_6.jpg
- supersample1-8_5.jpg
- more_s2jdbc_1.jpg
- more_s2jdbc_2.jpg
- more_s2jdbc_3.jpg
- more_s2jdbc_4.jpg
- t_safe_search1_1.jpg
- t_safe_search1_2.jpg
- t_safe_search2_1.jpg
- t_safe_search2_2.jpg
- project_websrv_1.jpg
- project_websrv_3.jpg
- ws_1address_1.jpg
- flexbuilder_1.jpg
- flexbuilder_2.jpg
- flexbuilder_3.jpg
- flexbuilder_4.jpg
- flexbuilder_5.jpg
- flexbuilder_6.jpg
- flexbuilder_7.jpg
- flexbuilder_8.jpg
- flexbuilder_9.jpg
- flexbuilder_10.jpg
- flexbuilder_11.jpg
- flexbuilder_12.jpg
- flexbuilder_13.jpg
- project_flex_1.jpg
- project_flex_2.jpg
- project_flex_3.jpg
- project_flex_4.jpg
- project_flex_5.jpg
- project_flex_8.jpg
- project_flex_7.jpg
- project_flex_6.jpg
- flex_emp_list_1.jpg
- flex_emp_list_2.jpg
- flex_emp_lst_e_1.jpg
- flex_emp_add_1.jpg
- flex_emp_add_2.jpg
- flex_emp_add_3.jpg
- flex_emp_add2_1.jpg
- flex_emp_add2_2.jpg
- flex_emp_add2_3.jpg
- flex_upload_1.jpg
- flex_upload_2.jpg
- tcpmon_1.jpg
- tcpmon_2.jpg
- tcpmon_3.jpg
- tcpmon_4.jpg
- flex_upload2_1.jpg
- flex_upload2_2.jpg
- flex_upload2_3.jpg
- flex_emp_del_1.jpg
- flex_emp_del_2.jpg
- flex_emp_del_3.jpg
- flex_emp_spr_1.jpg
- flex_emp_spr_2.jpg
- flex_emp_spr_3.jpg
- flex_grid_chk1_1.jpg
- flex_grid_chk1_2.jpg
- flex_grid_chk1_3.jpg
- flex_bulk_del_1.jpg
- flex_bulk_del_2.jpg
- flex_bulk_del_3.jpg
- flex_dd_grid1_1.jpg
- flex_dd_grid1_2.jpg
- flex_dd_grid1_3.jpg
- blazeds_setup_1.jpg
- s2blaze_proj_1.jpg
- s2blaze_proj_2.jpg
- s2blaze_proj_3.jpg
- s2blaze_proj_4.jpg
- s2blaze_proj_5.jpg
- s2blaze_proj_6.jpg
- s2blaze_proj_7.jpg
- s2blaze_proj_8.jpg
- s2blaze_proj_9.jpg
- blaze_emplist1_1.jpg
- blaze_emplist2_1.jpg
- blaze_emplist2_2.jpg
- blaze_emp_add_1.jpg
- blaze_emp_add_2.jpg
- blaze_emp_add_3.jpg
- blaze_fms1_1.jpg
- blaze_fms1_2.jpg
- blaze_fms2_1.jpg
- project_batch_1.jpg
- supersample2-3_1.jpg
- supersample2-3_2.jpg
- supersample2-3_3.jpg
- supersample2-8_1.jpg
- supersample2-8_2.jpg
















