码迷,mamicode.com
首页 > 编程语言 > 详细

SSH(struts2+spring+hibernate)三大框架整合

时间:2019-01-05 12:14:09      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:结构   数据库连接   hbm.xml   信息   框架整合   t_sql   eclips   打开   doctype   

SSH框架整合理论:

在SSH框架的JAVAweb项目的开发过程中,WEB层通常使用的是Struts2+jsp,service层使用的是javaBean,DAO层使用的是hibernate,而spring的使用覆盖三层。

        使用了spring框架之后,我们可以把对象交给spring来管理。在WEB层中,所有action对象的创建和管理都可以交给spring来完成,这样Struts2就不用自己来new一个action,这一切都可以交给spring,直接向spring来要action对象。

  在DAO层中hibernate框架,在这一层中hibernate--SessionFactory对象,包括session的获得,AOP事务,都可以交给spring,这样使用hibernate只需要做一些业务操作就可以了,

把其他的关于事务以及对象的管理都交给spring来完成。

简单的说,三大框架整合,就是把Struts2整合到spring当中,把hibernate整合到spring当中。

下面我们来进行三大框架整合:

整合的第一步,我们首先需要做的就是导包,导入三个框架需要用到的jar包,这是三大框架整个中的第一步,也是最重要的一步,导包完成后,我们的三大框架整合就已经完成了百分之六十。

如果三大框架所用到的jar包文件压缩包没有,可以去各个框架的相关网站上下载。

hibernate包:

  1. hibernate/lib/required
  2. hibernate/lib/jpa
  3. 数据库驱动包

在hibernate中的lib下的required包中jar文件,都需要拿过来。

技术分享图片

/lib/jpa  java持久化规范

技术分享图片

Struts2包:

