关于整合hibernate4和spring3的相关注意事项

时间:2022-12-20 23:58:30

这两天在做一个小项目,需要搭建SSH(spring+struts+hibernate),在前一段时间我尝试着用直接向lib目录下添加jar 报的形式,可能由于经验不足,没有配置成功,再后来学会了使用maven进行jar报的管理,所以这次使用 maven+hibernate4+spring3+struts2,毫无疑问,刚开始就需要把相关的jar包通过maven进行导入,不过不得不说这些 相关jar包我也是在网上的一些源码上找的,pom.xml代码如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cct</groupId>
<artifactId>webFrame1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webFrame1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 配置了三個遠程倉庫 -->
<repositories>
<repository>
<id>maven</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>

<repository>
<id>alibaba-opensource</id>
<name>alibaba-opensource</name>
<url>http://code.alibabatech.com/mvn/releases/</url>
<layout>default</layout>
</repository>

<repository>
<id>alibaba-opensource-snapshot</id>
<name>alibaba-opensource-snapshot</name>
<url>http://code.alibabatech.com/mvn/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>

<!-- 设置编码集,utf-8 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- 导入相关jar包 -->
<dependencies>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- fastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.24</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- sql server数据库驱动 -->
<!-- <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.2.4</version> </dependency> -->
<!-- oracle数据库驱动 -->
<!-- <dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.1.0</version>
</dependency> -->

<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.8</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.0</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11-beta-1</version>
<scope>test</scope>
</dependency>
<!-- hibernate4 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.7.Final</version>
</dependency>
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<!-- spring3 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.11.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>

</dependencies>
<build>
<finalName>webFrame</finalName>
</build>
</project>

 下面是spring-hibernate.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: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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- druid数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />

<!-- 初始化连接大小 -->
<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="poolPreparedStatements" value="true" /> <property
name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />

<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />

<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
<!-- hbm方式配置,下面两种方式效果一样,在此之前都省去了hibernate.cfg,xml配置 -->
<!-- <property name="mappingDirectoryLocations">
<list>
<value>classpath*:com/cct/entity/User</value>
</list>
</property> -->
<property name="mappingResources">
<list>
<value>com/cct/entity/Department.hbm.xml</value>
<value>com/cct/entity/User.hbm.xml</value>
<value>com/cct/entity/Post.hbm.xml</value>
</list>
</property>
</bean>



<!-- 配置事务 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/></property>
</bean>


</beans>

 关于数据库连接部分是读取的一个资源文件,在spring.xml通过 

<!-- 引入属性文件 -->

  <context:property-placeholder location="classpath:config.properties"></context:property-placeholder>

<!--添加另外一份spring文件,分开写让逻辑更清晰-->

<import resource="spring-hibernate.xml"></import>

 在spring-hibernate.xml中定义的sessionfactory可以通过在dao层中的@Resource实现注解注入sessionfactory

@Resource
private SessionFactory sessionFactory ;

这样的话在到dao层中可以直接得到这个sessionfactory,而在此之前呢,是走过不少弯路的,或者说是钻牛角尖,在以前写的一些程序当中,习惯用的是hibernate3,这样的话在dao层中就可以直接继承HibernateDaoSupport,通过继承它,那么在dao的方法中可以通过super.gethibernateTemplate()中的方法轻松实现增删改查操作,但是而这次选择用hibernate4(简称h4)就像看看其中到底区别在哪,在spring的包中里面有hibernate3的支持和hibernate4的支持,而hibernate4中不再有HibernateDaoSupport和hibernateTemplate(),所以在h4项目的dao层中依旧可以继承HibernateDaoSupport(在spring-hibernate.xml中事务和sessionfactory是用的h4的class),但是在运行的时候就会报错:

java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 而这个显示的是h3里面的错,所以刚开始还以为是h3没能够拿到sessionfactory,于是想将sessionfactory传到hibernateTemplate()里面去,而通过源码发现hibernateTemplate()中的set方法是final修饰的,只能想着自己写个方法通过super.set*()并加上@resource(之前在spring-hibernate.xml中用h4定义的bean)的方式将其传进去,但是发现依旧是这个问题,除此之外,我还想着通过在spring-hibernate.xml中定义一个hibernatetemplate的bean(使用的h3的类),问题还是这个,当时钻着牛角尖,感觉整个人就不好了,不调出来睡觉都觉得不爽,调了一天,后面......也就想明白了,在h3和h4中,h4最大的不同就是对session的处理方式不一样,根本就不需要去继承HibernateDaoSupport,自己就能够独立并且很好的实现对session的处理,所以在项目开发的时候需要注意不能h3和h4混着用,至少我是有些顾虑了,由此写下这篇文章,希望能够让一些人少走一些弯路,如果有人实现了h3和h4的混合使用,还望赐教。