インストール方法は簡単で、「Feature Updates」で「Search for new features to install」を選択して、「Update sites to visit」の「New Remote Site」ボタンで
http://springide.org/updatesite
を登録するだけです。その際Aspect Jのプラグイン(?)も必要になるようなので、併せてインストールしました。
<beans>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432"/Tomcat/>
<property name="username" value="tomcat"/>
<property name="password" value="password"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>マッピングファイル1.hbm.xml</value>
</list>
<list>
<value>マッピングファイル2.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
</value>
</property>
</bean>
</beans>
public class ProductDaoImpl implements ProductDao {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public Collection loadProductsByCategory(String category) throws DataAccessException {
return this.hibernateTemplate.find("from test.Product product where product.category=?", category);
}
}
<beans>
<bean id="myProductDao" class="product.ProductDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
HibernateTemplateのAPI
public class ProductDaoImpl implements ProductDao {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public Collection loadProductsByCategory(final String category) throws DataAccessException {
return this.hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Criteria criteria = session.createCriteria(Product.class);
criteria.add(Expression.eq("category", category));
criteria.setMaxResults(6);
return criteria.list();
}
};
}
}
JavaでWebアプリケーションを開発することになったのでSpring Frameworkを調査してみました。
従来のMVCフレームワークの問題点とSpring Frameworkによるその解決
従来のMVCフレームワークには以下のような問題点があるようです。
Spring Frameworkを利用するとどうなるのかというと