Spring整合Struts和Hibernate
一. 整合方式分类
1.零障碍整合(带hibernate的配置文件)
2. 不带有hibernate配置文件
二. spring的整合的思想
- 将Struts中和hibernate中需要创建的对象和配置文件中的信息整合到spring中
- Hibernate中的sessionFactory
三. 具体的实现
1.需要的jar包介绍
Spring:
IOC基本的开发
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
AOP的开发
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
使用JDBC模板和事务管理
spring-jdbc-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
整合web项目(在web容器中添加监听器:当启动web.xml的时候,自动加载spring容器)
spring-web-4.2.4.RELEASE.jar
整合单元测试
spring-test-4.2.4.RELEASE.jar
整合ORM框架(spring整合hibernate)
spring-orm-4.2.4.RELEASE.jar
Struts:
struts2-convention-plugin-2.3.24.jar ---Struts2的注解开发包(后续注解)(@PackageParent,@Namespace,@Action)
struts2-spring-plugin-2.3.24.jar ---Struts2和Spring整合的jar包
struts2-json-plugin-2.3.24.jar ---整合AJAX(项目课讲)
Hibernate
基本的开发包:lib\required\*.jar
日志记录
数据库驱动
mysql-connector-java-5.1.7-bin.jar
连接池(可选的)
lib\optional\c3p0\*.jar
2.第一种方式的配置文件
web.xml
==配置struts的核心过滤器==
==设置一个监听器,在加载配置文件的时候就将spring的applicationCointext的配置文件加载==
----------------------------------------------------------------------------------------------
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
struts.xml
==引用约束==
==配置常量==
=设置后缀名,开发者模式...=
==配置action类==
-------------------------------------------------------------------------------------------------
<?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>
<constant name="struts.action.extension" value="action"/>
<package name="ssh" extends="struts-default" namespace="/">
<action name="customer_*" class="customerAction" method="{1}"></action>
</package>
</struts>
hibernate.cfg.xml
==引入约束==
==配置sessionFactory==
=配置数据库=
=配置方言=
=配置可选属性=
=配置c3p0连接池=
=配置映射文件=
-------------------------------------------------------------------------------------------------
<?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:///lyric_ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">120</property>
<property name="c3p0.idle_test_period">3000</property>
<mapping resource="com/lyric/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Customer.hbm.xml
==引入约束==
==配置映射==
-------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!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.lyric.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping
applicationContext.xml
==引入约束==
==注册hibernate的sessionFactory==
=将hibernate.cfg.xml文件引入进来=
==注册CustomerDao==
=将sessionFactory注入到CustomerDao中=
==注册CustomerService==
=将CustomerDao注入到CustomerService中=
==注册CustomerAction==
=将CustomerService注入到CustomerAction中=
===注册事务管理=
=并将sessionFactory注入进去(如果是JDBC模板的话就注入DataSource)=
==开启注解==
-------------------------------------------------------------------------------------------------
<?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="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="customerDao" class="com.lyric.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="com.lyric.Service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<bean id="customerAction" class="com.lyric.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
注意:
- 在配置dao的时候注入一个sessionFactory,如果dao类继承一个HibernateDaoSupport的话就或自己生成一个hibernate的模板,在使用的时候就直接this.getHibernateTemplate.来操作数据库.
- 注册sessionFactory的时候需要加载hibernate.cfg.xml(第一种方式)的配置文件.
- 注册Action类的时候需要将scope属性改为prototype, 单例改多例
3.第二种方式的配置文件
区别:将hibernate.cfg.xml中的配置全部转移到applicationContext.xml中
步骤:
提供一个关于jdbc的属性文件
在applicationContext.xml中配置c3p0连接池
在applicationContext.xml中配置属性和映射
配置数据源–hibernate.cfg.xml中的数据库的配置
配置方言
配置可选属性
加载映射文件
映射文件有三种添加方式(推荐第三种方式) <property name="mappingResources"> <array> <value>com/lyric/domain/Customer.hbm.xml</value> </array> </property> =========================================================== <property name="mappingLocations"> <array> <value>classpath:com/lyric/domain/*.hbm.xml</value> </array> </property> =========================================================== <property name="mappingDirectoryLocations"> <array> <value>classpath:com/lyric/domain</value> </array> </property>
applicationContex.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">
==注册hibernate中的sessionFactory,并且配置相关的配置==
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<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>
<property name="mappingDirectoryLocations">
<array>
<value>classpath:com/lyric/domain</value>
</array>
</property>
</bean>
==配置dao==
<bean id="customerDao" class="com.lyric.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
==配置service==
<bean id="customerService" class="com.lyric.Service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
==配置action类==
<bean id="customerAction" class="com.lyric.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
==配置事务管理器==
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
==开启事物的注解==
<tx:annotation-driven transaction-manager="transactionManager"/>
==引入外部的属性文件==
<context:property-placeholder location="classpath:jdbc.properties"/>
==配置一个c3p0连接池作用域hibernate的数据源==
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<property name="checkoutTimeout" value="120"></property>
<property name="idleConnectionTestPeriod" value="3000"></property>
</bean>
</beans>
查询应用
命名查询,把使用的HQL语句从Dao中 提取出去,这样即可以实现通用,也可以提高程序的安全性
在hibernate.hbm.xml中添加<query name="queryAll">from Customer</query>
4.延迟加载(no - session )
产生的原因:
业务层查询数据,返回后,session关闭了, 表现层获取数据如果关联延迟数据,无法初始化 !
解决办法:
方案一: 在Xxx.hbm.xml中配置为立即加载 lazy=false (不推荐,因为只查询一个表的信息,其他表的信息都会立即检索出来)
方案二: Service方法返回前,即Session关闭之前,对延迟数据进行初始化 (缺点多,还需要手动写代码)
方案三: spring提供了OpenSessionInView 机制 (将Session开启到表现层 最前面 Filter )Spring 提供 OpenSessionInViewFilter 注意:需要配置在struts2 Filter前面,否则不起作用
web.xml
<!-- OpenSessionInView机制:会将会话到表现层,让会话在请求结束之后关闭,延迟了session关闭,需要放置到struts2的过滤器的前面 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
OpenSessionInViewFilter:在request过程中维持hibernate的session。延迟session的关闭,直到request结束(即一次请求),再自动关闭session,也就是说,直到表现层的数据全部加载完毕,再关闭Session。