table="customer"catalog="mkyong">
5.Struts2…
ImplementstheBoandDAOdesignpattern.AlltheBoandDAOwillbeDIbySpringintheSpringbeanconfigurationfile.IntheDAO,makeitextendsSpring’sHibernateDaoSupporttointegrateSpringandHibernateintegration.
CustomerBo.java
packagecom.mkyong.customer.bo;
importjava.util.List;
importcom.mkyong.customer.model.Customer;
publicinterfaceCustomerBo{
voidaddCustomer(Customercustomer);
ListlistCustomer();
}
CustomerBoImpl.java
packagecom.mkyong.customer.bo.impl;
importjava.util.List;
importcom.mkyong.customer.bo.CustomerBo;
importcom.mkyong.customer.dao.CustomerDAO;
importcom.mkyong.customer.model.Customer;
publicclassCustomerBoImplimplementsCustomerBo{
CustomerDAOcustomerDAO;
//DIviaSpring
publicvoidsetCustomerDAO(CustomerDAOcustomerDAO){
this.customerDAO=customerDAO;
}
//callDAOtosavecustomer
publicvoidaddCustomer(Customercustomer){
customerDAO.addCustomer(customer);
}
//callDAOtoreturncustomers
publicListlistCustomer(){
returncustomerDAO.listCustomer();
}
}
CustomerDAO.java
packagecom.mkyong.customer.dao;
importjava.util.List;
importcom.mkyong.customer.model.Customer;
publicinterfaceCustomerDAO{
voidaddCustomer(Customercustomer);
ListlistCustomer();
}
CustomerDAOImpl.java
packagecom.mkyong.customer.dao.impl;
importjava.util.List;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importcom.mkyong.customer.dao.CustomerDAO;
importcom.mkyong.customer.model.Customer;
publicclassCustomerDAOImplextendsHibernateDaoSupport
implementsCustomerDAO{
//addthecustomer
publicvoidaddCustomer(Customercustomer){
getHibernateTemplate().save(customer);
}
//returnallthecustomersinlist
publicListlistCustomer(){
returngetHibernateTemplate().find("fromCustomer");
}
}
CustomerAction.java–TheStruts2actionisnolongerneedtoextendstheActionSupport,Springwillhandleit.
packagecom.mkyong.customer.action;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importcom.mkyong.customer.bo.CustomerBo;
importcom.mkyong.customer.model.Customer;
importcom.opensymphony.xwork2.ModelDriven;
publicclassCustomerActionimplementsModelDriven{
Customercustomer=newCustomer();
ListcustomerList=newArrayList();
CustomerBocustomerBo;
//DIviaSpring
publicvoidsetCustomerBo(CustomerBocustomerBo){
this.customerBo=customerBo;
}
publicObjectgetModel(){
returncustomer;
}
publicListgetCustomerList(){
returncustomerList;
}
publicvoidsetCustomerList(ListcustomerList){
this.customerList=customerList;
}
//savecustomer
publicStringaddCustomer()throwsException{
//saveit
customer.setCreatedDate(newDate());
customerBo.addCustomer(customer);
//reloadthecustomerlist
customerList=null;
customerList=customerBo.listCustomer();
return"success";
}
//listallcustomers
publicStringlistCustomer()throwsException{
customerList=customerBo.listCustomer();
return"success";
}
}
6.Spring…
Almostalltheconfigurationisdonehere,atall,Springisspecializedinintegrationwork:
).
CustomerBean.xml–DeclaretheSpring’sbeans:
Action,BOandDAO.
xmlversion="1.0"encoding="UTF-8"?
>
//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.properties–Declarethedatabasedetails.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:
mysql:
//localhost:
3306/mkyong
jdbc.username=root
jdbc.password=password
DataSource.xml–Createadatasourcebean.
//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">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
WEB-INF/classes/config/database/properties/database.properties
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
HibernateSessionFactory.xml–CreateasessionFactorybeantointegrateSpringandHibernate.
xmlversion="1.0"encoding="UTF-8"?
>