JSF 2.0 + Spring + Hibernate統合の例
これは、JSF 2.0、Spring、およびHibernateを統合する方法を示す長い記事です。 記事の最後に、データベースの既存の顧客のリストを表示するページと、ユーザーがデータベースに新しい顧客を追加できるようにする「顧客の追加」機能を作成します。
P.S In this example, we are using MySQL database and deploy to Tomcat 6 web container.
1. プロジェクト構造
この例のディレクトリ構造


2. テーブルスクリプト
顧客テーブルを作成し、2つのダミーレコードを挿入します。
DROP TABLE IF EXISTS `exampledb`.`customer`; CREATE TABLE `exampledb`.`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; insert into exampledb.customer(customer_id, name, address, created_date) values(1, 'example1', 'address1', now()); insert into exampledb.customer(customer_id, name, address, created_date) values(2, 'example2', 'address2', now());
3. 休止状態のもの
顧客テーブルのモデルクラスとHibernateマッピングファイル。
ファイル:Customer.java
package com.example.customer.model;
import java.util.Date;
public class Customer{
public long customerId;
public String name;
public String address;
public Date createdDate;
//getter and setter methods
}
ファイル:Customer.hbm.xml
4. 春のもの
ビジネスロジックとデータベースインタラクションのためのSpringのBOおよびDAOクラス。
ファイル:CustomerBo.java
package com.example.customer.bo;
import java.util.List;
import com.example.customer.model.Customer;
public interface CustomerBo{
void addCustomer(Customer customer);
List findAllCustomer();
}
ファイル:CustomerBoImpl.java
package com.example.customer.bo.impl;
import java.util.List;
import com.example.customer.bo.CustomerBo;
import com.example.customer.dao.CustomerDao;
import com.example.customer.model.Customer;
public class CustomerBoImpl implements CustomerBo{
CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void addCustomer(Customer customer){
customerDao.addCustomer(customer);
}
public List findAllCustomer(){
return customerDao.findAllCustomer();
}
}
ファイル:CustomerDao.java
package com.example.customer.dao;
import java.util.List;
import com.example.customer.model.Customer;
public interface CustomerDao{
void addCustomer(Customer customer);
List findAllCustomer();
}
ファイル:CustomerDaoImpl.java
package com.example.customer.dao.impl;
import java.util.Date;
import java.util.List;
import com.example.customer.dao.CustomerDao;
import com.example.customer.model.Customer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class CustomerDaoImpl extends
HibernateDaoSupport implements CustomerDao{
public void addCustomer(Customer customer){
customer.setCreatedDate(new Date());
getHibernateTemplate().save(customer);
}
public List findAllCustomer(){
return getHibernateTemplate().find("from Customer");
}
}
ファイル:CustomerBean.xml
5. Spring +データベース
Springでデータベースの詳細を構成します。
ファイル:db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/exampledb jdbc.username=root jdbc.password=password
ファイル:DataSource.xml
WEB-INF/classes/config/database/db.properties
6. 春+休止状態
LocalSessionFactoryBeanを介してHibernateとSpringを統合します。
ファイル:HibernateSessionFactory.xml
7. JSF 2.0
JSFマネージドBeanは、SpringのBOを呼び出して、データベースから顧客のレコードを追加または取得します。
ファイル:CustomerBean.java
package com.example;
import java.io.Serializable;
import java.util.List;
import com.example.customer.bo.CustomerBo;
import com.example.customer.model.Customer;
public class CustomerBean implements Serializable{
//DI via Spring
CustomerBo customerBo;
public String name;
public String address;
//getter and setter methods
public void setCustomerBo(CustomerBo customerBo) {
this.customerBo = customerBo;
}
//get all customer data from database
public List getCustomerList(){
return customerBo.findAllCustomer();
}
//add a new customer data into database
public String addCustomer(){
Customer cust = new Customer();
cust.setName(getName());
cust.setAddress(getAddress());
customerBo.addCustomer(cust);
clearForm();
return "";
}
//clear form values
private void clearForm(){
setName("");
setAddress("");
}
}
h:dataTableを介して既存の顧客レコードを表示するJSFページと、ユーザーが新しい顧客レコードをデータベースに挿入できるようにするいくつかのテキストコンポーネント。
ファイル:default.xhtml
JSF 2.0 + Spring + Hibernate Example
Customer ID
#{c.customerId}
Name
#{c.name}
Address
#{c.address}
Created Date
#{c.createdDate}
Add New Customer
Name :
Address :
8. JSF 2.0 + Spring
JSF 2.0をSpringと統合します。詳細な説明はこちらをご覧ください–JSF 2.0 + Spring integration example
ファイル:applicationContext.xml
ファイル:faces-config.xml
org.springframework.web.jsf.el.SpringBeanFacesELResolver customer com.example.CustomerBean session customerBo #{customerBo}
ファイル:web.xml
JavaServerFaces org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener javax.faces.PROJECT_STAGE Development faces/default.xhtml Faces Servlet javax.faces.webapp.FacesServlet 1 Faces Servlet /faces/* Faces Servlet *.jsf Faces Servlet *.faces Faces Servlet *.xhtml
9. Demo
実行し、顧客データを入力して、「送信」ボタンをクリックします。


ソースコードをダウンロード
ダウンロード–JSF-2-Spring-Hibernate-Integration-Example.zip(19KB)