zoukankan      html  css  js  c++  java
  • 3、Spring整合Hibernate

    经过前面的两节分析:1、Hibernate之生成SessionFactory源码追踪 和 2、Spring的LocalSessionFactoryBean创建过程源码分析 。我们可以得到这样一个结论,spring的LocalSessionFactoryBean具体是调用Hibernate的Configuration中configure(...)方法来读取并解析xxx.cfg.xml文件的,同样也会得到一个原生态的org.hibernate.cfg.Configuration 和 org.hibernate.SessionFactory属性。

    所以,我们可以看见LocalSessionFactoryBean中保存有hibernate的configuration、sessionFactory、properties 以及 metadataSourceQueue。

    从前面两节分析中我们知道,LocalSessionFactoryBean会读取并解析xxx.cfg.xml和xxx.hbm.xml文件,所以在配置LocalSessionFactoryBean的时候需要明确指定这两个文件的位置。当然,连接数据库的dataSource也要配置在spring的bean中:

    下面是一个spring整合hibernate的实例:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    11 
    12     <!-- 配置自动扫描的包 -->
    13     <!-- 实体和dao -->
    14     <context:component-scan base-package="comentity,com.dao.impl"></context:component-scan>
    15     <!-- service -->
    16     <context:component-scan base-package="com.service.impl"></context:component-scan>
    17 
    18     <!-- 导入数据库资源文件 -->
    19     <context:property-placeholder location="classpath:db.properties"/>
    20     
    21     <!-- 配置c3p0数据源 -->
    22     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    23         <property name="user" value="${jdbc.user}"></property>
    24         <property name="password" value="${jdbc.password}"></property>
    25         <property name="driverClass" value="${jdbc.driverClass}"></property>
    26         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    27         
    28         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
    29         <property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
    30         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    31         <property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property>
    32     </bean>
    33     
    34     <!-- 配置sessionFacory -->
    35     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    36         <property name="dataSource" ref="dataSource"></property>
    37         <!-- 如果有多个配置文件,可以使用属性configLocations来配置,多个配置文件之间用逗号“,”来分割,
    38             如:classpath:hibernate.cfg.xml,classpath:extension.cfg.xml -->
    39         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    40         <!-- ORM映射关系配置文件 -->
    41         <property name="mappingLocations" value="classpath:com/gzpp123/web/entity/*.hbm.xml"></property>
    42     </bean>
    43     
    44     <!-- 配置spring的声明式事物 -->
    45     <!-- 1、配置hibernate的事物管理器 -->
    46     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    47         <property name="sessionFactory" ref="sessionFactory"></property>
    48     </bean>
    49     
    50     <!-- 2、配置事物属性 -->
    51     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    52         <tx:attributes>
    53             <tx:method name="get*" read-only="true"/>
    54             <tx:method name="*"/>
    55         </tx:attributes>
    56     </tx:advice>
    57     
    58     <!-- 3、配置事物切入点,再把事物属性和事务切入点关联起来 -->
    59     <aop:config>
    60         <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/>
    61         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    62     </aop:config>
    63     
    64     <aop:config>
    65         <aop:pointcut expression="execution (* com.service.CoreService.*(..))" id="txPointcut1"/>
    66         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut1"/>
    67     </aop:config>
    68 
    69 </beans>

    再看看类路径下的db.properties文件:

    jdbc.user=root
    jdbc.password=tiger123
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/gzpp
    
    jdbc.initialPoolSize=2
    jdbc.maxPoolSize=10
    jdbc.minPoolSize=1
    jdbc.acquireIncrement=5
    #...
    #appSecret: 62e5c0141c2fc9a3bc9d2ae73fb7cd12
    #appid: wx15fc2152e1406d02

    由于mapping以及dataSource都在spring中配置完成了,所以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     
     8         <!-- 配置 hibernate 的基本属性 -->
     9     
    10         <!-- 方言 -->
    11         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    12         
    13         <!-- 是否显示及格式化 SQL -->
    14         <property name="hibernate.show_sql">true</property>
    15         <property name="hibernate.format_sql">true</property>
    16     
    17         <!-- 生成数据表的策略 -->
    18         <property name="hibernate.hbm2ddl.auto">update</property>
    19         
    20         <!-- 二级缓存相关 -->
    21     
    22     </session-factory>
    23 </hibernate-configuration>
  • 相关阅读:
    JSP技术
    Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Zuul【文件上传】
    Zuul【限流】
    Zuul【自定义Filter】
    Zuul【工作原理】
    Zuul【基础配置】
    Zuul【入门】
    Hystrix【参数配置及缓存】
    Hystrix【异常机制处理】
  • 原文地址:https://www.cnblogs.com/lj95801/p/5396903.html
Copyright © 2011-2022 走看看