Struts-black.war/WEB-INF/lib/*

技术分享图片

如果有重复,删除版本低的那个。

技术分享图片

 Struts整合spring插件包

 技术分享图片

如果导入这个包,Struts在启动的时候就会寻找spring容器,如果还没有配置spring容器,单单是启动Struts,项目就会抛出异常。如果只是用Struts框架时,

这个包可以不需要导入。

 

 spring包:

  1. 基本包:4+2
  2. 整合web需要的web包
  3. 整合aop:4个包
  4. 整合JDBC事务:4个
  5. 整合Junit4测试:1个  test包
  • 基本包:4+2   core |beans |context| expression| logging| log4j
  • 整合web需要的web包:spring-web
  • 整合aop:4个包:spring-aop|spring-aspect|aop联盟|aopweaving
  • 整合JDBC事务:spring-jdbc|spring-tx|c3p0|spring-orm
  • 整合Junit4测试:spring-test

 

基本包:

技术分享图片

整合web需要的web包

 技术分享图片

整合aop:4个包

 技术分享图片

整合JDBC事务

 技术分享图片

整合Junit4测试

 技术分享图片

 

截止到这里,用到的包就导入完了,但是如果需要使用的是eclipse,标签库的包还是需要自己导入一下。

 标签库包:

技术分享图片

 

导包完成

 导包完成之后,就需要进行三大框架的整合步骤,首先是单独为项目配置每一个框架

单独配置spring容器

 创建配置文件,并导入约束(4个)beans|context|aop|tx

首先,创建spring配置文件applicationContext.xml

技术分享图片

然后切换到设计视图

 

首先导入一个技术分享图片约束

然后再继续导入剩余的4个约束

beans约束

技术分享图片

context约束

技术分享图片

然后aop,tx   这样四个命名空间就导入完成了

 技术分享图片

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<bean name="userAction " class="com.test.web.action.UserAction "></bean>						
</beans>

 

截止到这里,spring的约束以及命名空间也导入完成。

这样spring容器就配置好了,但是我们想让spring随着项目的启动而启动,就需要在web.xml文件中配置一个监听器。

web.xml

    <!-- 让spring随web启动而创建的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
    <!-- 配置spring配置文件位置参数 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  这个时候启动项目,项目不报错,说明spring容器随着项目的启动而启动配置没有错误。

 

 

单独配置Struts2

1. 配置Struts2主配置文件

<?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.objectFactory = spring	将action的创建交给spring容器	
			struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
			-->
	<constant name="struts.objectFactory" value="spring"></constant>

	<package name="crm" namespace="/" extends="struts-default" >
		
		<action name="UserAction_*" class="userAction" method="{1}" >
			<result name="toHome" type="redirect" >/index.htm</result>
			<result name="error" >/login.jsp</result>
		</action>
	</package>
</struts>

 主配置文件写好之后,需要配置Struts2的核心过滤器

在web.xml文件中配置Struts2的核心过滤器

代码如下:

   <filter>
  	<filter-name>openSessionInView</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <!-- struts2核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>openSessionInView</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

  需要注意的是:配置到这里,还是建议重新的启动项目,看看是否配置正确,我们在这里需要注意的是,每配置一步,我们就启动项目测试一下,看看该步是否正确

防止,等最后我们配置完的时候,出现错误的时候,不方便进行检查。

配置完spring到web项目,以及配置完Struts2到web项目,下面需要做的就是整合Struts2到spring中

整合Struts2与spring

1. 需要导包,导入spring与Struts2整合包,这个包我们在第一步已经导入过了struts2-spring-plugin-2.3.24.jar

2. 配置常量:

  (1)struts.objectFactory = spring 将action的创建交给spring容器 (2)struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性

  我们只需要配置第一个常量即可,因为第二个默认是打开的,不用配置。第一个常量是将action的创建交给spring容器 ,我们需要在Struts.xml文件中配置

<!-- #  struts.objectFactory = spring	将action的创建交给spring容器	
			struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
			-->
	<constant name="struts.objectFactory" value="spring"></constant>

  技术分享图片

下面进行Struts2与spring整合

整合有两种方案

第一种方案:class属性上仍然配置action的完整类名,struts2仍然创建action,由spring负责组装Action中的依赖属性

技术分享图片

使用这种方案,就是在配置文件中action  class依旧使用完整的类名,这样可以看成是Struts与spring配合进行对象创建。struts2仍然创建action,由spring负责组装Action中的依赖属性

 不过这种方法不推荐使用,不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.

 第二种方案:

       class属性上填写spring中action对象的BeanName,BeanName是在applicationContext.xml文件中定义的,完全由spring管理action生命周期,包括Action的创建注意:需要手动组装依赖属性

   注意:需要手动组装依赖属性,因为这样配置的话,spring就不能自动组装,需要手动组装依赖属性

手动组装依赖属性的意思就是,在applicationContext.xml文件中,把各个对象的依赖手动配置进去。

技术分享图片

在Struts.xml文件中,class属性只需要写beanName就可以了

技术分享图片

注意:scope="prototype"   Action对象作用范围一定是多例的.这样才符合struts2架构

	<!-- action -->
	<!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
	<bean name="userAction" class="com.test.web.action.UserAction" scope="prototype" >
		<property name="userService" ref="userService" ></property>
	</bean>
	<!-- service -->
	<bean name="userService" class="com.test.service.impl.UserServiceImpl" >
	</bean>

到这里,Struts整合spring完成了

下面需要做的是。进行单独配置hibernate,然后把hibernate与spring进行整合:

单独配置hibernate

 创建hibernate配置文件hibernate.cfg.xml,配置相关实体以及数据库的连接

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
	
		 <!-- 数据库驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		 <!-- 数据库url -->
		<property name="hibernate.connection.url">jdbc:mysql:///shop</property>
		 <!-- 数据库连接用户名 -->
		<property name="hibernate.connection.username">root</property>
		 <!-- 数据库连接密码 -->
		<property name="hibernate.connection.password">123456</property>
		<!-- 数据库方言
			注意: MYSQL在选择方言时,请选择最短的方言.
		 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		
		<!-- 将hibernate生成的sql语句打印到控制台 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
		<property name="hibernate.format_sql">true</property>
		<!-- 
		自动导出表结构. 自动建表
		 -->
		<property name="hibernate.hbm2ddl.auto">update</property>		 
		 <!-- 引入实体配置文件 -->
		<mapping resource="com/test/domain/Customer.hbm.xml" />
		<mapping resource="com/test/domain/LinkMan.hbm.xml" />
		<mapping resource="com/test/domain/User.hbm.xml" />
		
	</session-factory>
</hibernate-configuration>

  

hibernate与spring整合

整合原理:将sessionFactory对象交给spring容器管理

applicationContext.xml文件中加载hibernate框架配置文件有两种方式,下面我们对这两种方式做简要的介绍:

第一种方案:仍然使用外部的hibernate.cfg.xml配置信息,applicationContext.xml文件中

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
		<property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
	</bean> -->

这样配置的话,sessionFactory就会从classpath:hibernate.cfg.xml  配置文件中加载配置信息,使用的是该文件中的所有配置信息

这种方案不推荐使用,因为这样的话会使hibernate的配置增多,我们的初衷应该是把一些配置交给spring,应该往spring中挪一挪,name请看第二种方案

第二种方案:在spring配置中放置hibernate配置信息

	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
		<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
		<property name="dataSource" ref="dataSource" ></property>
		<!-- 配置hibernate基本信息 -->
		<property name="hibernateProperties">
			<props>
				<!--  必选配置 -->
				<prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
				<prop key="hibernate.connection.url" >jdbc:mysql:///crm</prop>
				<prop key="hibernate.connection.username" >root</prop>
				<prop key="hibernate.connection.password" >123456</prop> 
				<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
				
				<!--  可选配置 -->
				<prop key="hibernate.show_sql" >true</prop>
				<prop key="hibernate.format_sql" >true</prop>
				<prop key="hibernate.hbm2ddl.auto" >update</prop>
			</props>
		</property>
		<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
		<property name="mappingDirectoryLocations" value="classpath:com/test/domain" ></property>
	</bean>

这一种方案是推荐方案,我们把sessionFactory交给spring容器来管理

在三大框架整合中,这些是最基本的整合操作,下面将对整合中的细节进行讲解:

使用C3P0连接池:

 

++++++++++++++++++++++++++++++++++++++++++++待续++++++++

 

整理不易,转载请注明出处。

 

SSH(struts2+spring+hibernate)三大框架整合

标签:结构   数据库连接   hbm.xml   信息   框架整合   t_sql   eclips   打开   doctype   

原文地址:https://www.cnblogs.com/leo1014/p/10218693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!