Spring Rooで既存のDBからJPAエンティティを生成し、CRUDアプリをGWTで作成する
※ 1.2系じゃないと↓は動きません
使うテーブルはよくあるやつ。MySQLを使用する。
mysql> desc address;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| STREET | varchar(20) | NO | | NULL | |
| CITY | varchar(10) | NO | | NULL | |
| VERSION | int(11) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc department;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| NAME | varchar(10) | NO | UNI | NULL | |
| LOCATION | varchar(10) | NO | | NULL | |
| VERSION | int(11) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc employee;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| DEPARTMENT_ID | int(11) | NO | MUL | NULL | |
| ADDRESS_ID | int(11) | NO | UNI | NULL | |
| NAME | varchar(10) | NO | | NULL | |
| AGE | int(11) | NO | | NULL | |
| VERSION | int(11) | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
今回はリモートのMySQLサーバー(ユーザー名:root、パスワードなし、データベース名:example)につなぎにいってみる。
$ roo.sh
roo> project --topLevelPackage am.ik.crud
roo> jpa setup --provider HIBERNATE --database MYSQL --hostName MySQLサーバーのIP --userName root --databaseName example
リバースエンジニアリングのコマンドは以下である。
roo> database reverse engineer --schema example --package ~.model --testAutomatically
初回実行すると
roo> database reverse engineer --schema example --package ~.model --testAutomatically
Located add-on that may offer this JDBC driver
1 found, sorted by rank; T = trusted developer; R = Roo 1.2 compatible
ID T R DESCRIPTION -------------------------------------------------------------
01 Y Y 5.1.17.0020 #jdbcdriver driverclass:com.mysql.jdbc.Driver. This...
--------------------------------------------------------------------------------
[HINT] use 'addon info id --searchResultId ..' to see details about a search result
[HINT] use 'addon install id --searchResultId ..' to install a specific search result, or
[HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
JDBC driver not available for 'com.mysql.jdbc.Driver'
と怒られる。MySQLドライバがないらしい。suggestされるがままに
roo> addon install id --searchResultId 01
Target resource(s):
-------------------
Spring Roo - Wrapping - mysql-connector-java (5.1.17.0020)
Required resource(s):
---------------------
slf4j-api (1.6.1)
slf4j-nop (1.6.1)
Deploying...done.
Successfully installed add-on: Spring Roo - Wrapping - mysql-connector-java [version: 5.1.17.0020]
を実行してMySQLドライバをインストールする。もう一回実行すると
roo> database reverse engineer --schema example --package ~.model --testAutomatically
Created SRC_MAIN_RESOURCES/dbre.xml
Created SRC_MAIN_JAVA/am/ik/crud/model
Created SRC_MAIN_JAVA/am/ik/crud/model/Address.java
Created SRC_MAIN_JAVA/am/ik/crud/model/Department.java
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee.java
Created SRC_TEST_JAVA/am/ik/crud/model
Created SRC_TEST_JAVA/am/ik/crud/model/AddressDataOnDemand.java
Created SRC_TEST_JAVA/am/ik/crud/model/AddressIntegrationTest.java
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentDataOnDemand.java
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentIntegrationTest.java
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeDataOnDemand.java
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeIntegrationTest.java
Created SRC_MAIN_JAVA/am/ik/crud/model/Address_Roo_Configurable.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Address_Roo_Jpa_Entity.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Address_Roo_DbManaged.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Address_Roo_ToString.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Address_Roo_Entity.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Department_Roo_Configurable.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Department_Roo_Jpa_Entity.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Department_Roo_DbManaged.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Department_Roo_ToString.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Department_Roo_Entity.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee_Roo_Configurable.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee_Roo_Jpa_Entity.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee_Roo_DbManaged.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee_Roo_ToString.aj
Created SRC_MAIN_JAVA/am/ik/crud/model/Employee_Roo_Entity.aj
Created SRC_TEST_JAVA/am/ik/crud/model/AddressIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/AddressDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/am/ik/crud/model/AddressIntegrationTest_Roo_IntegrationTest.aj
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeIntegrationTest_Roo_IntegrationTest.aj
Created SRC_TEST_JAVA/am/ik/crud/model/EmployeeDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/AddressDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/am/ik/crud/model/DepartmentIntegrationTest_Roo_IntegrationTest.aj
一杯生成された!
あとは
roo> web gwt setup
roo> web gwt all --proxyPackage ~.client.proxy --requestPackage ~.client.request
からの〜
$ mvn gwt:run
で
できた!