国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Spring4整合Hibernate4

2019-11-14 22:07:36
字體:
供稿:網(wǎng)友
SPRing4整合Hibernate4

首先,要明確Spring整合Hibernate可以做什么?

答案是:

  1.由IOC容器來管理Hibernate的sessionFactory  2.讓Hibernate使用上Spring的聲明式事務

整合的過程以一個實例來說明。

在整合的中,hibernate的一些配置都可以放在spring的配置文件中。但是為了使配置文件看起啦比較清晰,建議還是分開存放。

比如在db.properties中存放數(shù)據(jù)庫信息,hibernate.cfg.xml中存放hibernate的基本信息。其余的配置信息可以放在spring的配置文件(applicationContext.xml)中。

db.properties

1 jdbc.user=yulei2 jdbc.passWord=yulei3 jdbc.driverClass=Oracle.jdbc.driver.OracleDriver4 jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl5 6 jdbc.initialPoolSize=57 jdbc.maxPoolSize=10

hibernate.cfg.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6     <session-factory> 7         <!-- 配置連接數(shù)據(jù)庫的基本信息 --> 8         <!--  9             1.數(shù)據(jù)源配置到IOC容器中,所以此處不再需要配置數(shù)據(jù)源10             2.關聯(lián)的.hbm.xml也在IOC容器配置SessionFactory實例時再進行配置11             3.配置Hibernate的基本屬性:方言、SQL顯式及格式化、生成數(shù)據(jù)表的策略以及二級緩存等12          -->13         14     15         <!-- 配置hibernate基本信息 -->16         <!-- hibernate所使用的數(shù)據(jù)庫方言 -->17         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>18         19         <!-- 執(zhí)行操作時是否在控制臺打印sql -->20         <property name="show_sql">true</property>21         22         <!-- 是否對SQL進行格式化 -->        23         <property name="format_sql">true</property>24         25         <!-- 指定自動生成數(shù)據(jù)表的策略 -->26         <property name="hbm2ddl.auto">update</property>27         28         29     </session-factory>30 </hibernate-configuration>

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:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <!-- 配置自動掃描的包 -->    <context:component-scan base-package="com.yl.spring.hibernate"></context:component-scan>    <!-- 配置數(shù)據(jù)源 -->    <!-- 導入資源文件 -->    <context:property-placeholder location="classpath:db.properties"/>    <!-- 配置c3p0數(shù)據(jù)源 -->    <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.initialPoolSize}"></property>        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>    </bean>        <!-- 配置 Hibernate 的 SessionFactory 實例 : 通過Spring 提供的LocalSessionFactoryBean配置-->    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!-- 配置數(shù)據(jù)源屬性 -->        <property name="dataSource" ref="dataSource"></property>                <!-- 配置 hibernate 配置文件的名稱及位置 -->        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->        <!-- 使用hibernateProperties屬性來配置Hibernate原生的屬性 -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>                <!-- 配置 hibernate 映射文件的位置及名稱 , 可以使用通配符-->        <property name="mappingLocations" value="classpath:com/yl/spring/hibernate/entities/*.hbm.xml"></property>    </bean>        <!-- 配置 Spring 的聲明式事務 -->    <!-- 1.配置事務管理器 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!-- 2.配置事務屬性 , 需要事務管理器 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="get*" read-only="true"/>            <!-- 添加下面此行后改變了 事務的隔離級別 -->            <!-- <tx:method name="purchase" propagation="REQUIRES_NEW"/> -->            <tx:method name="*"/>        </tx:attributes>    </tx:advice>    <!-- 3.配置事務切點, 并把切點和事務屬性聯(lián)系起來 -->    <aop:config>        <aop:pointcut expression="execution(* com.yl.spring.hibernate.service.*.*(..))"             id="txPointcut"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>    </aop:config>        </beans>

以上三個配置文件基本上就是Spring整合Hibernate所需的配置。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东海县| 大邑县| 宜宾县| 钟山县| 临潭县| 临夏县| 永丰县| 英吉沙县| 忻州市| 安平县| 汾阳市| 饶阳县| 楚雄市| 隆昌县| 金阳县| 姜堰市| 察哈| 阳山县| 尉氏县| 佳木斯市| 若羌县| 蒙山县| 库伦旗| 吉林市| 延寿县| 新郑市| 东平县| 苍山县| 宿迁市| 澎湖县| 高雄县| 城口县| 新泰市| 五家渠市| 南投市| 龙井市| 收藏| 如东县| 双城市| 台东市| 新丰县|