Struts2+Spring+Hibernate整合流程.docx
《Struts2+Spring+Hibernate整合流程.docx》由会员分享,可在线阅读,更多相关《Struts2+Spring+Hibernate整合流程.docx(21页珍藏版)》请在冰豆网上搜索。
Struts2+Spring+Hibernate整合流程
Struts2+Spring3.0+Hibernate3.3整合过程
(老夫呕心沥血制作,如果要传播,请一定联系你翔哥!
翻版必究!
)
一、创建WEB工程,在工程中按照如下规范定义包结构
二、按顺序添加struts2、spring、hibernate框架
1、添加struts2框架
a、将struts2框架所需要的jar文件(包括与spring整个插件jar包)配置到工程中;
b、将struts2框架的配置文件模板复制粘贴到src中;
2、添加spring框架
图-1
注意:
选择的SpringLibrary包括(AOP、Core、PersistenceCore、Web);
图-2
3、添加hibernate框架
图-1
图-2
图-3
图-4
图-5
通过以上5个步骤,已经在当前项目中添加SSH框架,但是发现spring配置文件applicationContext.xml文件出错
,因为在SSH整合中使用到了数据源DataSource,缺少对象的jar包
;找到对应的jar包配置到当前项目中即可;
三、配置SSH框架
1、web.xml文件中配置struts2核心控制器StrutsPrepareAndExecuteFilter(注:
在添加struts1框架时,工具会自动配置struts1核心控制器);
2、在web.xml文件中配置用于加载Spring配置文件的上下文加载监听器
3、在struts.xml文件中配置常量(设置字符集编码和业务控制器的创建管理方式)
4、通过工具生成数据库表对应的实体类和映射文件
注意:
生成完毕后,实体类和hbm映射文件不一定位于com.mstanford.entity和com.mstanford.hbm包中,需要手动修改(包括在*.hbm.xml文件中类路径和applicationContext文件指定的映射文件路径)
四、完成用户登陆功能
1、在com.mstanford.dao包中定义实现持久化的接口IBaseDAO
packagecom.mstanford.dao;
importjava.io.Serializable;
importjava.util.List;
/**
*定义通用的持久化操作接口
*
*@authorQct2013-7-1
*/
publicinterfaceIBaseDAO{
/**
*保存对象
*
*@paramobject
*/
publicvoidsave(Objectobject);
/**
*删除对象
*
*@paramobject
*/
publicvoiddelete(Objectobject);
/**
*修改对象
*
*@paramobject
*/
publicvoidupdate(Objectobject);
/**
*根据对象标识获取指定类的对象
*
*@paramclazz
*@paramid
*@return
*/
publicObjectget(Classclazz,Serializableid);
/**
*执行hql查询
*
*@paramhql
*@return
*/
publicList
/**
*执行hql查询,带分页功能
*
*@paramhql
*@parampageSize
*@parampageNo
*@return
*/
publicList
/**
*执行SQL(查询)
*
*@paramsql
*@return
*/
publicList
/**
*执行SQL(insert、delete、update)
*
*@paramsql
*/
publicvoidexecute(Stringsql,Object[]params);
/**
*调用存储过程
*
*@paramsql
*@paramparams
*@return
*/
publicObject[]call(Stringsql,Object[]params);
}
2、在com.mstanford.dao包中定义抽象类BaseDAO继承HibernateDAOSupport类并实现持久化通用接口IBaseDAO
packagecom.mstanford.dao;
importjava.io.Serializable;
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.SQLException;
importjava.util.List;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.SQLQuery;
importorg.hibernate.Session;
importorg.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
publicclassBaseDAOextendsHibernateDaoSupportimplementsIBaseDAO{
publicvoidcall(Stringsql,Object[]params)throwsSQLException{
Connectionconn=this.getSession().connection();
CallableStatementstm=conn.prepareCall(sql);
if(params!
=null&¶ms.length>0){
for(intindex=0;indexstm.setObject(index,params[index]);
}
}
stm.execute();
stm.close();
stm.close();
}
publicvoiddelete(Objectobject){
//TODOAuto-generatedmethodstub
this.getHibernateTemplate().delete(object);
}
publicvoidexecute(finalStringsql,finalObject[]params)throwsSQLException{
//TODOAuto-generatedmethodstub
this.getHibernateTemplate().execute(newHibernateCallback(){
publicIntegerdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
//TODOAuto-generatedmethodstub
SQLQuerysqlQuery=session.createSQLQuery(sql);
if(params!
=null&¶ms.length>0){
for(intindex=0;indexsqlQuery.setParameter(index,params[index]);
}
}
returnsqlQuery.executeUpdate();
}
});
}
publicList
//TODOAuto-generatedmethodstub
returnthis.getHibernateTemplate().executeFind(newHibernateCallback(){
publicListdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
//TODOAuto-generatedmethodstub
SQLQuerysqlQuery=session.createSQLQuery(sql);
if(params!
=null&¶ms.length>0){
for(intindex=0;indexsqlQuery.setParameter(index,params[index]);
}
}
returnsqlQuery.list();
}
});
}
publicObjectget(Classclazz,Serializableid){
returnthis.getHibernateTemplate().load(clazz,id);
}
publicList
//TODOAuto-generatedmethodstub
returnthis.getHibernateTemplate().executeFind(newHibernateCallback(){
publicListdoInHibernate(Sessionsession)throwsHibernateException,
SQLException{
Queryquery=session.createQuery(hql);
if(params!
=null&¶ms.length>0){
for(intindex=0;indexquery.setParameter(index,params[index]);
}
}
returnquery.list();
}
});
}
publicList
//TODOAuto-generatedmethodstub
returnthis.getHibernateTemplate().executeFind(newHibernateCallback(){
publicListdoInHibernate(Sessionsession)throwsHibernateException,
SQLException{
Queryquery=session.createQuery(hql);
if(params!
=null&¶ms.length>0){
for(intindex=0;indexquery.setParameter(index,params[index]);
}
}
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
returnquery.list();
}
});
}
publicvoidsave(Objectobject){
//TODOAuto-generatedmethodstub
this.getHibernateTemplate().save(object);
}
publicvoidupdate(Objectobject){
//TODOAuto-generatedmethodstub
this.getHibernateTemplate().update(object);
}
}
3、定义用于对用户操作的持久化类UserDAO,继承BaseDAO
packagecom.mstanford.dao;
publicclassUserDAOextendsBaseDAO{
}
4、在com.mstanford.service包中定义业务层类的接口UserService
packagecom.mstanford.service;
importcom.mstanford.entity.User;
publicinterfaceUserService{
/**
*用户登陆
*@paramuserName
*@paramuserPwd
*@return
*/
publicUserlogin(StringuserName,StringuserPwd);
}
5、在com.mstanford.service.impl包中定义业务层类UserServiceImpl实现接口UserService
packagecom.mstanford.service.impl;
importjava.util.List;
importcom.mstanford.dao.UserDAO;
importcom.mstanford.entity.User;
importcom.mstanford.service.UserService;
publicclassUserServiceImplimplementsUserService{
privateUserDAOuserDAO;
publicUserDAOgetUserDAO(){
returnuserDAO;
}
publicvoidsetUserDAO(UserDAOuserDAO){
this.userDAO=userDAO;
}
publicUserlogin(StringuserName,StringuserPwd){
Stringhql="fromUserwhereuserName=?
anduserPwd=?
";
Object[]params={userName,userPwd};
Listlist=(List)userDAO.query("fromUserwhereuserName=?
anduserPwd=?
",params);
if(list==null||list.size()<=0){
returnnull;
}
else{
returnlist.get(0);
}
}
}
6、在com.mstanford.action包中定义业务控制器Action父类BaseAction;
packagecom.mstanford.action;
importjava.util.Map;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionContext;
publicabstractclassBaseAction{
publicMapgetRequest(){
return(Map)ActionContext.getContext().get("request");
}
publicMapgetSession(){
returnActionContext.getContext().getSession();
}
publicMapgetApplication(){
returnActionContext.getContext().getApplication();
}
publicHttpServletResponsegetResponse(){
returnServletActionContext.getResponse();
}
}
7、在com.mstanford.action包中定义业务控制器Action负责处理登陆请求的类UserAction类
packagecom.mstanford.action;
importcom.mstanford.entity.User;
importcom.mstanford.service.UserService;
publicclassUserActionextendsBaseAction{
privateUserServiceuserService;
privateUseruser;
publicUserServicegetUserService(){
returnuserService;
}
publicvoidsetUserService(UserServiceuserService){
this.userService=userService;
}
publicUsergetUser(){
returnuser;
}
publicvoidsetUser(Useruser){
this.user=user;
}
publicStringlogin(){
Useruser=userService.login(this.user.getUserName(),this.user.getUserPwd());
if(user==null){
return"err";
}
else{
//保存当前登陆的用户对象
this.getSession().put("user",user);
return"suc";
}
}
}
8、在applicationContext.xml文件中配置受管理的Bean(包括Dao、Service、Action)
注:
程序中各层的依赖关系如下
struts业务控制器Action业务层Service持久层DAOSessionFactoryDataSource
9、在struts.xml文件中配置业务控制器Action
10、准备登陆页面index.jsp和err.jsp、suc.jsp页面