hibernate + ehcache的例子

这是个hibernate + ehcache的例子,目前使用最新的hibernate-core.4.1.7.Final.jar + ehcache-core.2.6.0.jar



数据库使用的是mysql.

Xml代码
1.<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
2. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
3. <property name="connection.url">jdbc:mysql://localhost/test</property>
4. <property name="connection.username">root</property>
5. <property name="connection.password">admin</property>
<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/test</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
数据库脚本文件/src/main/resources/sql/*.sql



hibernate配置文件有:

1、/src/main/resources/hibernate.cfg.xml

2、/src/main/resources/ehcache.xml



datamapping 文件有:

1、/src/main/demo.pojo/Airport.hbm.xml

2、/src/main/demo.pojo/Country.hbm.xml

3、/src/main/demo.pojo/Employee.hbm.xml

4、/src/main/demo.pojo/Language.hbm.xml



详细请看附件的例子,数据库配置好后可以直接运行。



运行结果:

1、CountryDAOTest.java

测试代码:

Java代码
1.CountryDAO dao = new CountryDAO();
2. for(int i = 1; i <= 5; i++) {
3. Transaction tx = SessionManager.getSession().beginTransaction();
4. TestTimer timer = new TestTimer("testGetCountries");
5. List countries = dao.getCountries();
6. tx.commit();
7. timer.done();
8. SessionManager.closeSession();
9. assertNotNull(countries);
10. assertEquals(countries.size(),229);
11. }
CountryDAO dao = new CountryDAO();
for(int i = 1; i <= 5; i++) {
Transaction tx = SessionManager.getSession().beginTransaction();
TestTimer timer = new TestTimer("testGetCountries");
List countries = dao.getCountries();
tx.commit();
timer.done();
SessionManager.closeSession();
assertNotNull(countries);
assertEquals(countries.size(),229);
}DAO代码:

Java代码
1.public List getCountries() {
2. return SessionManager.currentSession()
3. .createQuery(
4. "from Country as c order by c.name")
5. .setCacheable(true)
6. .list();
7.}
public List getCountries() {
return SessionManager.currentSession()
.createQuery(
"from Country as c order by c.name")
.setCacheable(true)
.list();
}注意:只有.setCacheable(true) ,此时的查询结果才会缓存,否则不会缓存。



Country.hbm.xml配置如下:



Xml代码
1.<hibernate-mapping package="demo.pojo">
2. <class name="Country" table="COUNTRY" dynamic-update="true">
3. <meta attribute="implement-equals">true</meta>
4. <cache usage="read-only"/> <!-- 必须配置 -->
5.
6. <id name="id" type="long" unsaved-value="null" >
7. <column name="cn_id" not-null="true"/>
8. <generator class="increment"/>
9. </id>
10.
11. <property column="cn_code" name="code" type="string"/>
12. <property column="cn_name" name="name" type="string"/>
13.
14. <set name="airports" >
15. <cache usage="read-only"/>
16. <key column="cn_id"/>
17. <one-to-many class="Airport"/>
18. </set>
19. </class>
20.</hibernate-mapping>
<hibernate-mapping package="demo.pojo">
<class name="Country" table="COUNTRY" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<cache usage="read-only"/> <!-- 必须配置 -->

<id name="id" type="long" unsaved-value="null" >
<column name="cn_id" not-null="true"/>
<generator class="increment"/>
</id>

<property column="cn_code" name="code" type="string"/>
<property column="cn_name" name="name" type="string"/>

<set name="airports" >
<cache usage="read-only"/>
<key column="cn_id"/>
<one-to-many class="Airport"/>
</set>
</class>
</hibernate-mapping>
输出如下:

可以看到查询语句只执行一次,其他4次的数据全部从缓存中获取。

Hibernate: select country0_.cn_id as cn1_0_, country0_.cn_code as cn2_0_, country0_.cn_name as cn3_0_ from COUNTRY country0_ order by country0_.cn_name
testGetCountries : 281 ms.
testGetCountries : 16 ms.
testGetCountries : 31 ms.
testGetCountries : 0 ms.
testGetCountries : 16 ms.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值