一、回忆spring和struts的整合
加入struts2-spring-plugin-2.3.24.jar插件包即可,配置文件不需要整合,整合过程比较简单。
修改 web.xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> ContextLoaderListener实现ServletContextListener接口,在服务器启动时加载, 默认自动载入置于WEB-INF目录下的Spring配置文件applicationContext.xml
二、现在来看hibernate和spring的整合
1、关于需要引入的jar包在之前的ssh基本开发环境搭建的博客中有说明,这里不一一说明
2、修改applicationContext中的配置
(1)引入申明式事务支持的XML命名空间 tx,并且引入xsd约束格式文件 xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
(2)配置dataSource
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 初始化连接数量; --> <property name="initialSize" value="0" /> <!-- 最大并发连接数 --> <property name="maxActive" value="20" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="20" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="0" /> <!-- 最大等待时长 --> <property name="maxWait" value="60000" /> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="true" /> <!-- 超过时间限制多长; --> <property name="removeAbandonedTimeout" value="180"/> <!-- 数据源连接参数配置; --> <property name="username" value="${db.username}"/> <property name="url" value="${db.url}"/> <property name="password" value="${db.password}"/> <property name="driverClassName" value="${db.driverClassName}"/> </bean>
这里的${db.username}叫做占位符,需要在外部配置文件中去配置相关的值。这里是关于jdbc MySQL数据库的参数设置
(3)sessionFactory配置 <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="net.xinqushi.model.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean>
(4)读取外部属性文件,获取数据源参数 <context:property-placeholder location="classpath:dataSource.properties/>
dataSource.properties:
db.username=jack db.url=jdbc:mysql://localhost:3306/album db.password=12345678 db.driverClassName=com.mysql.jdbc.Driver hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=update hibernate.format_sql=true(5)配置事务管理器 <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
(6)初始化hibernateTemplate <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
(7)配置事务管理 <!-- 定义切面 --> <aop:config> <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> <!-- 声明式事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> 这样配置完成之后,就不需要hibernate.cfg.xml这个关于hibernate本身的配置文件了。而且在DAO具体实现类当中不用HibernateUtil类中的sessionFactory。像获取session,关闭session,事务的begin和提交都由spring框架完成。至于具体的对数据库中数据的操作可以参考文档 hibernateTemplate操作数据的方法:http://www.blogjava.net/tbwshc/articles/380014.html
完整的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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl,net.xinqushi.aop"/> <aop:config> <aop:aspect ref="log"> <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.*(..))" id="mypt"/> <aop:before method="mybefore" pointcut-ref="mypt"/> <aop:after method="myafter" pointcut-ref="mypt"/> </aop:aspect> </aop:config> <!-- 注解配置AOP才会用到的 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 初始化连接数量; --> <property name="initialSize" value="0" /> <!-- 最大并发连接数 --> <property name="maxActive" value="20" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="20" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="0" /> <!-- 最大等待时长 --> <property name="maxWait" value="60000" /> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="true" /> <!-- 超过时间限制多长; --> <property name="removeAbandonedTimeout" value="180"/> <!-- 数据源连接参数配置; --> <property name="username" value="${db.username}"/> <property name="url" value="${db.url}"/> <property name="password" value="${db.password}"/> <property name="driverClassName" value="${db.driverClassName}"/> </bean> <!-- 读取外部文件 --> <context:property-placeholder location="classpath:dataSource.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="net.xinqushi.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 初始化hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义切面 --> <aop:config> <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> <!-- 声明式事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <tx:method name="is*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>