Spring-MVC/ステップ・バイ・ステップ/Daoの実装
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[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>
}}
また、Genericsを使うためには、バージョン5以降のコンパイラ...
#pre{{
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>Shift_JIS</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
}}
*** クラスパスの変更 [#iff2d857]
以下のコマンドを実行して.project, .classpathファイルを更...
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
ここで、以下の警告がでます。
#pre{{
Downloading: http://repo1.maven.org/maven2/javax/transact...
[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 repos...
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 ...
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can de...
mvn deploy:deploy-file -DgroupId=javax.transaction -D...
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file...
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等がライセンス付きで提供しているライブラリを勝...
ため、ユーザがsunのサイトからjarファイルをダウンロードし...
ローカルのリポジトリに登録しなくてはなりません。
http://java.sun.com/products/jta/ からjta-1_0_1B-classes....
#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 ...
INSERT INTO t_recipe (id, name) values (2, 'Bucket of Mac...
INSERT INTO t_recipe (id, name) values (3, 'Deep fried ba...
}}
** Genericsを使ってDaoを作る [#g6c5265c]
[[きのさいと>http://www.masatom.in/pukiwiki/Hibernate/Spr...
を参考にGenericsを使ってDaoインタフェースとDaoを作成しま...
*** Generic Daoインタフェース [#ube9c53d]
Daoインタフェースでは、レコードの挿入(C)、読み込み(R)、更...
#pre{{
import java.io.Serializable;
import java.util.List;
public interface IGenericDao<T, PK extends Serializable> {
void saveOrUpdate(T obj);
void delete(T obj);
T findById(Long id);
List<T> findAll();;
}
}}
saveOrUpdateは、挿入または、更新の両方をサポートするメソ...
*** Generic Dao [#af088218]
Generic Daoは、HibernateDaoSupportのサブクラスとして以下...
#pre{{
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.Hiberna...
public class GenericHibernateDao<T, PK extends Serializab...
implements IGenericDao<T, PK> {
private Class<T> type;
public GenericHibernateDao(Class<T> type) {
this.type = type;
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public List<T> findAll() {
return (List<T>)getHibernateTemplate().loadAll(type);
}
public T findById(PK id) {
return (T) getHibernateTemplate().get(type, id);
}
public void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
}
}}
*** Recipe Daoインタフェース [#hb0627b9]
Generic Daoインタフェースを使って、Recipeオブジェクトへの...
Daoインタフェースは以下のように簡潔に記述できます。
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public interface IRecipeDao extends IGenericDao<Recipe, L...
}
}}
*** Recipe Dao [#x0df23e8]
Recipe Daoインタフェースと同様にRecipe Daoも、以下のよう...
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public class RecipeDao extends GenericHibernateDao<Recipe...
public RecipeDao(Class<Recipe> type) {
super(type);
}
}
}}
*** StubRecipeManagerの変更について [#hd6db3a2]
StubRecipeMangerを直接変更してもよいのですが、Bean定義フ...
そのまま残し、代わりにStubRecipeDaoManagerを作成すること...
StubRecipeDaoManagerは、非常に簡単になりました。
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public class StubRecipeDaoManager extends RecipeDao imple...
public StubRecipeDaoManager() {
super(Recipe.class);
}
public void save(Recipe user) {
super.saveOrUpdate(user);
}
}
}}
*** Hibernateのhbm定義ファイル [#f1e62f4f]
Hibernateのhbm定義ファイルをRecipeクラスを同じ場所に定義...
Recipe.hbm.xmlの内容は以下の通りです。
#pre{{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3...
<hibernate-mapping>
<class
name="org.springframework.showcase.coverc.domain.Recipe"
table="T_RECIPE">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
}}
** Bean定義ファイルの変更 [#k8af63da]
最後にBean定義ファイルを変更します。
*** applicationContext.xml [#ea8ea39c]
applicationContext.xmlのrecipeManagerを以下のように変更し...
#pre{{
<bean id="recipeManager" class="org.springframework.show...
<property name="sessionFactory" ref="sessionFactory" />
</bean>
}}
以下のデータベースBean定義をapplicationContext.xmlに追加...
#pre{{
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManage...
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost/springdb</value>
</property>
<property name="username">
<value>spring</value>
</property>
<property name="password">
<value>spring</value>
</property>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionF...
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Po...
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/org/springframework/showcase/coverc...
</list>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
}}
** コメント [#sa0634ce]
この記事は、
#vote(おもしろかった[11],そうでもない[4],わかりずらい[11])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[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>
}}
また、Genericsを使うためには、バージョン5以降のコンパイラ...
#pre{{
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>Shift_JIS</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
}}
*** クラスパスの変更 [#iff2d857]
以下のコマンドを実行して.project, .classpathファイルを更...
#pre{{
$ rm .project .classpath
$ mvn eclipse:eclipse -DdownloadSources=true
}}
ここで、以下の警告がでます。
#pre{{
Downloading: http://repo1.maven.org/maven2/javax/transact...
[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 repos...
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 ...
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can de...
mvn deploy:deploy-file -DgroupId=javax.transaction -D...
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file...
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等がライセンス付きで提供しているライブラリを勝...
ため、ユーザがsunのサイトからjarファイルをダウンロードし...
ローカルのリポジトリに登録しなくてはなりません。
http://java.sun.com/products/jta/ からjta-1_0_1B-classes....
#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 ...
INSERT INTO t_recipe (id, name) values (2, 'Bucket of Mac...
INSERT INTO t_recipe (id, name) values (3, 'Deep fried ba...
}}
** Genericsを使ってDaoを作る [#g6c5265c]
[[きのさいと>http://www.masatom.in/pukiwiki/Hibernate/Spr...
を参考にGenericsを使ってDaoインタフェースとDaoを作成しま...
*** Generic Daoインタフェース [#ube9c53d]
Daoインタフェースでは、レコードの挿入(C)、読み込み(R)、更...
#pre{{
import java.io.Serializable;
import java.util.List;
public interface IGenericDao<T, PK extends Serializable> {
void saveOrUpdate(T obj);
void delete(T obj);
T findById(Long id);
List<T> findAll();;
}
}}
saveOrUpdateは、挿入または、更新の両方をサポートするメソ...
*** Generic Dao [#af088218]
Generic Daoは、HibernateDaoSupportのサブクラスとして以下...
#pre{{
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.Hiberna...
public class GenericHibernateDao<T, PK extends Serializab...
implements IGenericDao<T, PK> {
private Class<T> type;
public GenericHibernateDao(Class<T> type) {
this.type = type;
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public List<T> findAll() {
return (List<T>)getHibernateTemplate().loadAll(type);
}
public T findById(PK id) {
return (T) getHibernateTemplate().get(type, id);
}
public void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
}
}}
*** Recipe Daoインタフェース [#hb0627b9]
Generic Daoインタフェースを使って、Recipeオブジェクトへの...
Daoインタフェースは以下のように簡潔に記述できます。
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public interface IRecipeDao extends IGenericDao<Recipe, L...
}
}}
*** Recipe Dao [#x0df23e8]
Recipe Daoインタフェースと同様にRecipe Daoも、以下のよう...
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public class RecipeDao extends GenericHibernateDao<Recipe...
public RecipeDao(Class<Recipe> type) {
super(type);
}
}
}}
*** StubRecipeManagerの変更について [#hd6db3a2]
StubRecipeMangerを直接変更してもよいのですが、Bean定義フ...
そのまま残し、代わりにStubRecipeDaoManagerを作成すること...
StubRecipeDaoManagerは、非常に簡単になりました。
#pre{{
import org.springframework.showcase.coverc.domain.Recipe;
public class StubRecipeDaoManager extends RecipeDao imple...
public StubRecipeDaoManager() {
super(Recipe.class);
}
public void save(Recipe user) {
super.saveOrUpdate(user);
}
}
}}
*** Hibernateのhbm定義ファイル [#f1e62f4f]
Hibernateのhbm定義ファイルをRecipeクラスを同じ場所に定義...
Recipe.hbm.xmlの内容は以下の通りです。
#pre{{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3...
<hibernate-mapping>
<class
name="org.springframework.showcase.coverc.domain.Recipe"
table="T_RECIPE">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
}}
** Bean定義ファイルの変更 [#k8af63da]
最後にBean定義ファイルを変更します。
*** applicationContext.xml [#ea8ea39c]
applicationContext.xmlのrecipeManagerを以下のように変更し...
#pre{{
<bean id="recipeManager" class="org.springframework.show...
<property name="sessionFactory" ref="sessionFactory" />
</bean>
}}
以下のデータベースBean定義をapplicationContext.xmlに追加...
#pre{{
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManage...
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost/springdb</value>
</property>
<property name="username">
<value>spring</value>
</property>
<property name="password">
<value>spring</value>
</property>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionF...
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Po...
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/org/springframework/showcase/coverc...
</list>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
}}
** コメント [#sa0634ce]
この記事は、
#vote(おもしろかった[11],そうでもない[4],わかりずらい[11])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc