标签:
Struts2,Spring3,Hibernate4整合--SSH框架(学习中)
一、包的导入
1、Spring包

2、Hibernate 包

3、struts 包

4、数据库方面的包及junt4的包

二、配置文件
1、beans.xml (具体要注意的已经注释到 xml 中了,目前整合了Spring 与 hibernate4 )
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 打开Spring 的 Annotation 支持-->
<context:annotation-config/>
<!-- 设定Annotation 到 哪里 找-->
<context:component-scan base-package="org.cs"/>
<!-- 打开Spring 的 Aop 代理 -->
<aop:aspectj-autoproxy/>
<!-- 使用 DBCP 创建 dateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置 hibernate -->
<!-- 配置连接池的初始值 为 1-->
<property name="initialSize" value="1"/>
<!-- 配置最小空闲时 -->
<property name="minIdle" value="1"/>
<!-- 最大连接池 -->
<property name="maxTotal" value="100"/>
<!-- 配置最大空闲时-->
<property name="maxIdle" value="20"/>
<!-- 配置等待时间-->
<property name="maxWaitMillis" value="1000" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 整合 hibernate4 创建 SessionFactory 工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 设置 Spring 在哪里找 实体类 -->
<property name="packagesToScan">
<value>org.cs.Model</value>
</property>
<!-- 配置 hibernate -->
<property name="hibernateProperties">
<!--<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>-->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<!-- 配置 Spring 的事务管理 -->
<!-- 创建事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 设置 aop:pointcut 表示 哪些[ 包.类.方法(..)] 需要加入事务-->
<aop:pointcut id="allMethods"
expression="execution(* org.cs.Dao.*.*(..))"/>
<!-- 设置 aop:pointcut 表示 具体 需要加入事物的 方法-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
</aop:config>
<!-- 配置那些方法需要加入事务处理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 让所有方法加入事务 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
三、总结:
赶时间做出来的,还没写完整(目前整合了hibernate4 与 Spring ,快断网了。。),如果有错的地方请大家指出,方便大家一起学习。
Struts2,Spring3,Hibernate4整合--SSH框架
标签:
原文地址:http://www.cnblogs.com/caishun/p/5393296.html