ssh框架整合最新版本.docx

上传人:b****8 文档编号:10279501 上传时间:2023-02-09 格式:DOCX 页数:17 大小:369.10KB
下载 相关 举报
ssh框架整合最新版本.docx_第1页
第1页 / 共17页
ssh框架整合最新版本.docx_第2页
第2页 / 共17页
ssh框架整合最新版本.docx_第3页
第3页 / 共17页
ssh框架整合最新版本.docx_第4页
第4页 / 共17页
ssh框架整合最新版本.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

ssh框架整合最新版本.docx

《ssh框架整合最新版本.docx》由会员分享,可在线阅读,更多相关《ssh框架整合最新版本.docx(17页珍藏版)》请在冰豆网上搜索。

ssh框架整合最新版本.docx

ssh框架整合最新版本

Struts2+Spring+Hibernate整合

在SSH2框架中,Spring充当了一个JavaBean容器的作用,Spring使用IOC(依赖注入)和AOP(面向切面编程)技术接管了Hibernate的DAO、事务和Struts的action对象及service业务层。

MVC分别使用:

(1)M层,Spring;

(2)V层,JSP;(3)C层,Struts;(4)持久层,Hibernate

我用的是myeclipse8.6,tomcat6.0.30,jdk6.0,和sqlServer2000版本的。

在开发工程中所用到的jar包如下图所示:

我是直接先引进去的。

来做一个示例讲解三者的整合:

一个登录的结构

1.新建数据库stuInfo

其中数据库中有一个表userInfo,字段如下图所示:

Id是唯一标识符,自动增长,userName和passWord是用户名和密码

2.开始准备整合工作

新建一个webProject项目

导入所需要的jar包,前面截图已经给出来了。

3.添加stucts2的支持

选择stucts2的目录放在src下,然后打开并增加下列代码进去

对于web.xml的修改我们放在最后三个框架全部整合好后进行修改

4.添加数据连接

如图所示:

这里我用的是jtds

下面我们在src下建立一个包,命名为com.hubu.entity,存放实体类

添加hibernate的支持

其中我们在最后一步是否选择createsessionFactory的时候取消

点击完成即可

然后我们将下列代码加进去

true

true

这样我们执行程序的时候会再控制台显示相应的hql查询

如下所示:

5.添加spring的支持

点击完成即可

这样就添加了Spring的支持

从上述代码中可以看到,Spring封装了Hibernate的操作,用Spring的方式接管了Hibernate的Session创建及销毁,创建SessionFactory的周边信息。

6.此时我们配置web.xml文件

将如下代码加到web.xml中

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

contextConfigLocation

/WEB-INF/applicationContext*.xml

org.springframework.web.context.ContextLoaderListener

lazyLoadingFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

flushMode

AUTO

lazyLoadingFilter

