Struts 2 + Spring + Hibernate統合の例
それをダウンロードする –
Struts2-Spring-Hibernate-Integration-Example.zip
このチュートリアルでは、「
Struts2 Spring Hibernate
」の統合を示しています。続行する前に、次のチュートリアルを確認してください。
-
link://struts2/struts-2-hibernate-integration-example/[Struts 2
休止状態の統合の例]。
Struts 2 + Spring
統合例]。リンク://struts/struts-spring-hibernate-integration-example/[Struts 1.x
+ Spring + Hibernate統合の例]
統合手順の概要を参照してください。
-
すべての依存関係ライブラリを取得する(たくさん).
-
Springの
ContextLoaderListener
を登録して、Struts 2と
春。
-
Springの
LocalSessionFactoryBean
を使用してSpringと
休止状態。
-
完了、すべての接続.
関係を参照してください:
Struts 2 <-- (ContextLoaderListener) --> Spring <-- (LocalSessionFactoryBean) --> Hibernate
これは非常に長いチュートリアルで、説明はほとんどありません。上記の3つの記事で詳細を確認してください。
チュートリアルスタート…
顧客を追加し、顧客機能をリストアップして、顧客ページを作成します。フロントエンドは
Struts 2
を使用して表示します。
Springは依存性注入エンジンとして、
Hibernate ** はデータベース操作を行うために使用します。始めましょう…
1.プロジェクトの構造
プロジェクトフォルダ構造。
2. MySQLテーブルスクリプト
顧客のテーブルスクリプト。
DROP TABLE IF EXISTS `mkyong`.`customer`; CREATE TABLE `mkyong`.`customer` ( `CUSTOMER__ID` bigint(20) unsigned NOT NULL AUTO__INCREMENT, `NAME` varchar(45) NOT NULL, `ADDRESS` varchar(255) NOT NULL, `CREATED__DATE` datetime NOT NULL, PRIMARY KEY (`CUSTOMER__ID`) ) ENGINE=InnoDB AUTO__INCREMENT=17 DEFAULT CHARSET=utf8;
3.依存ライブラリ
このチュートリアルでは、多くの依存関係ライブラリを要求しています。
<!-- Struts 2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> <!-- Struts 2 + Spring plugins --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.1.8</version> </dependency>
-
MySQL … **
<!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency>
-
春…**
<!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>2.5.6</version> </dependency>
-
休止状態… **
<!-- Hibernate core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.7.ga</version> </dependency> <!-- Hibernate core library dependency start --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- Hibernate core library dependency end --> <!-- Hibernate query library dependency start --> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <!-- Hibernate query library dependency end -->
4.休止状態…
SpringはHibernateの設定を処理するため、モデルとマッピングファイルのみが必要です。
-
Customer.java ** – 顧客テーブル用のクラスを作成します。
package com.mkyong.customer.model; import java.util.Date; public class Customer implements java.io.Serializable { private Long customerId; private String name; private String address; private Date createdDate; //getter and setter methods }
-
Customer.hbm.xml ** – 顧客用のHibernateマッピングファイル。
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 20 Julai 2010 11:40:18 AM by Hibernate Tools 3.2.5.Beta --> <hibernate-mapping> <class name="com.mkyong.customer.model.Customer" table="customer" catalog="mkyong"> <id name="customerId" type="java.lang.Long"> <column name="CUSTOMER__ID"/> <generator class="identity"/> </id> <property name="name" type="string"> <column name="NAME" length="45" not-null="true"/> </property> <property name="address" type="string"> <column name="ADDRESS" not-null="true"/> </property> <property name="createdDate" type="timestamp"> <column name="CREATED__DATE" length="19" not-null="true"/> </property> </class> </hibernate-mapping>
5.ストラット2 …
BoとDAOのデザインパターンを実装します。すべてのBoとDAOはSpring Bean設定ファイルでSpringによってDIになります。 DAOでは、Springの
HibernateDaoSupport
を拡張して、SpringとHibernateの統合を統合します。
-
CustomerBo.java **
package com.mkyong.customer.bo; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerBo{ void addCustomer(Customer customer); List<Customer> listCustomer(); }
-
CustomerBoImpl.java **
package com.mkyong.customer.bo.impl; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.dao.CustomerDAO; import com.mkyong.customer.model.Customer; public class CustomerBoImpl implements CustomerBo{ CustomerDAO customerDAO; //DI via Spring public void setCustomerDAO(CustomerDAO customerDAO) { this.customerDAO = customerDAO; } //call DAO to save customer public void addCustomer(Customer customer){ customerDAO.addCustomer(customer); } //call DAO to return customers public List<Customer> listCustomer(){ return customerDAO.listCustomer(); } }
-
CustomerDAO.java **
package com.mkyong.customer.dao; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerDAO{ void addCustomer(Customer customer); List<Customer> listCustomer(); }
-
CustomerDAOImpl.java **
package com.mkyong.customer.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.mkyong.customer.dao.CustomerDAO; import com.mkyong.customer.model.Customer; public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO{ //add the customer public void addCustomer(Customer customer){ getHibernateTemplate().save(customer); } //return all the customers in list public List<Customer> listCustomer(){ return getHibernateTemplate().find("from Customer"); } }
-
CustomerAction.java
– Struts2アクションは
ActionSupport ** を拡張する必要がなくなり、Springはそれを処理します。
package com.mkyong.customer.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.model.Customer; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction implements ModelDriven{ Customer customer = new Customer(); List<Customer> customerList = new ArrayList<Customer>(); CustomerBo customerBo; //DI via Spring public void setCustomerBo(CustomerBo customerBo) { this.customerBo = customerBo; } public Object getModel() { return customer; } public List<Customer> getCustomerList() { return customerList; } public void setCustomerList(List<Customer> customerList) { this.customerList = customerList; } //save customer public String addCustomer() throws Exception{ //save it customer.setCreatedDate(new Date()); customerBo.addCustomer(customer); //reload the customer list customerList = null; customerList = customerBo.listCustomer(); return "success"; } //list all customers public String listCustomer() throws Exception{ customerList = customerBo.listCustomer(); return "success"; } }
6.春…
ほぼすべての設定がここで行われます。つまり、Springは統合作業に特化しています。
-
CustomerBean.xml ** – SpringのBeanを宣言します:Action、BO、DAO。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerAction" class="com.mkyong.customer.action.CustomerAction"> <property name="customerBo" ref="customerBo"/> </bean> <bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" > <property name="customerDAO" ref="customerDAO"/> </bean> <bean id="customerDAO" class="com.mkyong.customer.dao.impl.CustomerDAOImpl" > <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
-
database.properties ** – データベースの詳細を宣言します。
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mkyong jdbc.username=root jdbc.password=password
-
DataSource.xml ** – データソースBeanを作成します。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>WEB-INF/classes/config/database/properties/database.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
-
HibernateSessionFactory.xml
– SpringとHibernateを統合する
sessionFactory ** Beanを作成します。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show__sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/mkyong/customer/hibernate/Customer.hbm.xml</value> </list> </property> </bean> </beans>
-
SpringBeans.xml ** – コアのSpring Bean構成ファイルを作成し、中央のBean管理として動作させます。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Database Configuration --> <import resource="config/spring/DataSource.xml"/> <import resource="config/spring/HibernateSessionFactory.xml"/> <!-- Beans Declaration --> <import resource="com/mkyong/customer/spring/CustomerBean.xml"/> </beans>
7. JSPページ
JSPページでStruts 2タグを含む要素を表示します。
-
customer.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 + Spring + Hibernate integration example</h1> <h2>Add Customer</h2> <s:form action="addCustomerAction" > <s:textfield name="name" label="Name" value=""/> <s:textarea name="address" label="Address" value="" cols="50" rows="5"/> <s:submit/> </s:form> <h2>All Customers</h2> <s:if test="customerList.size() > 0"> <table border="1px" cellpadding="8px"> <tr> <th>Customer Id</th> <th>Name</th> <th>Address</th> <th>Created Date</th> </tr> <s:iterator value="customerList" status="userStatus"> <tr> <td><s:property value="customerId"/></td> <td><s:property value="name"/></td> <td><s:property value="address"/></td> <td><s:date name="createdDate" format="dd/MM/yyyy"/></td> </tr> </s:iterator> </table> </s:if> <br/> <br/> </body> </html>
8. struts.xml
すべてのリンク〜
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default"> <action name="addCustomerAction" class="customerAction" method="addCustomer" > <result name="success">pages/customer.jsp</result> </action> <action name="listCustomerAction" class="customerAction" method="listCustomer" > <result name="success">pages/customer.jsp</result> </action> </package> </struts>
9. Struts 2 + Spring
Struts 2とSpringを統合するには、
ContextLoaderListener
リスナークラスを登録し、デフォルトの ”
applicationContext.xml
“の代わりに ”
SpringBeans.xml
“を解析するようSpringコンテナに要求する ”
contextConfigLocation
“パラメータを定義します。
-
web.xml **
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd" > <web-app> <display-name>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/** </url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/SpringBeans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
リファレンス
-
link://struts2/struts-2-hibernate-integration-example/[Struts 2
休止状態の統合の例]。
Struts 2 + Spring
統合例]。 link://struts2/struts-2-hibernate-integration-with-full-hibernate-plugin/[Struts
2完全なHibernate Pluginを持つHibernateの例]。リンク://struts/struts-spring-hibernate-integration-example/[Struts 1.x
Spring Hibernate統合の例]