• 从零配置hibernate


      hibernate的配置文件主要是两大块:hibernate.cfg.xml(主配置文件)和**.hbm.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>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
            <property name="connection.username">root</property>
            <property name="connection.password">pwd</property>
            
            <property name="hbm2ddl.auto">update</property>
            <mapping resource="cn/iot/domain/person.hbm.xml"></mapping>
        </session-factory>
        
    </hibernate-configuration>

      这个就是最基本的配置文件了,包含数据源的信息,由映射文件生成表以及映射文件。

      其他常用的配置还有连接池的配置以及二级缓存的配置

      虽然hibernate有内置的连接池,但是并不推荐使用,而且在hibernate3时,第三方的DBCP连接池也不推荐使用。下面主要说明常用的C3P0和Proxool的配置。

      C3P0的配置:

    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>    
    <property name="hibernate.c3p0.max_size">20</property>    
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.timeout">120</property>        <!--获得连接的最长时间-->  
    <property name="hibernate.c3p0.max_statements">100</property>     <!--最大的PreparedStatement数量-->
    <property name="hibernate.c3p0.idle_test_period">120</property>  <!--清理线程多长时间清理一次-->
    <property name="hibernate.c3p0.acquire_increment">2</property>  <!--连接用完后一次获取连接的数量-->

      proxool的配置: 首先配置好proxool.xml文件,再在hibernate.cfg.xml中导入

    <?xml version="1.0" encoding="UTF-8"?>      
    <!-- the proxool configuration can be embedded within your own application's.      
    Anything outside the "proxool" tag is ignored. -->      
    <something-else-entirely>    
        <proxool>    
            <!--连接池的别名-->    
            <alias>DBPool</alias>    
            <!--proxool只能管理由自己产生的连接-->    
            <driver-url>    
                jdbc:mysql://localhost:3306/WebShop?user=root&amp;password=&amp;useUnicode=true&amp;characterEncoding=GB2312
            </driver-url>    
            <!—JDBC驱动程序-->    
            <driver-class>com.mysql.jdbc.Driver</driver-class>    
            <driver-properties>    
                <property name="user" value="root"/>    
                <property name="password" value=""/>    
            </driver-properties>      
            <!-- proxool自动检查各个连接状态的时间间隔(毫秒),检查到空闲的连接就马上回收,超时的销毁-->      
            <house-keeping-sleep-time>90000</house-keeping-sleep-time>    
            <!-- 当没有空闲连接可以分配时在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->      
            <maximum-new-connections>20</maximum-new-connections>    
            <!-- 最少保持的空闲连接数-->      
            <prototype-count>5</prototype-count>    
            <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->      
            <maximum-connection-count>100</maximum-connection-count>    
            <!-- 最小连接数-->    
            <minimum-connection-count>10</minimum-connection-count>    
        </proxool>      
    </something-else-entirely> 

      在hibernate.cfg.xml中导入

    <?xml version='1.0' encoding='UTF-8'?>    
    <!DOCTYPE hibernate-configuration     
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"     
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">    
    <hibernate-configuration>    
        <session-factory>    
        <property name="hibernate.connection.provider_class">    
            org.hibernate.connection.ProxoolConnectionProvider     
        </property> 
        <!--在proxool.xml中取得别名-->
        <property name="hibernate.proxool.pool_alias">DBPool</property>  
        <property name="hibernate.proxool.xml">proxool.xml</property>    
        <property name="show_sql">true</property>    
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>      
        
        <mapping resource="" />    
    </session-factory>      
    </hibernate-configuration>    

    二级缓存的配置:常用的缓存如下

    组件 Provider类 类型 集群 查询缓存
    Hashtable org.hibernate.cache.HashtableCacheProvider 内存 不支持 支持
    EHCache org.hibernate.cache.EhCacheProvider 内存,硬盘 不支持 支持
    OSCache org.hibernate.cache.OSCacheProvider 内存,硬盘 支持 支持
    SwarmCache org.hibernate.cache.SwarmCacheProvider 集群 支持 不支持
    JBoss TreeCache org.hibernate.cache.TreeCacheProvider 集群 支持 支持

    下面用EHCache的配置来说明二级缓存的配置:

      配置二级缓存需要三步:1、配置ehcache.xml  2、配置hibernate.cfg.xml  3、配置**.hbm.xml

      首先配置ehcache.xml :

        <?xml version="1.0" encoding="UTF-8"?>  
        <ehcache>  
          <!--如果缓存中的对象存储超过指定的缓存数量的对象存储的磁盘地址-->  
          <diskStore path="D:/ehcache"/>  
       
          <!-- 默认cache:如果没有对应的特定区域的缓存,就使用默认缓存 
          eternal :设置是否永远不过期
          timeToIdleSeconds :对象处于空闲状态的最多秒数后销毁
          timeToLiveSeconds :对象处于缓存状态的最多秒数后销毁
          overflowToDisk :当缓存中的数量超出maxElementsInMemory时是否溢出到硬盘
          maxElementsInMemory :缓存在内存中的最大数目
          maxElementsOnDisk:缓存在磁盘上的最大数目
          memoryStoreEvictionPolicy:缓存算法,有LRU(默认)、LFU、LFU
        --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false"/> <!-- 指定区域cache:通过name指定,name对应到Hibernate中的区域名即可--> <cache name="cn.iot.student" eternal="false" maxElementsInMemory="100" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"> </cache> </ehcache>

      对于Ehcache的overflowToDisk如果这个属性为true,那么要求缓存的数据必须是可序列化的,如果不是可序列化的,ehcache将在日志中打印这个错误(文档中仅指出了这点),并且调用 memoryStoreEvictionPolicy设置的策略(例如LRU)移除内存中的一个缓存元素再放入新的Element,同时触发 CacheEventListener的notifyElementEvicted方法。

      然后再配置hibernate.cfg.xml

        <hibernate-configuration>  
           <session-factory>  
          
              ......  
          
              <!-- 开启二级缓存 -->  
              <property name="hibernate.cache.use_second_level_cache">true</property>  
              <!-- 启动"查询缓存"如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集,必须配置此项-->  
              <property name="hibernate.cache.use_query_cache">true</property>  
              <!-- 设置二级缓存插件EHCache的Provider类-->  
              <property name="hibernate.cache.provider_class">  
                 org.hibernate.cache.EhCacheProvider  
              </property>
              <!-- 二级缓存区域名的前缀 -->  
              <!--<property name="hibernate.cache.region_prefix">test</property>-->  
              <!-- 高速缓存提供程序 -->  
              <property name="hibernate.cache.region.factory_class">  
                 net.sf.ehcache.hibernate.EhCacheRegionFactory  
              </property>  
              <!-- Hibernate4以后都封装到org.hibernate.cache.ehcache.EhCacheRegionFactory -->  
              <!-- 指定缓存配置文件位置 -->  
              <property name="hibernate.cache.provider_configuration_file_resource_path">  
                 ehcache.xml  
              </property> 
              <!-- 强制Hibernate以更人性化的格式将数据存入二级缓存 -->  
              <property name="hibernate.cache.use_structured_entries">true</property>  
          
              <!-- Hibernate将收集有助于性能调节的统计数据 -->  
              <property name="hibernate.generate_statistics">true</property>  
          
              ......  
          
           </session-factory>  
        </hibernate-configuration>  

      最后是在每个实体的**.hbm.xml中为类或者集合配置缓存策略:

      

        <?xml version="1.0" encoding='UTF-8'?>  
        <!DOCTYPE hibernate-mapping PUBLIC  
                                    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                                    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
        <hibernate-mapping>      
           <class>  
               <!-- 设置该持久化类的二级缓存并发访问策略 read-only read-write nonstrict-read-write transactional-->  
               <class name="cn.java.test.model.User" table="TBL_USER">  
                      <cache usage="read-write"/>  
               ......    
           </class>  
        </hibernate-mapping>  

      到这里就完成了hibernate的一些基本配置。

     

  • 相关阅读:
    让PHP开发者事半功倍的十大技巧
    Sublime Text 3 快捷键总结
    localhost访问错误Forbidden You don't have permission to access / on this server.解决办法(亲测)
    html在一个页面显示另一个页面的部分内容
    db2许可证安装
    spring3中使用注解方式实现定时器调度任务
    c3p0详细设置
    hibernate.cfg.xml详细配置
    ExtJS学习(面板)
    汉诺塔算法
  • 原文地址:https://www.cnblogs.com/Jc-zhu/p/4538981.html
Copyright © 2020-2023  润新知