Struts-Spring-Hibernate 整合
- 初学用的Myeclipse工具(不是手动配置)
一、引入jar包
(一)struts2
- 核心包和struts-spring
(二)spring
- 核心包和持久化包
(三)hibernate
- 需要把hibernate的核心配置文件配置到spring中
二、Hibernate映射和应用程序生成
(一)从database explorer表生成Hibernate映射和Java类
创建映射
更新配置文件的映射
生成pojp实体类对应表结构
创建dao层(SpringDao)
指定sessionFactory配置交给applicationContext.xml
ID生成规则 : increment
(二)配置类型映射的细节
l ID生成规则 : increment
(三)配置反向工程的细节
- 包含引用表(A-> B)包括引用表(A< b)
三、配置web.xml
(一)指定spring配置文件 applicationContext.xml的路径
<!-- 指定spring 配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
(二)配置加载配置文件的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(三)配置hibernate延时加载异常的过滤器
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.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>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
四、配置spring applicationContext.xml
(一)引入约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
(二)设置数据库链接和线程池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
<property name="username" value="uname" />
<property name="password" value="passwd" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"></property>
</bean>
(三)配置sessionFactory
<!-- sessionFactory 工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 设置数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- hibernate设置 -->
<property name="hibernateProperties">
<props>
<!-- 设置方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.DerbyDialect
</prop>
<!-- 设置显示sql语句 -->
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<!-- 设置实体类映射 -->
<property name="mappingResources">
<list>
<value>cn/po/Bank.hbm.xml</value>
</list>
</property>
</bean>
(四)配置spring装配对象
<!-- dao层 -->
<bean id="BankDAO" class="cn.dao.BankDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- biz层 -->
<bean id="BankBiz" class="cn.biz.BanBiz">
<property name="bankdao" ref="BankDAO" />
</bean>
<!-- 创建action -->
<bean id="StudentAction" class="cn.action.StudentAction">
<property name="biz" ref="StudentBiz"></property>
</bean>
(五)创建事务管理器
<!-- 创建事务管理器 -->
<bean id="txmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
(六)配置事务属性
<!-- 配置事务 -->
<tx:advice id="txAdvice" transaction-manager="txmanager">
<tx:attributes>
<!-- 指定事务隔离级别 -->
<tx:method name="update" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
(七)添加切点
<!-- 添加切点 -->
<aop:config>
<!-- 配置aop切点 -->
<aop:pointcut expression="execution(* cn.biz.BanBiz.*(..))" id="bank_biz_aop" />
`<!-- 配置事务增强切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="bank_biz_aop" />
<!-- 配置方法增强切面 -->
<aop:aspect ref="log">
<!-- 配置切面通知类型 -->
<!-- 前置通知 -->
<aop:before method="log()" pointcut-ref="bank_biz_aop" />
<!-- 后置通知 -->
<aop:after method="end()" pointcut-ref="bank_biz_aop" />
</aop:aspect>
</aop:config>
(八)struts2 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="Student" namespace="/" extends="struts-default">
<action name="*_*" class="{2}Action" method="{1}">
<result name="ok" type="redirect">${path}</result>
</action>
</package>
</struts>