[[Spring-MVC/ステップ・バイ・ステップ]]

2008/04/07からのアクセス回数 &counter;

#contents

** データベースの準備 [#g362eca6]
今回は、Mac版のPostgreSQLを使います。
(残念ながらPostgreX は、Leopardには対応していません)

Mac OSX用のPkgインストーラが以下のサイトにあります。 
http://www.magic3.org/postgrex/

- PostgreX(PostgreSQL8.1.4のOSX用インストールパッケージ)
- Magic Postgre(PostgreSQLフロントエンドプログラム)
- PgPreference?(PostgreSQLの起動停止を行うシステム環境設定プラグイン) 

をダウンロードし、インストールしてください。

Windowsの場合には、PgAdmin Ⅲ が便利です。

*** ユーザ、データベースの作成 [#l11e9096]
Magic Postgreを起動し、ユーザspringを作成します。

- ユーザ名を spring とします
- パスワードも spring とします
- Create Table の権限を与えます

次に、データベースを作成します。

- データベース名を springdb とします
- データベース所有者を spring とします
- Encodingは、UTF-8 とします

** ライブラリの追加 [#h403b421]

Daoは、HibernateSupportDaoを継承しますので、MVNREPOSITORYから

- Hibernate version 3.1
- PostgreSQL JDBC Driver 8.1.x

を検索します。

以下のdependecyタグをpom.xmlに追加します。
#pre{{
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.1-408.jdbc3</version>
    </dependency>
}}

*** クラスパスの変更 [#iff2d857]
以下のコマンドを実行して.project, .classpathファイルを更新します。

#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}

ここで、以下の警告がでます。
#pre{{
Downloading: http://repo1.maven.org/maven2/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar
[WARNING] An error occurred during dependency resolution.
    Failed to retrieve javax.transaction:jta-1.0.1B
Caused by: Unable to download the artifact from any repository

Try downloading the file manually from: 
    http://java.sun.com/products/jta

Then, install it using the command: 
    mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta 
    -Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file

Alternatively, if you host your own repository you can deploy the file there: 
    mvn deploy:deploy-file -DgroupId=javax.transaction -DartifactId=jta 
    -Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

Path to dependency: 
        1) spring.example.web:mvc-convention:war:0.0.1
        2) org.hibernate:hibernate:jar:3.1
        3) javax.transaction:jta:jar:1.0.1B


  javax.transaction:jta:jar:1.0.1B

from the specified remote repositories:
  central (http://repo1.maven.org/maven2)
}}

これは、sun等がライセンス付きで提供しているライブラリを勝手にMVN Repositoryに登録できない
ため、ユーザがsunのサイトからjarファイルをダウンロードし、install_fileプラグインを使って
ローカルのリポジトリに登録しなくてはなりません。

http://java.sun.com/products/jta/ からjta-1_0_1B-classes.zipファイルをダウンロードしてください。
#ref(jta-1_0_1B-classes.zip);

次に以下のコマンドでローカルのリポジトリに登録してください。

#pre{{
$ mvn install:install-file \
 -Dfile=./jta-1_0_1B-classes.zip \
 -DgroupId=javax.transaction \
 -DartifactId=jta \
 -Dversion=1.0.1B \
 -Dpackaging=jar
}}

** テーブルの作成 [#feb95cd7]
以下のSQL文を実行して、t_recipeテーブルを作成し、テストデータを
挿入します。(私は、DbEditを使いました)

#pre{{
CREATE TABLE t_recipe (
	id integer NOT NULL PRIMARY KEY,
	name varchar (255) );

INSERT INTO t_recipe (id, name) values (1, 'Goats Cheese with beetroot sauce');
INSERT INTO t_recipe (id, name) values (2, 'Bucket of MaccyDeez chicken de Harrop');
INSERT INTO t_recipe (id, name) values (3, 'Deep fried battered Hershey bar');
}}

** Genericsを使ってDaoを作る [#g6c5265c]
[[きのさいと->http://www.masatom.in/pukiwiki/Hibernate/Spring%A4%C8Generic%A4%F2%BB%C8%A4%C3%A4%C6%A1%A2%C8%C6%CD%D1%C5%AA%A4%CADAO%A4%F2%BA%EE%C0%AE%A4%B9%A4%EB/]]
を参考にGenericsを使ってDaoインタフェースとDaoを作成します。

*** Daoインタフェース [#ube9c53d]
Daoインタフェースでは、レコードの挿入(C)、読み込み(R)、更新(U)、削除(D)を記述します。

#pre{{
import java.io.Serializable;

public interface IGenericDao<T, PK extends Serializable> {
	void 	saveOrUpdate(T obj);
	void	delete(T obj);
	T 		findById(Long id);
	T[]		findAll();
}
}}

saveOrUpdateは、挿入または、更新の両方をサポートするメソッドです。

** コメント [#sa0634ce]
この記事は、

#vote(おもしろかった,そうでもない,わかりずらい)

皆様のご意見、ご希望をお待ちしております。

#comment

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
SmartDoc