系统程序架构:
整合思路
1.逆依赖方向而行,由Spring提供对象管理和服务
2.依次实现Spring与Hibernate、Spring与Struts2的集成
配置web.xml文件:
1.设置Spring配置文件
Spring配置文件的存放位置,若文件放置在/WEB-INF/目录下且名称为applicationContext.xml,可以不配置此项
范例:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath:applicationContext.xml</param-value> 4 </context-param>
2.指定何时启动Spring容器
该监听器用于在web环境中启动Spring容器
范例:
1 <listener> 2 <listener-class> 3 org.springframework.web.context.ContextLoaderListener 4 </listener-class> 5 </listener>
3.配置OpenSessionInVIew模式
该过滤器把一个Hibernate Session和一次完整的请求过程相绑定解决了诸如延迟加载等问题。
范例:
1 <filter> 2 <filter-name>OpenSessionInViewFilter</filter-name> 3 <filter-class> 4 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 5 </filter-class> 6 </filter> 7 <filter-mapping> 8 <filter-name>OpenSessionInViewFilter</filter-name> 9 <url-pattern>/*</url-pattern> 10 </filter-mapping>
注意:
1. 该过滤器要配置在Struts 2核心控制器之前
2. 若Spring中定义的SessionFactory Bean的id不是sessionFactory,需要为该过滤器配置sessionFactoryBeanName参数
在Spring中配置数据源和会话工厂:
方法一:配置文件分离
a.定义独立的Hibernate配置文件
b.由Spring导入并创建会话工厂Bean
方法二:在Spring配置文件中进行集中配置
a.先配置数据源,再以此为基础配置会话工厂Bean
b.持久化类的映射文件可以逐个配置,也可以按目录导入
使用HibernateDaoSupport基类:
1.HibernateDaoSupport基类的setSessionFactory()方法
范例:
1 <bean id="sessionFactory" 2 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 3 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 4 </bean> 5 6 <bean id="userDao" class="com.xuetang9.demo.dao.impl.UserDaoImpl"> 7 <property name="sessionFactory" ref="sessionFactory"></property> 8 </bean>
1.hibernate的配置文件自动生成时,没有userName 和 password?
范例:
1.hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">root</property> 11 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 12 <property name="show_sql">true</property> 13 <property name="format_sql">true</property> 14 15 <mapping resource="com/Elastic/SpringDemo3/ivy/entity/User.hbm.xml"/> 16 17 </session-factory> 18 </hibernate-configuration>
2.struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 </struts>
3.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>SpringDemo3_ivy</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 配置Spring文件的位置 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name><!-- 固定 --> 16 <!-- classPath:表示放在项目的根目录下 --> 17 <param-value>classPath:applicationContext.xml</param-value> 18 </context-param> 19 20 <!-- 配置Spring的启动监听器 --> 21 <listener> 22 <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class> 23 </listener> 24 25 <!-- 配置OpenSessionInView模式 --> 26 <filter> 27 <filter-name>OpenSessionInView</filter-name> 28 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 29 </filter> 30 <filter-mapping> 31 <filter-name>OpenSessionInView</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 35 <!-- 配置Struts2核心控制器 --> 36 <filter> 37 <filter-name>Struts2</filter-name> 38 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 39 </filter> 40 <filter-mapping> 41 <filter-name>Struts2</filter-name> 42 <url-pattern>/*</url-pattern> 43 </filter-mapping> 44 45 <!-- 配置HttpSession过期时间(单位:分钟) --> 46 <session-config> 47 <session-timeout>20</session-timeout> 48 </session-config> 49 50 </web-app>
4.applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 7 8 <!-- 配置SessionFactory(方法1:独立hibernate配置文件) --> 9 <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 10 <property name="configLocation" value="classPath:applicationContext.xml"></property> 11 </bean> --> 12 13 <!-- 使用Spring集成Hibernate(方法2:不需要hibernate配置文件) --> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="user" value="root"></property> 16 <property name="password" value="root"></property> 17 <property name="jdbcUrl" value="jdbc:mysql://localhost/hibernatedb"></property> 18 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 19 20 <!-- 连接池 --> 21 <property name="initialPoolSize" value="10"></property> 22 <property name="minPoolSize" value="5"></property> 23 <property name="maxPoolSize" value="100"></property> 24 <property name="acquireIncrement" value="3"></property> 25 <!-- 请求获取连接的重试次数 --> 26 <property name="acquireRetryAttempts" value="3"></property> 27 <property name="acquireRetryDelay" value="5"></property> 28 <property name="idleConnectionTestPeriod" value="60"></property> 29 30 </bean> 31 32 <!-- 使用dataSource配置SessionFactory --> 33 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 34 <property name="dataSource" ref="dataSource"></property> 35 <property name="hibernateProperties"> 36 <props> 37 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop> 38 <prop key="show_sql">true</prop> 39 <prop key="format_sql">true</prop> 40 </props> 41 </property> 42 43 <!-- 通配 --> 44 <property name="mappingLocations" value="classPath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml"> 45 </property> 46 47 <!-- 穷举 --> 48 <!-- <property name="mappingLocations"> 49 <list> 50 <value>classPath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml</value> 51 </list> 52 </property> --> 53 54 </bean> 55 </beans>