- 整合hibernate
- 增加相关jar包
- 增加spring配置文件:applicationContext.xml,文件内容如下:
- 配置组件扫描。让spring管理basePackage和他的子包下所有的类。
<context:component-scanbase-package="com.ll"></context:component-scan>
- 配置dataSource数据源
<beanid="ds" class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<propertyname="url"value="jdbc:mysql://localhost:3306/test"></property>
<propertyname="username" value="root"></property>
<propertyname="password" value="root"></property>
</bean>
- 配置sessionFactory
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="packagesToScan" >
<list>
<value>com.ll.po</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">true</prop>
<propkey="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<propertyname="dataSource" ref="ds"></property>
</bean>
- 配置事务管理(事务增加在service层!)
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="txManager" />
- 配置HibernateTemplate
<beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
- 整合struts
- 导入jar包
- 配置web.xml
- 配置<context-param>指定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>
- 配置opensessionInview过滤器(一定要配置在struts过滤器之前)
<filter>
<filter-name>hibernateSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 配置struts过滤器
<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上下文加载的监听器
- 增加struts.xml:
增加<constant name="struts.objectFactory" value="spring" />