Struts 2 + Spring + Hibernate 整合实例图文并茂.docx

上传人:b****6 文档编号:8053031 上传时间:2023-05-12 格式:DOCX 页数:19 大小:110.13KB
下载 相关 举报
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第1页
第1页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第2页
第2页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第3页
第3页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第4页
第4页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第5页
第5页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第6页
第6页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第7页
第7页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第8页
第8页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第9页
第9页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第10页
第10页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第11页
第11页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第12页
第12页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第13页
第13页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第14页
第14页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第15页
第15页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第16页
第16页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第17页
第17页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第18页
第18页 / 共19页
Struts 2 + Spring + Hibernate 整合实例图文并茂.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Struts 2 + Spring + Hibernate 整合实例图文并茂.docx

《Struts 2 + Spring + Hibernate 整合实例图文并茂.docx》由会员分享,可在线阅读,更多相关《Struts 2 + Spring + Hibernate 整合实例图文并茂.docx(19页珍藏版)》请在冰点文库上搜索。

Struts 2 + Spring + Hibernate 整合实例图文并茂.docx

Struts2+Spring+Hibernate整合实例图文并茂

Struts2+Spring+Hibernate整合实例

Seethesummaryofintegrationsteps:

1.Getallthedependencylibraries(alot).

2.RegisterSpring’sContextLoaderListenertointegrateStruts2andSpring.

3.UseSpring’sLocalSessionFactoryBeantointegrateSpringandHibernate.

4.Done,allconnected.

Seetherelationship:

Struts2<--(ContextLoaderListener)-->Spring<--(LocalSessionFactoryBean)-->Hibernate

Thiswillbeaverylongtutorialwithlittleexplanation,makesureyouchecktheabove3articlesfordetailsexplanation.

TutorialsStart…

Itwillgoingtocreateacustomerpage,withaddcustomerandlistcustomerfunction.FrontendisusingStruts2todisplay,Springasthedependencyinjectionengine,andHibernatetodoingthedatabaseoperation.Letstart…

1.Projectstructure

Projectfolderstructure.

2.MySQLtablescript

Customer’stablescript.

DROPTABLEIFEXISTS`mkyong`.`customer`;

CREATETABLE`mkyong`.`customer`(

`CUSTOMER_ID`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,

`NAME`VARCHAR(45)NOTNULL,

`ADDRESS`VARCHAR(255)NOTNULL,

`CREATED_DATE`datetimeNOTNULL,

PRIMARYKEY(`CUSTOMER_ID`)

)ENGINE=InnoDBAUTO_INCREMENT=17DEFAULTCHARSET=utf8;

3.Dependencylibraries

Thistutorialsrequestmanydependencylibraries.

Struts2…

--Struts2-->

org.apache.struts

struts2-core

2.1.8

--Struts2+Springplugins-->

org.apache.struts

struts2-spring-plugin

2.1.8

MySQL…

--MySQLdatabasedriver-->

mysql

mysql-connector-java

5.1.9

Spring…

--Springframework-->

org.springframework

spring

2.5.6

org.springframework

spring-web

2.5.6

Hibernate…

--Hibernatecore-->

org.hibernate

hibernate

3.2.7.ga

 

--Hibernatecorelibrarydependencystart-->

dom4j

dom4j

1.6.1

 

commons-logging

commons-logging

1.1.1

 

commons-collections

commons-collections

3.2.1

 

cglib

cglib

2.2

--Hibernatecorelibrarydependencyend-->

 

--Hibernatequerylibrarydependencystart-->

antlr

antlr

2.7.7

--Hibernatequerylibrarydependencyend-->

4.Hibernate…

Onlythemodelandmappingfilesarerequired,becauseSpringwillhandletheHibernateconfiguration.

Customer.java–Createaclassforcustomertable.

packagecom.mkyong.customer.model;

 

importjava.util.Date;

 

publicclassCustomerimplementsjava.io.Serializable{

 

privateLongcustomerId;

privateStringname;

privateStringaddress;

privateDatecreatedDate;

 

//getterandsettermethods

}

Customer.hbm.xml–Hibernatemappingfileforcustomer.

xmlversion="1.0"?

>

DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"

--Generated20Julai201011:

40:

18AMbyHibernateTools3.2.5.Beta-->

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"?

>

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 其它

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2