最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring+hibernate 兩種整合方式配置文件的方法

 更新時間:2017年04月06日 09:26:32   作者:QH_JAVA  
本篇文章主要介紹了spring+hibernate 兩種整合方式配置文件的方法,主要有兩種方式 1、注解方式 2、xml方式實現(xiàn),有興趣的可以了解一下。

之前的文章都是講解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因為工作的需要,最近在使用hibernate 所以下面我們來看看 spring整合hibernate的配置文件,這里只說spring+hibernate 的配置文件而不說springmvc 因為這些是不用變的。

spring整合hibernate 有兩種方式 1、注解方式 2、xml方式實現(xiàn)

1、注解方式實現(xiàn):

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: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/tx 
     http://www.springframework.org/schema/tx/spring-tx.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"> 
  <context:component-scan base-package="com.test" /> 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations"> 
    <list> 
      <value>classpath:jdbc.properties</value> 
    </list> 
   </property> 
  </bean> 
  <bean id="c3p0DataSource" destroy-method="close" 
    class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    <property name="driverClass" value="${driverClass}" /> 
    <property name="jdbcUrl" value="${url}" /> 
    <property name="user" value="${user}" /> 
    <property name="password" value="${password}" /> 
    <property name="initialPoolSize" value="${initialPoolSize}" /> 
    <property name="minPoolSize" value="${minPoolSize}" /> 
    <property name="maxPoolSize" value="${maxPoolSize}" /> 
    <property name="maxIdleTime" value="${maxIdleTime}" /> 
  </bean>          
  <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="c3p0DataSource" /> 
    <property name="packagesToScan"> 
      <list> 
        <value>com.test.bean</value> 
      </list> 
    </property> 
    <property name="hibernateProperties"> 
      <props> 
        <prop key="hibernate.dialect">${dialect}</prop> 
        <prop key="hibernate.show_sql">${show_sql}</prop> 
        <prop key="hibernate.format_sql">${format_sql}</prop> 
        <prop key="hibernate.use_sql_commants">${use_sql_comments}</prop> 
        <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop> 
      </props> 
    </property> 
  </bean> 
  <bean id="txManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
  <tx:advice id="txAdvice" transaction-manager="txManager"> 
    <tx:attributes> 
      <tx:method name="get*" read-only="true" /> 
      <tx:method name="*" /> 
    </tx:attributes> 
  </tx:advice> 
  <aop:config> 
    <aop:pointcut id="bizMethods" expression="execution(* com.test.biz.*.*(..))" /> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" /> 
  </aop:config> 
</beans> 

2.xml方式實現(xiàn)

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: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/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd 
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop.xsd"> 
      
  <!-- 讓spring 去讀取指定路徑下的資源文件 --> 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations" value="classpath:jdbc.properties"/> 
  </bean> 
   
  <!-- 配置c3p0連接池 --> 
  <bean id="c3p0Source" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
   <property name="driverClass" value="${driverClass}" /> 
   <property name="jdbcUrl" value="${url}" /> 
   <property name="user" value="${user}" /> 
   <property name="password" value="${password}" /> 
   <property name="initialPoolSize" value="${initialPoolSize}" /> 
   <property name="minPoolSize" value="${minPoolSize}" /> 
   <property name="maxPoolSize" value="${maxPoolSize}" /> 
   <property name="maxIdleTime" value="${maxIdleTime}" /> 
  </bean> 
   
  <!-- 配置SessionFactory --> 
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
   <property name="dataSource" ref="c3p0Source" /> 
   <property name="mappingResources"> 
     <list> 
      <value>/com/cdzg/spring/bean/User.hbm.xml</value> 
     </list> 
   </property> 
   <property name="hibernateProperties"> 
    <props> 
        <prop key="hibernate.dialect">${dialect}</prop> 
        <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop> 
        <prop key="hibernate.show_sql">${show_sql}</prop> 
        <prop key="hibernate.format_sql">${format_sql}</prop> 
        <prop key="hibernate.use_sql_comments">${use_sql_comments}</prop> 
      </props> 
   </property> 
  </bean> 
   
  <!-- 配置事務管理器 --> 
  <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
   
  <!-- 定義事務通知 --> 
  <tx:advice id="txAdvice" transaction-manager="txManager"> 
   <tx:attributes> 
    <tx:method name="get*" read-only="true"/> 
    <tx:method name="*"/> 
   </tx:attributes> 
  </tx:advice> 
    
   <!-- 定義事務切面,并應用事務通知 -->   
   <aop:config> 
   <aop:pointcut id="xxxBizImpl" expression="execution(* com.cdzg.spring.biz.*.*(..))"/> 
   <aop:advisor pointcut-ref="xxxBizImpl" advice-ref="txAdvice"/> 
   </aop:config> 
      
  <bean id="userDaoImpl" class="com.cdzg.spring.dao.impl.UserDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
  </bean> 
  <bean id="userBizImpl" class="com.cdzg.spring.biz.impl.UserBizImpl"> 
    <property name="userDao" ref="userDaoImpl" /> 
  </bean> 
  <bean id="userAction" class="com.cdzg.spring.web.actions.UserAction"> 
    <property name="userBiz" ref="userBizImpl" /> 
  </bean> 
