SSH整合 Spring Struts Hibernate三大框架

Struts、Spring、Hibernate整合

声明:本人刚学习SSH框架,如有不足之处,请大家谅解,提出,谢谢

本人所用架包:http://pan.baidu.com/s/1bp1VBx9

第一步:创建项目

第二步:搭建Struts框架

第三步:整合ss

第四步:插入Hibernate框架

第五步:完成SSH整合

1.创建JAVA项目

用eclipse(开发工具)创建Wwb项目,注意生成web.xml文件。

2.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <!-- 配置spring的监听器 -->  
	<listener>  
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
	</listener>  
	<context-param>  
		<param-name>contextConfigLocation</param-name>  
		<!-- application前加‘/’则在src根目录查询,不加则在Web根目录下查询-->
		<param-value>classpath:/applicationContext*.xml</param-value>  
	</context-param> 
		
  	<!-- 配置Struts2的核心过滤器 -->  
	<filter>
		<filter-name>struts2</filter-name>
			<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
			</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
			<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>

这里注意:<param-value>classpath:/applicationContext*.xml</param-value>里

applicationContext*.xml前面的‘/’代表文件在Java文件中寻找,如果去掉‘/’则默认在Web根目录下寻找!!

3.配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!--struts.devMode : 是否设置为开发模式 -->
	<constant name="struts.devMode" value="true" />
	
	<!-- namespace :对应与项目名称后面的"/" -->
	<package name="front" namespace="/" extends="struts-default">
		<action name="login" class="loginAction" method="login">
			<result name="success">/index.jsp</result>
		</action>		
	</package>
</struts>

4.整合SS

先创建一个applicationContext.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
		<!--将针对注解的处理器配置好  -->   
	 	<bean id="loginAction" class="com.mwl.action.LoginAction"/>
</beans>

确认‘beanid’与struts.xml里class的值一致后,再创建LoginAction文件:

package com.mwl.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{	
	public String login() throws Exception {

		return SUCCESS;
	}

最后创建index.jsp文件,输入内容“Helloword”

运行JAVA文件,如网页显示Helloword,则SS整合框架,搭建成功。

这里注意:String与Struts整合必须用到struts2-spring-plugin-2.3.4.jar架包,否则无法成功整合。

5.配置hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 链接数据库  -->  
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sshTest</property>
		<!-- 数据库驱动-->  
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 数据库账号密码 --> 
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!-- 根据schema更新数据表的工具 -->  
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 是否显示SQL --> 
		<property name="hibernate.show_sql">true</property>
		<!-- 数据表映射配置文件 -->  
		<mapping resource="com/mwl/model/Person.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

同时创建Person类,配置相应的映射文件Person.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.mwl.model">
	<class name="Person" table="person">
		<id name="id" column="id">
			<!-- 自动增加-->
			<generator class="increment"></generator>
		</id>
		
		<property name="name"/>
		<property name="pwd"/>
	</class>
</hibernate-mapping>

6.根据项目流程Action-Service-Dao完善applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
		<!--将针对注解的处理器配置好  -->   
	 	<bean id="loginAction" class="com.mwl.action.LoginAction">
	 		<property name="loginService" ref="loginService"/>
	 	</bean>
	 	<bean id="loginService" class="com.mwl.service.LoginServiceImpl">
	 		<property name="loginDao" ref="loginDao"/>
	 	</bean>
	 	<bean id="loginDao" class="com.mwl.dao.LoginDaoImpl">
	 		<property name="sessionFactory" ref="sessionFactory"/>
	 	</bean>
	 	
	 	 <!-- 配置使用基于Hibernate的事务管理器 -->
		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
		</bean>

</beans>

7.根据applicationContext.xml的内容,依次创建Class类与其相关接口,内容如下:

package com.mwl.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mwl.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	String name;
	String pwd;
	
	private LoginService loginService;
	
	public String login() throws Exception {
		//接受数据
		String notice =loginService.login(name, pwd);
		HttpServletRequest request=ServletActionContext.getRequest();
		//传输到页面
		request.setAttribute("notice", notice);
		return SUCCESS;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}

根据LoginAction类,创建接口LoginService

package com.mwl.service;

import com.mwl.model.Person;

public interface LoginService {
	 public String login(String username,String password);
}

接着创捷其实现类:

package com.mwl.service;

import com.mwl.dao.LoginDao;
import com.mwl.model.Person;

public class LoginServiceImpl implements LoginService{
	private LoginDao loginDao;
	@Override
	public String login(String username, String password) {
		Person p =new Person();
		p.setName(username);
		p.setPwd(password);
		loginDao.save(p);
		return "登陆成功";
	}
	public LoginDao getLoginDao() {
		return loginDao;
	}
	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}
}

根据LoginServiceImpl类,创建接口LoginDao

package com.mwl.dao;

import com.mwl.model.Person;

public interface LoginDao {
	public void save(Person p);
}

接着创建LoginDao的实现类LoginDaoImpl

package com.mwl.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.mwl.model.Person;

public class LoginDaoImpl implements LoginDao{
	SessionFactory  sessionFactory;
	
	@Override
	public void save(Person p) {
		Session session=sessionFactory.openSession();
		session.beginTransaction().begin();

		Person a=new Person();
		a.setName("222");
		a.setPwd("123");
		
		session.save(a);
		
		session.beginTransaction().commit();
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	
}

最后创建数据库sshTest,并创建检测类检查代码。

运行Tomcat,输入localhost

如果数据库sshTest里自动生成表格person,并给name,pwd分别赋值222,123则SSH框架整合搭建成功!!!!