`

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.

分享到:
评论

相关推荐

    Hibernate4 + Ehcache 例子

    NULL 博文链接:https://ligf06.iteye.com/blog/1711161

    hibernate+ehcache代码示例

    hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码

    struts2+spring+hibernate集成例子(包含所有jar包,ehcache二级缓存)

    struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建

    struts1.x+spring+hibernate集成例子(包含所有jar包,ehcache二级缓存)

    struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建

    JSF+Spring+Hibernate小例子

    基于网上很多朋友在问JSF+Spring+Hibernate的使用方法,于是抽空写了个小例子希望大家提出宝贵意见。 采用DBUnit测试 mysql数据库脚本: 新建test数据库,初始化脚本 create table tt(id int primary key,name ...

    (2.0版本)自己写的struts2+hibernate+spring实例

    spring-plugin-2.0.11.1 antlr-2.7.5H3.jar asm.jar asm-attrs.jar cglib-2.1.3.jar commons-collections-2.1.1.jar dom4j-1.6.1.jar ehcache-1.1.jar hibernate3.jar jaas.jar...

    Hibernate4注解+Struts2例子

    Hibernate4注解+Struts2的例子,里面详细介绍了怎么正确搭建Hibernate4,怎么使用注解生成实体类,怎么使用Hibernate4的二级缓存Ehcache,完整的增删查改功能,附带扁平化列表的效果图和里面关键配置的学习文档,让...

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    例子:xwork-2.1.2.jar在2.1.8中xwork-core-2.1.6.jar 具体啥问题没测过 3 hibernate3相关包 antlr-2.7.2.jar //2 aopalliance-1.0.jar //2 asm-attrs.jar //3 asm.jar // 3 cglib-2.1.3.jar //3 commons-...

    jeeplus版多商家商城

    springmvc,mybatis,mybatis-plus,spring,beetl,hibernate-validator ehcache,controller层,map+warpper返回方式介绍 map+warpper方式即为把controller层的返回结果使用BeanKit工具类把原有bean转化为Map的的...

    JAVA上百实例源码以及开源项目源代码

    1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...

    JAVA上百实例源码以及开源项目

    1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...

Global site tag (gtag.js) - Google Analytics