</beans> 

兩種配置最大的區(qū)別就是注解方式不用在寫O/R映射配置文件而xml方式實現(xiàn)的要配置O/R映射配置文件

注解的這種方式,直接掃描bean包就可以,剩下的對應關系由框架完成

而xml配置方式要配置O/R 映射文件并在這里指定文件,如果多的話可以使用通配符 "*"

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring Boot搭建文件上傳服務的方法

    Spring Boot搭建文件上傳服務的方法

    這篇文章主要為大家詳細介紹了Spring Boot搭建文件上傳服務的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java對世界不同時區(qū)timezone之間時間轉換的處理方法

    Java對世界不同時區(qū)timezone之間時間轉換的處理方法

    這篇文章主要介紹了Java對世界不同時區(qū)timezone之間時間轉換的處理方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • IntelliJ IDEA失焦自動重啟服務的解決方法

    IntelliJ IDEA失焦自動重啟服務的解決方法

    在使用 IntelliJ IDEA運行 SpringBoot 項目時,你可能會遇到一個令人困擾的問題,一旦你的鼠標指針離開當前IDE窗口,點擊其他位置時, IDE 窗口會失去焦點,你的 SpringBoot 服務就會自動重啟,所以本文給大家介紹了IntelliJ IDEA失焦自動重啟服務的解決方法
    2023-10-10
  • 深入理解Java Socket通信

    深入理解Java Socket通信

    本篇文章主要介紹了深入理解Java Socket,Java中的網(wǎng)絡通信是通過Socket實現(xiàn)的,Socket分為ServerSocket和Socket兩大類,有興趣的可以了解一下
    2017-02-02
  • Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法

    Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法

    這篇文章主要介紹了Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • 淺析如何高效格式化輸出JSON字符串

    淺析如何高效格式化輸出JSON字符串

    JSON(JavaScript?Object?Notation)作為一種輕量級的數(shù)據(jù)交換格式,已經(jīng)成為了各種編程語言間傳遞數(shù)據(jù)的標準,下面我們就來聊聊如何高效格式化輸出JSON字符串吧
    2025-01-01
  • Java線程協(xié)調(diào)運行操作實例詳解

    Java線程協(xié)調(diào)運行操作實例詳解

    這篇文章主要介紹了Java線程協(xié)調(diào)運行操作,結合具體實例形式詳細分析了Java線程協(xié)調(diào)運行原理、實現(xiàn)方法及相關操作注意事項,需要的朋友可以參考下
    2019-09-09
  • Springboot實現(xiàn)緩存預熱的方法

    Springboot實現(xiàn)緩存預熱的方法

    在系統(tǒng)啟動之前通過預先將常用數(shù)據(jù)加載到緩存中,以提高緩存命中率和系統(tǒng)性能的過程,緩存預熱的目的是盡可能地避免緩存擊穿和緩存雪崩,這篇文章主要介紹了Springboot實現(xiàn)緩存預熱,需要的朋友可以參考下
    2024-03-03
  • Java將時間戳轉換為Date對象的方法小結

    Java將時間戳轉換為Date對象的方法小結

    在?Java?編程中,處理日期和時間是一個常見需求,特別是在處理網(wǎng)絡通信或者數(shù)據(jù)庫操作時,本文主要為大家整理了Java中將時間戳轉換為Date對象的方法,希望對大家有所幫助
    2024-12-12
  • IDEA整合Dubbo+Zookeeper+SpringBoot實現(xiàn)

    IDEA整合Dubbo+Zookeeper+SpringBoot實現(xiàn)

    初學者,想自己動手做一個簡單的demo,本文主要介紹了IDEA整合Dubbo+Zookeeper+SpringBoot實現(xiàn),需要的朋友們下面隨著小編來一起學習學習吧
    2021-06-06

最新評論

姚安县| 明水县| 万州区| 龙口市| 开平市| 绥江县| 赫章县| 宝鸡市| 外汇| 定远县| 衡东县| 东兴市| 梁平县| 临桂县| 吉木乃县| 宜州市| 延川县| 桃源县| 湖口县| 苏尼特右旗| 九龙城区| 闻喜县| 策勒县| 蒲江县| 句容市| 延边| 泽库县| 华蓥市| 北安市| 龙井市| 广灵县| 高陵县| 慈溪市| 勐海县| 开封县| 泾川县| 新建县| 白河县| 安阳市| 新和县| 泗洪县|