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);//打开二级缓存