SSH项目整合详细介绍

SSH整合

1.jar包整合

 Struts所需要的jar包有(我使用的是2.3.15.3):

  

Spring所需要的jar包有(我使用的是3.2.0):

首先是基础jar包:beans、core、context、expression、commons-logging(这个包在Struts中已经导入)

AOP编程所需要的jar包:aopalliance、spring-aop、aspectj、spring-aspects

数据库需要的jar包:spring-jdbc、spring-tx

数据库驱动:mysql-connector-java-5.1.5-bin.jar

连接池:c3p0(采用c3p0连接池,也可以导入其他连接池比如说:dbcp但是用dbcp需要导入连个jar包:

com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar【dbcp连接池的jar包】、

com.springsource.org.apache.commons.pool-1.5.3.jar【dbcp依赖包】)

测试所需要jar包:Spring-test(如果不需要进行测试可以不导入)

web开发所需要的jar包:spring-web

Spring整合hibernate所需要的jar包:spring-orm

Hibernate所需要的jar包(采用的是3.6.10)

Hibernate核心包:hibernate3.jar

Hibernate必须jar包:antlr、commons-collection、dom4j、jta、slf4j、javassist(这个包在struts中已经导入,就不需要导入)

整合log4j包:slf4j-log4j

二级缓存包:ehcahe

二级缓存依赖包:backport-util-concurrent.jar、commons-logging(这个包已经导入。就不再导入)

最后还差一个struts整合Spring的包:

struts2-spring-plugin-2.3.15.3.jar


项目导入的jar包截图:


2.Spring整合Hibernate(整合hibernate时有hibernate.cfg.xml配置文件)

  2.1PO类设计:

 javabean:

public class User {
	private Integer id;
	private String username;
	private String password;
	private Integer age;
       //省略geter和setter方法
}
 映射文件:
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.lc.domain.User" table="user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="username"></property>
		<property name="password"></property>
		<property name="age"></property>
	</class>

</hibernate-mapping> 

2.2dao层设计:

dao层采用Spring提供的HibernateTemplate方式来操作PO对象。通过dao层实现类继承HibernateDaoSupport方法来获得HibernateTemplate

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao{
	
	@Override
	public void save(User user) {
		this.getHibernateTemplate().save(user);
	}

}

 2.3Service层设计:

public class UserServiceImpl implements IUserService{
	
	//通过Spring容器通过set方式注入UserDao
	private IUserDao userDao;
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void regiter(User user) {
		userDao.save(user);
	}

}
2.4配置hibernate.cfg.xml

<session-factory>
		<!-- 基本四项 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>	
		<property name="hibernate.connection.url">jdbc:mysql:///spring_study_day02</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">12345678</property>
		
		<!-- 配置方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!-- 显示sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		
		<!-- 4 自动生成表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 5本地线程绑定 -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		<!-- 导入映射文件 -->
		<mapping resource="com/lc/domain/User.hbm.xml"/>
	</session-factory>
 2.5配置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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.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
       					   http://www.springframework.org/schema/context 
       					   http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 1加载hibernate.cfg.xml获得SessionFactory:Spring提供了LocalSessionFactoryBean来创建SessionFactory实例 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>
	
	<!-- 2注册dao,并且给dao层注入sessionFactory,因为dao层继承了HibernateDaoSupport需要   sessionFactory来获取HibernateTemplate实例 -->
	<bean id="userDao" class="com.lc.dao.impl.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
      <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
	
	<!-- 3注册Service,并给Service层注入Dao层 -->
	<bean id="userService" class="com.lc.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<!-- 4.1配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 4.2配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="regiter" propagation="REQUIRED" isolation="DEFAULT"/>
		</tx:attributes>
	</tx:advice>
	<!-- 4.3AOP编程 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lc.service.impl.UserServiceImpl.*(..))"/>
	</aop:config>
</beans>
  这样Spring就把Hibernate整合好了。(下面这种方式一般经常用的配置方式:)

3.Spring整合Hibernate没有hibernate.cfg.xml

 3.1首先删hibernate.cfg.xml,不要。

 3.2修改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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.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
       					   http://www.springframework.org/schema/context 
       					   http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 可以加载properties文件,这样下面的dataSource里面注入的数据就可以使用${key}从properties文件中加载。加载properties文件需要写加载标签:
 例如:<context:property-placeholder location="这里写properties文件路径"/>
 -->
	<!-- 加载数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///spring_study_day02"></property>
		<property name="user" value="root"></property>
		<property name="password" value="12345678"></property>
	</bean>
	
	
	<!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory 
		* configLocation确定配置文件位置
			<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		1)dataSource 数据源
		2)hibernateProperties hibernate其他配置项
		3) 导入映射文件
			mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】
				<property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property>
				<property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
			mappingResources ,加载执行映射文件,从src下开始 。不支持通配符*
				<property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property>
			mappingDirectoryLocations ,加载指定目录下的,所有配置文件
				<property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property>
			mappingJarLocations , 从jar包中获得映射文件
	-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
			</props>
		</property>
		<property name="mappingLocations" value="classpath:com/lc/domain/User.hbm.xml"></property>
	</bean>
	
	
	<!-- 3注册dao,并且给dao层注入SessionFactory -->
	<bean id="userDao" class="com.lc.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 4注册Service,并给Service层注入Dao层 -->
	<bean id="userService" class="com.lc.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<!-- 5.1配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 5.2配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="regiter" propagation="REQUIRED" isolation="DEFAULT"/>
		</tx:attributes>
	</tx:advice>
	<!-- 5.3AOP编程 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lc.service.impl.UserServiceImpl.*(..))"/>
	</aop:config>
	

</beans>

4.struts整合Spring,由Spring来创建action

 4.1action类:

  

public class UserAction extends ActionSupport implements ModelDriven<User>{
	//封装数据
	private User user = new User();
	@Override
	public User getModel() {
		return user;
	}
	
	//通过Spring容器注入userService
	private IUserService userService;
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	//注册方法
	public String register() {
		userService.regiter(user);
		return "success";
	}

	
}

 4.2在Spring配置文件中注册action类

 

<!-- 配置Action -->
	<bean id="userAction" class="com.lc.web.action.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
4.3配置struts.xml文件

<struts>
	<!-- 开发者模式 -->
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
		<action name="userAction_*" class="userAction" method="{1}">
			<result name="success">/success.jsp</result>
		</action>
    </package>
</struts>

4.5配置web.xml文件

  <!-- 加载Spring配置文件路径 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--添加Spring提供的ContextLoaderListener监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 添加struts2的strutsPrepareAndExecuteFilter过滤器 -->
  <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>

4.6jsp测试页面

注册页面截图:

 

  测试成功截图:

   

5.struts整合Spring,由Sturts来创建Action

 保证其他不变,直接修改Spring的application.xml配置文件和struts.xml配置文件

5.1修改application.xml配置文件:删除注册action的bean:


 5.2修改struts.xml配置文件:把struts.xml中写action的class改成全限定名。

  

 然后在此运行就可以了。测试结果是正确的,这里我就不再粘贴测试结果了。




























猜你喜欢

转载自blog.csdn.net/ititii/article/details/79271448
今日推荐