SSH整合流程
2014-03-02 23:19
344 查看
Struts2+Spring3.0+Hibernate3.3整合过程
一、 创建WEB工程,在工程中按照如下规范定义包结构
二、 按顺序添加struts2、spring、hibernate框架
1、 添加struts2框架
a、 将struts2框架所需要的jar文件(包括与spring整个插件jar包)配置到工程中;
b、 将struts2框架的配置文件模板复制粘贴到src中;
2、 添加spring框架
图-1
注意:选择的Spring Library包括(AOP、Core、Persistence Core、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
package com.mstanford.dao;
import java.io.Serializable;
import java.util.List;
/**
* 定义通用的持久化操作接口
*
* @author Qct 2013-7-1
*/
public interface IBaseDAO {
/**
* 保存对象
*
* @param object
*/
public void save(Object object);
/**
* 删除对象
*
* @param object
*/
public void delete(Object object);
/**
* 修改对象
*
* @param object
*/
public void update(Object object);
/**
* 根据对象标识获取指定类的对象
*
* @param clazz
* @param id
* @return
*/
public Object get(Class clazz, Serializable id);
/**
* 执行hql查询
*
* @param hql
* @return
*/
public List<Object> query(String hql,Object[]params);
/**
* 执行hql查询,带分页功能
*
* @param hql
* @param pageSize
* @param pageNo
* @return
*/
public List<Object> query(String hql,Object[]params, int pageSize, int pageNo);
/**
* 执行SQL(查询)
*
* @param sql
* @return
*/
public List<Object> find(String sql,Object[]params);
/**
* 执行SQL(insert、delete、update)
*
* @param sql
*/
public void execute(String sql, Object[] params);
/**
* 调用存储过程
*
* @param sql
* @param params
* @return
*/
public Object[] call(String sql, Object[] params);
}
2、 在com.mstanford.dao包中定义抽象类BaseDAO继承HibernateDAOSupport类并实现持久化通用接口IBaseDAO
package com.mstanford.dao;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDAO extends HibernateDaoSupport implements IBaseDAO{
public void call(String sql, Object[] params)throws SQLException {
Connection conn=this.getSession().connection();
CallableStatement stm=conn.prepareCall(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
stm.setObject(index, params[index]);
}
}
stm.execute();
stm.close();
stm.close();
}
public void delete(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(object);
}
public void execute(final String sql, final Object[] params)throws SQLException {
// TODO Auto-generated method stub
this.getHibernateTemplate().execute(new HibernateCallback() {
public Integer doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
SQLQuery sqlQuery=session.createSQLQuery(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
sqlQuery.setParameter(index, params[index]);
}
}
return sqlQuery.executeUpdate();
}
});
}
public List<Object> find(final String sql,final Object [] params) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
SQLQuery sqlQuery=session.createSQLQuery(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
sqlQuery.setParameter(index, params[index]);
}
}
return sqlQuery.list();
}
});
}
public Object get(Class clazz, Serializable id) {
return this.getHibernateTemplate().load(clazz, id);
}
public List<Object> query(final String hql,final Object [] params) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException,
SQLException {
Query query=session.createQuery(hql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
query.setParameter(index, params[index]);
}
}
return query.list();
}
});
}
public List<Object> query(final String hql,final Object [] params, final int pageSize,final int pageNo) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException,
SQLException {
Query query=session.createQuery(hql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
query.setParameter(index, params[index]);
}
}
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
return query.list();
}
});
}
public void save(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(object);
}
public void update(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(object);
}
}
3、 定义用于对用户操作的持久化类UserDAO,继承BaseDAO
package com.mstanford.dao;
public class UserDAO extends BaseDAO{
}
4、 在com.mstanford.service包中定义业务层类的接口UserService
package com.mstanford.service;
import com.mstanford.entity.User;
public interface UserService {
/**
* 用户登陆
* @param userName
* @param userPwd
* @return
*/
public User login(String userName,String userPwd);
}
5、 在com.mstanford.service.impl包中定义业务层类UserServiceImpl实现接口UserService
package com.mstanford.service.impl;
import java.util.List;
import com.mstanford.dao.UserDAO;
import com.mstanford.entity.User;
import com.mstanford.service.UserService;
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User login(String userName, String userPwd) {
String hql="from User where userName=? and userPwd=?";
Object [] params={userName,userPwd};
List<User> list=(List<User>)userDAO.query("from User where userName=? and userPwd=?", params);
if(list==null || list.size()<=0){
return null;
}
else{
return list.get(0);
}
}
}
6、 在com.mstanford.action包中定义业务控制器Action父类BaseAction;
package com.mstanford.action;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public abstract class BaseAction {
public Map<String, Object> getRequest(){
return (Map<String, Object>)ActionContext.getContext().get("request");
}
public Map<String, Object> getSession(){
return ActionContext.getContext().getSession();
}
public Map<String, Object> getApplication(){
return ActionContext.getContext().getApplication();
}
public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
}
7、 在com.mstanford.action包中定义业务控制器Action负责处理登陆请求的类UserAction类
package com.mstanford.action;
import com.mstanford.entity.User;
import com.mstanford.service.UserService;
public class UserAction extends BaseAction{
private UserService userService;
private User user;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login(){
User user=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à持久层DAOàSessionFactoryàDataSource
<bean id="userDAO" class="com.mstanford.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.mstanford.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<bean id="userAction" class="com.mstanford.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
9、 在struts.xml文件中配置业务控制器Action
10、 准备登陆页面index.jsp和err.jsp、suc.jsp页面
一、 创建WEB工程,在工程中按照如下规范定义包结构
二、 按顺序添加struts2、spring、hibernate框架
1、 添加struts2框架
a、 将struts2框架所需要的jar文件(包括与spring整个插件jar包)配置到工程中;
b、 将struts2框架的配置文件模板复制粘贴到src中;
2、 添加spring框架
图-1
注意:选择的Spring Library包括(AOP、Core、Persistence Core、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
package com.mstanford.dao;
import java.io.Serializable;
import java.util.List;
/**
* 定义通用的持久化操作接口
*
* @author Qct 2013-7-1
*/
public interface IBaseDAO {
/**
* 保存对象
*
* @param object
*/
public void save(Object object);
/**
* 删除对象
*
* @param object
*/
public void delete(Object object);
/**
* 修改对象
*
* @param object
*/
public void update(Object object);
/**
* 根据对象标识获取指定类的对象
*
* @param clazz
* @param id
* @return
*/
public Object get(Class clazz, Serializable id);
/**
* 执行hql查询
*
* @param hql
* @return
*/
public List<Object> query(String hql,Object[]params);
/**
* 执行hql查询,带分页功能
*
* @param hql
* @param pageSize
* @param pageNo
* @return
*/
public List<Object> query(String hql,Object[]params, int pageSize, int pageNo);
/**
* 执行SQL(查询)
*
* @param sql
* @return
*/
public List<Object> find(String sql,Object[]params);
/**
* 执行SQL(insert、delete、update)
*
* @param sql
*/
public void execute(String sql, Object[] params);
/**
* 调用存储过程
*
* @param sql
* @param params
* @return
*/
public Object[] call(String sql, Object[] params);
}
2、 在com.mstanford.dao包中定义抽象类BaseDAO继承HibernateDAOSupport类并实现持久化通用接口IBaseDAO
package com.mstanford.dao;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDAO extends HibernateDaoSupport implements IBaseDAO{
public void call(String sql, Object[] params)throws SQLException {
Connection conn=this.getSession().connection();
CallableStatement stm=conn.prepareCall(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
stm.setObject(index, params[index]);
}
}
stm.execute();
stm.close();
stm.close();
}
public void delete(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(object);
}
public void execute(final String sql, final Object[] params)throws SQLException {
// TODO Auto-generated method stub
this.getHibernateTemplate().execute(new HibernateCallback() {
public Integer doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
SQLQuery sqlQuery=session.createSQLQuery(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
sqlQuery.setParameter(index, params[index]);
}
}
return sqlQuery.executeUpdate();
}
});
}
public List<Object> find(final String sql,final Object [] params) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
SQLQuery sqlQuery=session.createSQLQuery(sql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
sqlQuery.setParameter(index, params[index]);
}
}
return sqlQuery.list();
}
});
}
public Object get(Class clazz, Serializable id) {
return this.getHibernateTemplate().load(clazz, id);
}
public List<Object> query(final String hql,final Object [] params) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException,
SQLException {
Query query=session.createQuery(hql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
query.setParameter(index, params[index]);
}
}
return query.list();
}
});
}
public List<Object> query(final String hql,final Object [] params, final int pageSize,final int pageNo) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException,
SQLException {
Query query=session.createQuery(hql);
if(params!=null && params.length>0){
for(int index=0;index<params.length;index++){
query.setParameter(index, params[index]);
}
}
query.setFirstResult((pageNo-1)*pageSize);
query.setMaxResults(pageSize);
return query.list();
}
});
}
public void save(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(object);
}
public void update(Object object) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(object);
}
}
3、 定义用于对用户操作的持久化类UserDAO,继承BaseDAO
package com.mstanford.dao;
public class UserDAO extends BaseDAO{
}
4、 在com.mstanford.service包中定义业务层类的接口UserService
package com.mstanford.service;
import com.mstanford.entity.User;
public interface UserService {
/**
* 用户登陆
* @param userName
* @param userPwd
* @return
*/
public User login(String userName,String userPwd);
}
5、 在com.mstanford.service.impl包中定义业务层类UserServiceImpl实现接口UserService
package com.mstanford.service.impl;
import java.util.List;
import com.mstanford.dao.UserDAO;
import com.mstanford.entity.User;
import com.mstanford.service.UserService;
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User login(String userName, String userPwd) {
String hql="from User where userName=? and userPwd=?";
Object [] params={userName,userPwd};
List<User> list=(List<User>)userDAO.query("from User where userName=? and userPwd=?", params);
if(list==null || list.size()<=0){
return null;
}
else{
return list.get(0);
}
}
}
6、 在com.mstanford.action包中定义业务控制器Action父类BaseAction;
package com.mstanford.action;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public abstract class BaseAction {
public Map<String, Object> getRequest(){
return (Map<String, Object>)ActionContext.getContext().get("request");
}
public Map<String, Object> getSession(){
return ActionContext.getContext().getSession();
}
public Map<String, Object> getApplication(){
return ActionContext.getContext().getApplication();
}
public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
}
7、 在com.mstanford.action包中定义业务控制器Action负责处理登陆请求的类UserAction类
package com.mstanford.action;
import com.mstanford.entity.User;
import com.mstanford.service.UserService;
public class UserAction extends BaseAction{
private UserService userService;
private User user;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login(){
User user=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à持久层DAOàSessionFactoryàDataSource
<bean id="userDAO" class="com.mstanford.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.mstanford.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<bean id="userAction" class="com.mstanford.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
9、 在struts.xml文件中配置业务控制器Action
10、 准备登陆页面index.jsp和err.jsp、suc.jsp页面
相关文章推荐
- SSH搭建流程以及实现增删查改(整合)
- 通过Axis整合SSH 项目创建WebService的流程步骤
- Spring+Struts2+Hibernate(ssh)三大框架整合流程
- SSH(Struts2,Spring,Hibernate)整合开发流程
- SSH-整合流程!
- Shiro整合SSH开发2:结合Struts2实现登陆和退出以及Shiro执行流程和原理解析
- java-ssh整合之后的运行逻辑(调用流程)
- JavaWeb框架-SSH-整合流程!
- SSH整合后的流程简述
- SSH三大框架整合的流程是怎样走的,spring在三大框架中式怎么的一个流程,hibernate是怎么的一个流程,struts2是怎么的一个流程的,
- SSH项目搭建图文流程,附整合jar包下载地址
- SSH整合流程
- SSH大概整合步骤
- ssh整合---简单的登录判断例子
- activiti工作流的web流程设计器整合视频教程 SSM和独立部署
- SSH整合JBPM4.4
- activiti自定义流程之Spring整合activiti-modeler实例(六):启动流程
- SSH三大框架整合小案例
- SSH整合时执行hibernate查询报错:java.lang.ClassCastException: com.ch.hibernate.Depart
- 【整合篇】Activiti业务与流程整合之查询(三)