Hibernate与Struts2和Spring组合.docx

上传人:b****6 文档编号:5900415 上传时间:2023-01-02 格式:DOCX 页数:11 大小:405.79KB
下载 相关 举报
Hibernate与Struts2和Spring组合.docx_第1页
第1页 / 共11页
Hibernate与Struts2和Spring组合.docx_第2页
第2页 / 共11页
Hibernate与Struts2和Spring组合.docx_第3页
第3页 / 共11页
Hibernate与Struts2和Spring组合.docx_第4页
第4页 / 共11页
Hibernate与Struts2和Spring组合.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

Hibernate与Struts2和Spring组合.docx

《Hibernate与Struts2和Spring组合.docx》由会员分享,可在线阅读,更多相关《Hibernate与Struts2和Spring组合.docx(11页珍藏版)》请在冰豆网上搜索。

Hibernate与Struts2和Spring组合.docx

Hibernate与Struts2和Spring组合

1.1组合Hibernate与Spring

1.在Eclipse中,新建一个Webproject。

2.给该项目增加Hibernate开发能力,增加Hibernate相关类库到当前项目的BuildPath,同时也提供了hibernate.cfg.xml这个配置文件。

3.给该项目增加Spring开发能力,增加spring相关类库到当前项目的BuildPath,同时也提供了applicationContext.xml文件。

注意:

最好把applicationContext.xml文件保存到当前项目的WebRoot/WEB-INF的根目录下。

4.删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加sessionFactory和dataSource的设置。

修改的内容如下所示:

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/beanshttp:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd">

class="mons.dbcp.BasicDataSource">

value="oracle.jdbc.driver.OracleDriver">

value="jdbc:

oracle:

thin:

@localhost:

1521:

ora9">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

org.hibernate.dialect.Oracle9Dialect

cn/com/jobedu/shop/model/Catalog.hbm.xml

5.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。

6.修改applicationContext.xml文件中元素的内容。

如下所示:

cn/com/jobedu/shop/model/Catalog.hbm.xml

cn/com/jobedu/shop/model/Product.hbm.xml

7.编写DAO接口。

package.jobedu.shop.dao;

importjava.util.List;

import.jobedu.shop.model.Catalog;

publicinterfaceCatalogDao{

publicvoidcreate(Catalogc);

publicCataloggetCatalog(Longid);

publicListgetCatalogs();

publicvoidupdate(Catalogc);

publicvoidremove(Longid);

}

8.编写DAO接口的实现类,实现类使用Spring提供的帮助类。

示意如下:

package.jobedu.shop.dao.hibernate;

importjava.util.List;

importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

import.jobedu.shop.dao.CatalogDao;

import.jobedu.shop.model.Catalog;

publicclassCatalogDaoHibernateextendsHibernateDaoSupportimplementsCatalogDao{

@Override

publicvoidcreate(Catalogc){

//HibernateTemplatetemplate=getHibernateTemplate();

//template.save(c);

getHibernateTemplate().save(c);

}

@Override

publicCataloggetCatalog(Longid){

return(Catalog)getHibernateTemplate().get(Catalog.class,id);

}

@Override

publicListgetCatalogs(){

returngetHibernateTemplate().find("fromCatalogorderbyiddesc");

}

@Override

publicvoidremove(Longid){

//Catalogc=this.getCatalog(id);

//getHibernateTemplate().delete(c);

getHibernateTemplate().delete(getCatalog(id));

}

@Override

publicvoidupdate(Catalogc){

getHibernateTemplate().update(c);

}

}

9.修改applicationContext.xml文件,增加对Dao实现类的配置。

class=".jobedu.shop.dao.hibernate.CatalogDaoHibernate">

1.2在组合Struts2,完成Struts2、Spring和Hibernate的三者组合

1.修改web.xml文件,增加struts2的所需要的过滤器配置。

struts2

org.apache.struts2.dispatcher.FilterDispatcher

struts2

/*

1.增加Struts2相应类库,增加struts2-spring-plugin-x-x-x.jar文件。

(Struts的压缩包中,Struts所提供的。

2.需要拷贝struts.xml文件到src跟目录下,再修改struts.xml文件,进行常量配置。

3.修改web.xml文件,配置Spring监听器和上下文变量。

contextConfigLocation

/WEB-INF/applicationContext*.xml

org.springframework.web.context.ContextLoaderListener

4.编写Action类。

5.配置Struts.xml文件。

struts.xml

DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

"http:

//struts.apache.org/dtds/struts-2.0.dtd">

hello.jsp

....

6.修改applicationConext.xml,默认情况下,Spring从下面的文件中寻找为action所做的配置。

xmlversion="1.0"encoding="UTF-8"?

>

...

7.编写所需要的JSP文件。

8.部署,调试整个项目。

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

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1