Spring+Struts2+hibernate整合

SSH三大框架整合

(1)struts2

(2)hibernate5.x

(3)spring4.x

回顾SSH框架知识点

Hibernate框架

1、 hibernate核心配置文件

(1)orm思想

- 对象关系映射

(1)数据库信息

(2)hibernate信息

(3)映射配置

(4)hibernate核心配置文件

- 如果单纯使用hibernate框架,核心配置文件名称 hibernate.cfg.xml和位置 src下面 固定的

-hibernate和spring整合时候,hibernate核心配置文件名称和位置没有固定要求的

2 、hibernate映射配置文件

(1)实体类和数据库表映射关系—使用orm思想

3、 hibernate操作的步骤

(1)在spring框架对hibernate框架进行封装,使用hibernateTemplate

Struts2框架

1、 Action操作

(1)action创建三种方式

- 继承类 ActionSupport

(2)配置action访问路径

- 创建struts.xml配置文件,这个文件名称和位置固定 src下面的

(3)配置访问action的多个方法

- 使用通配符方式配置

(4)在action获取表单提交数据

- 获取request对象

** 使用ServletActionContext类获取

- 属性封装

- 模型驱动(重点)

- 表达式封装

(5)在action操作域对象

- 使用ServletActionContext获取域对象

(6)配置struts2的过滤器


2 、值栈

(1)向值栈放数据

- set方法

- push方法

- 定义变量,生成get方法

(2)从值栈获取数据

- 在jsp中使用struts2标签+ognl获取

- <s:property>

- <s:iterator>


3 、拦截器

(1)aop和责任链模式

(2)自定义拦截器

- 继承MethodFilterInterceptor

- 重写类里面的方法

- 配置拦截器和action关联

Spring框架

1、 spring核心配置文件

(1)名称和位置没有固定要求

(2)在spring核心配置文件中引入schema约束

2、 创建对象

(1)xml配置方式:<bean  id=””  class=”” scope=””/>

(2)注解方式:四个注解

3、 注入属性(对象类型属性)

(1)xml配置方式:

(2)注解方式:两个注解

- autowired

- resource

4 、使用ServletContext对象和监听器实现

(1)在服务器启动时候,加载spring配置文件,创建对象

(2)配置spring的监听器

(3)指定spring配置文件位置

5、 jdbcTemplate

6、 spring事务配置

(1)xml方式

(2)注解方式

SSH框架整合思想


1 、三大框架应用在javaee三层结构



2、 struts2框架和spring整合

(1)struts2的action在spring配置


3 、spring框架和hibernate框架整合

(1)hibernate的sessionFactory交给spring配置

(2)把hibernate数据库配置交给spring配置



整合struts2和spring框架

1 、把struts2的action交给spring管理

2 、实现过程

第一步 导入struts2的jar包

(1)导入用于整合的jar包



第二步 创建action

第三步 创建struts2核心配置文件,配置action

(1)位置在src下面,名称是struts.xml

第四步 配置struts2过滤器

第五步 导入spring的jar包

第六步 创建spring配置文件

(1)      引入约束

(2)      配置spring监听器

(3)      指定spring配置文件位置


