建立一个Web项目,然后导入如下包
l struts2包:在struts2-blank.war下的lib目录下,以及struts-2.3.15.1\lib下的struts和spring整合的插件包struts2-spring-plugin-2.3.15.1.jar
l hibernate包:hibernate-distribution-3.6.10.Final\lib\,把required中的所有包都导入进去,以及jpa下的hibernate-jpa-2.0-api-1.0.1.Final,optional/ c3p0下的JDBC连接池c3p0-0.9.1.jar包,MYSQL的jdbc驱动mysql-connector-java-5.1.7-bin, hibernate的的一个日志系统,hibernate3.jar。
l spring2.5:spring
在hibernate-distribution-3.6.10.Final\project\etc下找到一个hibernate.cfg.xml文件,拷贝到项目中的src下,去掉没用的部分。
添加applicationContext.xml文件。
1、整合SHH
首先整合spring和hibernate
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
<!-- 配置SessionFactory(与Hibernate整合) -->
<!-- 表示下面的变量是从这个配置文件读取的 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 在spring中配置了一个叫sessionFactory的bean,类为hibernate中的LocalSessionFactoryBean,这样,spring就和hibernate联系起来了 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件的位置,classpath:hibernate.cfg.xml:配置文件的路径 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property><!-- -->
<property name="dataSource">
<!-- 配置DataSource数据源,这里采用的是c3p0的数据库连接 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbcUrl}"></property>