/*

《这些代码等你用的时间久了自然就会了解》这里不做太多的讲述

添加下列代码到stucts2中

struts.objectFactory的作用是使用Spring来管理struts2的action。

 

在application.cfg.xml中添加事务的管理,这里若是不添加在我们以后做curd操作的时候会是一直不停的打开着数据库操作,而没有执行关闭的操作

引入:

《beans》如下所示

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

aop="http:

//www.springframework.org/schema/aop"

xmlns:

tx="http:

//www.springframework.org/schema/tx"xmlns:

p="http:

//www.springframework.org/schema/p"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx.xsd

http:

//www.springframework.org/schema/aop

http:

//www.springframework.org/schema/aop/spring-aop.xsd">

--配置事务管理-->

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

adviceid="txAdvice"transaction-manager="transactionManager">

attributes>

methodname="save"propagation="REQUIRED"/>

methodname="delete*"propagation="REQUIRED"/>

methodname="update*"propagation="REQUIRED"/>

methodname="*"read-only="true"/>

attributes>

advice>

config>

pointcutid="allManagerMethod"

expression="execution(*com.hubu.service.*.*(..))"/>

advisoradvice-ref="txAdvice"pointcut-ref="allManagerMethod"/>

config>

7.此时我们算是基本完成了ssh框架的整合,我们来做一个测试,看看刚才我们的整合是不是正确的。

就以userInfo这个表为例来讲述他的curd操作

我们在src下建立如下所示的包

然分别在相应的包中放置相应的方法和类

逆向生成实体类:

其中实现功能的代码如下所示:

userDao接口

packagecom.hubu.dao;

importjava.util.List;

importcom.hubu.entity.UserInfo;

publicinterfaceuserInfoDao{

publicvoidsaveUserInfo(UserInfouserInfo);

publicvoiddeleteUserInfo(Longid);

publicvoidupdateUerInfo(UserInfouserInfo);

publicUserInfofindById(Longid);

publicListlistUserInfo();

}

userInfoDaoImpl类

packagecom.hubu.daoImpl;

importjava.util.List;

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

importcom.hubu.dao.userInfoDao;

importcom.hubu.entity.UserInfo;

publicclassUserInfoDaoImplextendsHibernateDaoSupportimplementsuserInfoDao{

publicvoiddeleteUserInfo(Longid){

//TODOAuto-generatedmethodstub

this.getHibernateTemplate().delete(this.getHibernateTemplate().get(UserInfo.class,id));

}

publicUserInfofindById(Longid){

//TODOAuto-generatedmethodstub

returnthis.getHibernateTemplate().get(UserInfo.class,id);

}

publicListlistUserInfo(){

//TODOAuto-generatedmethodstub

returnthis.getHibernateTemplate().find("fromUserInfo");

}

publicvoidsaveUserInfo(UserInfouserInfo){

//TODOAuto-generatedmethodstub

this.getHibernateTemplate().save(userInfo);

}

publicvoidupdateUerInfo(UserInfouserInfo){

//TODOAuto-generatedmethodstub

this.getHibernateTemplate().update(userInfo);

}

}

userService接口

packagecom.hubu.service;

importjava.util.List;

importcom.hubu.entity.UserInfo;

publicinterfaceUserInfoService{

publicvoidsaveUserInfo(UserInfouserInfo);

publicvoiddeleteUserInfo(Longid);

publicvoidupdateUerInfo(UserInfouserInfo);

publicUserInfofindById(Longid);

publicListlistUserInfo();

}

packagecom.hubu.serviceImpl;

importjava.util.List;

importcom.hubu.dao.UserInfoDao;

importcom.hubu.entity.UserInfo;

importcom.hubu.service.UserInfoService;

publicclassUserInfoServiceImplimplementsUserInfoService{

privateUserInfoDaouserInfoDao;

publicvoiddeleteUserInfo(Longid){

//TODOAuto-generatedmethodstub

this.getUserInfoDao().deleteUserInfo(id);

}

publicUserInfofindById(Longid){

//TODOAuto-generatedmethodstub

returnthis.getUserInfoDao().findById(id);

}

publicListlistUserInfo(){

//TODOAuto-generatedmethodstub

returnthis.getUserInfoDao().listUserInfo();

}

publicvoidsaveUserInfo(UserInfouserInfo){

//TODOAuto-generatedmethodstub

this.getUserInfoDao().saveUserInfo(userInfo);

}

publicvoidupdateUerInfo(UserInfouserInfo){

//TODOAuto-generatedmethodstub

}

publicvoidsetUserInfoDao(UserInfoDaouserInfoDao){

this.userInfoDao=userInfoDao;

}

publicUserInfoDaogetUserInfoDao(){

returnuserInfoDao;

}

}

对应的action类

packagecom.hubu.action;

importjava.util.List;

importcom.hubu.entity.UserInfo;

importcom.hubu.service.UserInfoService;

importcom.opensymphony.xwork2.ActionSupport;

publicclassUserInfoActionextendsActionSupport{

privateUserInfouserInfo;

privateUserInfoServiceuserInfoService;

privateLongid;

privateListlistAllInfos;

publicvoidsetUserInfo(UserInfouserInfo){

this.userInfo=userInfo;

}

publicUserInfogetUserInfo(){

returnuserInfo;

}

publicvoidsetUserInfoService(UserInfoServiceuserInfoService){

this.userInfoService=userInfoService;

}

publicUserInfoServicegetUserInfoService(){

returnuserInfoService;

}

publicvoidsetId(Longid){

this.id=id;

}

publicLonggetId(){

returnid;

}

publicvoidsetListAllInfos(ListlistAllInfos){

this.listAllInfos=listAllInfos;

}

publicListgetListAllInfos(){

returnlistAllInfos;

}

publicStringsaveUserInfo()throwsException{

try{

this.userInfoService.saveUserInfo(userInfo);

}catch(Exceptione){

//TODO:

handleexception

e.printStackTrace();

returnINPUT;

}

returnSUCCESS;

}

publicStringdeleteUserInfo()throwsException{

try{

this.userInfoService.deleteUserInfo(id);

}catch(Exceptione){

//TODO:

handleexception

e.printStackTrace();

returnINPUT;

}

returnSUCCESS;

}

publicStringfindById()throwsException{

try{

userInfo=this.userInfoService.findById(id);

}catch(Exceptione){

//TODO:

handleexception

e.printStackTrace();

returnINPUT;

}

returnSUCCESS;

}

publicStringlistAllUer()throwsException{

try{

listAllInfos=this.userInfoService.listUserInfo();

}catch(Exceptione){

//TODO:

handleexception

e.printStackTrace();

returnINPUT;

}

returnSUCCESS;

}

}

配置stucts2

listUserInfo.action

/error.jsp

listUserInfo.action

/error.jsp

listUserInfo.action

/error.jsp

/web/user/userModify.jsp

/error.jsp

/web/user/listUerInfo.jsp

/error.jsp

8.建立相应的jsp文件

添加页面

列出已经添加的信息

嗯,总的算是成功了。

无论布局如何,,若是有什么不对的地方,希望大家能给与指正,谢谢。

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

当前位置:首页 > 求职职场 > 简历

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

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