2008/04/07からのアクセス回数 16022
今回は、Mac版のPostgreSQLを使います。 (残念ながらPostgreX は、Leopardには対応していません)
Mac OSX用のPkgインストーラが以下のサイトにあります。 http://www.magic3.org/postgrex/
をダウンロードし、インストールしてください。
Windowsの場合には、PgAdmin Ⅲ が便利です。
Magic Postgreを起動し、ユーザspringを作成します。
次に、データベースを作成します。
Daoは、HibernateSupportDaoを継承しますので、MVNREPOSITORYから
を検索します。
以下のdependecyタグをpom.xmlに追加します。
<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>
以下のコマンドを実行して.project, .classpathファイルを更新します。
$ rm .project .classpath $ mvn eclipse:eclipse -DdownloadSources=true
ここで、以下の警告がでます。
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ファイルをダウンロードしてください。
次に以下のコマンドでローカルのリポジトリに登録してください。
$ mvn install:install-file \ -Dfile=./jta-1_0_1B-classes.zip \ -DgroupId=javax.transaction \ -DartifactId=jta \ -Dversion=1.0.1B \ -Dpackaging=jar
以下のSQL文を実行して、t_recipeテーブルを作成し、テストデータを 挿入します。(私は、DbEditを使いました)
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インタフェースとDaoを作成します。
Daoインタフェースでは、レコードの挿入(C)、読み込み(R)、更新(U)、削除(D)を記述します。
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は、挿入または、更新の両方をサポートするメソッドです。
この記事は、
皆様のご意見、ご希望をお待ちしております。