首先准备开发环境,导入jar包和配置文件,这里配置jar包的方法使用eclipse配置导入jar包这里可以参考我之前的文章里面有配置的具体步骤:http://blog.csdn.net/weixin_39135506/article/details/75095294
这里使用到的jar包如下图:
Hibernate核心jar包
Spring的一些jar包
c3p0的c3p0-0.9.5.2.jar和mchange-commons-java-0.2.11.jar
还有一些配置文件和jar包
我的目录结构如下图
DeptDao.java代碼
Dept.java
Dept.hbm.xml
DeptService.java
bean.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: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 id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql:///hib_demo?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="12345678"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- Dao实例 -->
<bean id="deptDao" class="dao.DeptDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Service实例 -->
<bean id="deptService" class="service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- Spring和Hibernate整合 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<!-- hibernate常用配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate映射配置 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:entity/</value>
</list>
</property>
</bean>
<!-- 事务配置 -->
<!-- a. 配置事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- c. Aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* service.DeptService.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
App.java
原先的数据库结构如下
运行后结果
数据库没有插入的数据
将DeptService.java的代码修改,如图
这样就不会产生异常运行结果如下
数据库插入一条数据如下:
这样spring4+hibernate5的整合就结束了