Hibernate简单例子(二级缓存)

PO------------------

class UserPO{

  private int u_id;

  private String username;

  private String pwd;

setter();

getter();

}

 

UserPO.hbm.xml----------------------//

<hibernate-mapping>

   <class name="PO类路径" table="t_user">

     <id name="u_id" column="u_id" type="int">

        <property class="id如何自增"></>

     </>

     <property name="username" column="username" type="string"></>

     <property name="pwd" column="pwd" type="string"></>

   </>

</>

 

hibernate.xml-------------------------------

<hibernate-configuration>

  <session-factory>

<!--MySQL-->

    <property name="connection.username">root</property>
  <property name="connection.password">sa</property>
  <property name="connection.url">
   jdbc:mysql://localhost:3306/test
  </property>
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="myeclipse.connection.profile">test55</property>
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <property name="show_sql">true</property>

 

    <mapping resource="hbm路径"/>

  </>

</>

 

<!--配置二级缓存-->

ehcache.xml-----------------------------------------

<ehcache>
 <defaultCache maxElementsInMemory="1000" eternal="false"
  overflowToDisk="true" timeToIdleSeconds="180" timeToLiveSeconds="300"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
 <cache name="longTime" maxElementsInMemory="100" eternal="false"
  overflowToDisk="true" timeToIdleSeconds="1800" timeToLiveSeconds="3000"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />  
</ehcache>

 

在hibernate.xml中加人=====>

  <!--配缓存-->
  <property name="hibernate.cache.EhCacheProvider">true</property>
  <property name="cache.provider_class">
   org.hibernate.cache.EhCacheProvider
  </property>
  <!--查询缓存-->
  <property name="hibernate.cache.use_query_cache">true</property>

  <mapping resource="hbm路径"/>

 

Test----------------

Configuration config=new Configuration();

config.configure("hibernate路径");

SessionFactory sessionfactory=config.buildSessionFactory();

Session ss=sessionFactory.openSession();

Criteria c=s.createCriteria(UserPO.class);
  c.setCacheable(true);//打开二级缓存

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值