hibernate4 二级缓存demo实例

Hibernate4 所需jar

导入/lib/required文件夹下的所有jar包

  配置连接池需要导入/lib/optional/c3p0文件夹中的所有jar包

  数据库使用的是MYSQL,因此还需要mysql的连接驱动:mysql-connector-java-5.1.22-bin.jar

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>  
            <!-- Database connection settings -->  
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
            <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate4</property>  
            <property name="connection.username">root</property>  
            <property name="connection.password">root</property>  
            <!--配置c3p0连接池-->
            
           <!-- Enable c3p0 connection pooling,beacause hibernate pooling is not prod-ready. 
            Apparently connection.provider_class is needed in hibernate 3+ -->
            <!-- <property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> -->
            <property name="hibernate.c3p0.min_size">1</property>
            <property name="hibernate.c3p0.max_size">100</property>
            <property name="hibernate.c3p0.idle_test_period">30</property>

  <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> --> <!-- 配置二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- hibernate3的二级缓存配置 --> <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> --> <!-- 开启查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="com.test.pojo.User" /> </session-factory> </hibernate-configuration>


注意:hibernate4和hibernate3配置不一样,hibernate4是
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> 
而hibernate3的配置是
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值