您的位置:首页 > 编程语言 > Java开发

spring4 hibernate4(5) 整合

2016-04-25 16:38 639 查看
纸上得来终觉浅
1.前面学了Spring具有两个重要特性,IOC和AOP,利用这些特性可以优化代码框架; 既然如此,那么在使用Hibernate时,就可以利用Spring框架对hibernate的框架进行优化。这就是Spring hibernate整合。

2.Spring可以优化Hibernate的哪些部分?

1)在未使用hibernate之前,session的获取一般通过单例模式从sessionFactory中获取,利用Spring,可以直接将sessionFactory交给IOC容器来管理;

2)Spring  AOP的一个重要应用是声明式事务,这也可以在hibernate上使用,方便了事务管理。

3.下面是配置步骤:

1)加入hibernate 4和spring 4的jar包,(需要使用的(required文件夹下)都加上,否则后面整合后运行会有错误)

2)src目录下添加hibernate配置文件hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--     /*1.数据源的配置放在Spring中*/ -->

<!--     /*2.bibernate本身属性的配置留下*/ -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!--     <property name="current_session_context_class">thread</property> -->

<!--     /*3.关于hbm.xml(映射文件由Spring来管理)*/ -->
</session-factory>
</hibernate-configuration>
3)src下添加Spring配置文件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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 扫描的包 -->
<context:component-scan base-package="roadArchitectWeb.Test"></context:component-scan>

<!--导入数据源配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

<!-- 配置Hibernate的SessionFactory实例,通过Spring提供的LocalSessionFactoryBean来配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate配置文件的位置以及名称 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:roadArchitectWeb/Test/*.hbm.xml"></property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 启用注解来管理事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


sessionFactory和事务都是都这个文件来配置的。这个是整合的两个核心; 另外针对每个类的hbm.xml也是由这个文件来管理的。

4)其中db.properties文件在src下建立:

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///roadArchitectWeb

jdbc.initPoolSize=5
jdbc.maxPoolSize=10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息