第七步 把action交给spring进行配置(重点

原来的配置:


(1)在spring配置action对象,在struts.xml中也配置action对象

(2)解决:只需要在spring里面配置action对象,不要在struts.xml中配置

Spring配置文件中的action配置与struts2核心配置文件的action配置关系:


Spring框架整合hibernate框架

1 、把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置

2、 把hibernate里面的sessionFactory创建交给spring管理

具体实现

第一步 导入hibernate的jar包

(1)导入struts2和hibernate的jar包时候有jar冲突问题,在struts2里面有jar包

在hibernate里面有jar包


删除低版本的jar包

(2)导入spring整合持久化层框架需要导入jar包


第二步 搭建hibernate环境搭建

1 、创建实体类

2、 配置实体类映射关系

3 、创建核心配置文件

第三步 把hibernate核心配置文件数据库配置,在spring进行配置

(1)把hibernate核心文件中数据库配置去掉了,在spring配置
	<!-- 配置c3p0连接池,在Spring配置是用在JdbcTemplate,在整合中也可以用在hibernate -->
	<!-- 创建连接池对象 -->
	<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_day04"></property>
		<property name="user" value="root"></property>
		<property name="password" value="Huang123"></property>
	</bean>

第四步 把hibernate的sessionFactory交给spring配置

(1)服务器启动时候,加载spring配置文件,把配置文件中对象创建

(2)把sessionFactory对象创建在spring配置

(3)因为创建sessionFactory代码不是new出来的,而是多行代码实现的



(4)spring里面针对上面情况,封装类,配置类对象可以创建sessionFactory

<!-- 把hibernate的sessionFactory的创建也交给Spring管理 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 因为在hibernate核心配置文件中没有数据库配置,数据库配置在Spring中配置 ,需要注入Spring中的数据库配置 -->
		<!-- 注入Spring配置的DataSource -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定使用hibernate的核心配置文件 ,加上value属性,value属性写的是hibernate核心配置文件路径,写法固定,类路径下的ApplicationContext -->
		<!-- 特点;在单独使用hibernate时核心配置文件固定,在整合中没有固定位置,只要将文件路径引入就行 -->
		<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
	</bean>

第五步 在dao里面使用hibernateTemplate,记得要导入对应的hibernate版本包,本项目是5版本,要对象导入5的hibernateTemplate

(1)在dao得到hibernateTemplate的对象


注入HibernateTemplate


<!-- 创建service对象 -->
	<bean id="userService" class="com.integration.service.UserService"></bean>
	<!-- 创建daoimpl实现类对象 -->
	<bean id="userDaoImpl" class="com.integration.dao.UserDaoImpl"></bean>
	<!-- 创建HibernateTemplate对象 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<!-- 注入sessionFactory,name属性是hibernateTemplate源码中,给出了对象与set方法,ref是在applicationContext配置的对象 -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

(2)调用hibernate模板里面save方法添加

没有配置事务,做操作时候,出现异常:


第六步 配置事务

<!-- 注解方式配置事务 -->
	<!-- 1、配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 注入DataSource也行,但是一般建议注入sessionFactory,因为sessionFactory中包含了DataSource -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 2、开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<!-- 3、在类上加上注解,一般加在service类上 -->



HibernateTemplate介绍

1 、HibernateTemplate对hibernate框架进行封装,

直接调用HibernateTemplate里面的方法实现功能

2 、HibernateTemplate常用的方法

•       Serializable save(Object entity) :添加操作

•        void update(Object entity) :修改操作

•        void delete(Object entity) :删除操作

•        <T> Tget(Class<T> entityClass, Serializable id) :根据id查询

•        <T> Tload(Class<T> entityClass, Serializable id):根据id查询,延迟加载




 Listfind(String queryString, Object... values) :查询操作的方法

(1)

(1)第一个参数是 hql语句

(2)语句参数值

(2)findByCriteria()

SSH框架整合过程

第一步 导入jar包



第二步 搭建struts2环境

(1)创建action,创建struts.xml配置文件,配置action

(2)配置struts2的过滤器

第三步 搭建hibernate环境

(1)创建实体类

(2)配置实体类和数据库表映射关系

(3)创建hibernate核心配置文件

- 引入映射配置文件

第四步 搭建spring环境

(1)创建spring核心配置文件

(2)让spring配置文件在服务器启动时候加载

- 配置监听器


- 指定spring配置文件位置



第五步 struts2和spring整合

(1)把action在spring配置(action多实例的)


(2)在struts.xml中action标签class属性里面写 bean的id值


第六步 spring和hibernate整合

(1)把hibernate核心配置文件中数据库配置,在spring里面配置

(2)把hibernate的sessionFactory在spring配置


第七步 在dao里面使用hibernateTemplate

(1)在dao注入hibernateTemplate对象

(2)在hibernateTemplate对象中注入sessionFactory

第八步 配置事务

注入事务:

具体配置文件:

ApplicationContext:

<?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: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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 配置c3p0连接池 -->
	<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_day004"></property>
		<property name="user" value="root"></property>
		<property name="password" value="Huang123"></property>
	</bean>

	<!-- sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
	</bean>

	<!-- 注解方式配置事务 -->
	<!-- 1、配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 注入DataSource也行,但是一般建议注入sessionFactory,因为sessionFactory中包含了DataSource -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 2、开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<!-- 3、在类上加上注解,一般加在service类上 -->
	
	<bean id="userAction" class="com.ssh.action.UserAction">
		<property name="userService" ref="userService"></property>
	</bean>
	<bean id="userService" class="com.ssh.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	<bean id="userDaoImpl" class="com.ssh.dao.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

struts2.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>


	<package name="demo1" extends="struts-default" namespace="/">
		<action name="userAction" class="userAction"></action>
	</package>


</struts>

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> 
			<property name="hibernate.connection.url">jdbc:mysql:///spring_day004</property> 
			<property name="hibernate.connection.username">root</property> <property 
			name="hibernate.connection.password">Huang123</property> -->
			
		<!-- 第二部分: 配置hibernate信息 可选的 -->
		<!-- 输出底层sql语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 输出底层sql语句格式 -->
		<property name="hibernate.format_sql">true</property>
		<!-- hibernate帮创建表,需要配置之后 update: 如果已经有表,更新,如果没有,创建 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 配置数据库方言 在mysql里面实现分页 关键字 limit,只能使用mysql里面 在oracle数据库,实现分页rownum 让hibernate框架识别不同数据库的自己特有的语句 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 第三部分: 把映射文件放到核心配置文件中 必须的 -->
		<mapping resource="com/ssh/entity/user.hbm.xml" />
	</session-factory>
</hibernate-configuration>

出现异常时候,

第一行 异常信息

下面 cased by :

整合其他方式

1、 spring整合hibernate时候,可以不写hibernate核心配置文件

(1)把hibernate核心配置文件中,基本信息配置和映射引入都放到spring配置


Spring分模块开发(重点

1、 在spring里面配置多个内容,造成配置混乱,不利用维护

2、 把spring核心配置文件中,一部分配置放到单独的配置文件中,在spring核心配置文件中引入单独配置文件

在本项目中将action的配置抽取出来,放到user.xml中

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: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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 配置c3p0连接池 -->
	<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_day004"></property>
		<property name="user" value="root"></property>
		<property name="password" value="Huang123"></property>
	</bean>

	<!-- sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> -->
		<!-- 1、配置hibernate基本信息 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			</props>
		</property>
		<!-- 2、配置映射文件的映入 -->
		<property name="mappingResources">
			<list>
				<value>com/ssh/entity/user.hbm.xml</value>
			</list>
		</property>
	</bean>

	<!-- 注解方式配置事务 -->
	<!-- 1、配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<!-- 注入DataSource也行,但是一般建议注入sessionFactory,因为sessionFactory中包含了DataSource -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 2、开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<!-- 3、在类上加上注解,一般加在service类上 -->

<!-- 	<bean id="userAction" class="com.ssh.action.UserAction">
		<property name="userService" ref="userService"></property>
	</bean>
	<bean id="userService" class="com.ssh.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	<bean id="userDaoImpl" class="com.ssh.dao.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean> -->
	<!-- 引入其他的Spring配置文件 -->
	<import resource="classpath:user.xml"/>
</beans>

user.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: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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">


	<bean id="userAction" class="com.ssh.action.UserAction">
		<property name="userService" ref="userService"></property>
	</bean>
	<bean id="userService" class="com.ssh.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	<bean id="userDaoImpl" class="com.ssh.dao.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

问题:

1、在struts2里面和hibernate里面都有jar包


2、 如果把数据库信息配置在hibernate里面进行配置

(1)出现异常,事务里面找不到数据源了

(2)数据库配置在spring里面配置

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值