- 浏览: 204104 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (397)
- j2se (28)
- nio (3)
- 易错点 (3)
- 面试ssh (9)
- ssh整合 (11)
- jbpm+spring (2)
- js (15)
- 高级技术 (59)
- swing (3)
- 数据库 (16)
- hibernate (18)
- spring (19)
- 开发网站知识点 (9)
- jbpm (4)
- json (5)
- 设计模式 (22)
- 自定义标签 (1)
- j2ee (9)
- lucene (3)
- cahce (11)
- maven (5)
- html5 (1)
- 多数据源 (10)
- 页面聊天 (9)
- 富客户端 (1)
- android (13)
- aop+拦截器+jms (13)
- 框架整合 (1)
- 非阻塞io (24)
- 暂时不看 (13)
- webservice (3)
- oracle (3)
- 算法 (4)
- 协程 (2)
- netty (1)
- 爬虫 (0)
- 高级基础 (1)
- JVM调优总结 (12)
- 知识点技巧 (1)
- REST (0)
- 基础 io (2)
- dubbo (8)
- 线程 (1)
- spring源码 (2)
- git (1)
- office (2)
最新评论
-
sjzcmlt:
,写的挺好的啊
一个完整的负载均衡的例子 . -
他大姨妈:
网上大部分例子都是直接通过IdleStateHandler来实 ...
Netty的超时机制 心跳机制
这是个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.
数据库使用的是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.
发表评论
-
hibernate抓取策略fetch=select /join/subselect
2016-04-10 11:24 546出处:http://blog.csdn.net/ychato ... -
hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)
2016-04-09 22:23 460收藏自:http://www.360doc.com/cont ... -
hibernate实现JTA事物--代码
2014-11-08 16:15 446package com.ajita.jta; impo ... -
Hibernate的三种连接池设置C3P0、Proxool和DBCP
2014-11-08 16:16 415Xml代码 <!-- JDBC驱动程序 --& ... -
EHCache的使用
2014-05-22 11:39 463在开发高并发量,高性 ... -
缓存 hibernate
2014-06-17 09:40 4241. Session---单数据加载---load/ ge ... -
spring+ehcache实现的缓存查询
2014-06-17 09:40 527最近项目有一个需求,就是用户在查询界面,输入很多查询条件之后, ... -
Hibernate使用EHCache二级缓存 .
2014-05-12 10:30 428数据库结构: create table teamEH ... -
hibernate ehcache
2014-05-12 10:01 4011.EhCache是什么 EhCac ... -
Hibernate中cascade和inverse的作用
2014-05-07 10:38 502Inverse和cascade是Hibernate映射中最难掌 ... -
Hibernate中inverse的用法 .
2014-05-07 10:40 409一、Inverse是hibernate双向关系中的基本概念。i ... -
关联关系
2014-04-22 21:52 293一对一单向外键关联 (学生卡表里有“studentId”字段) ... -
Hibernate一对多,多对一,多对多,一对一关系汇总
2014-05-07 10:42 557一对多 ◆name:集合属性的名称(也可以理解为一对多中那个 ... -
HibernateTemplate+HibernateDaoSupport+SessionFactory
2014-04-19 20:13 469HibernateTemplate @Component ... -
数据库事物
2014-06-17 09:40 3301. 脏读 :脏读就是指当 ... -
Open Session In View
2014-06-17 09:40 430从昨天下午一直纠结到现在,原来是项目启用了Open Sessi ... -
Hibernate主键生成策略
2014-06-20 09:33 2581、自动增长identity 适用于MySQL、DB2、MS ...
相关推荐
NULL 博文链接:https://ligf06.iteye.com/blog/1711161
hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
基于网上很多朋友在问JSF+Spring+Hibernate的使用方法,于是抽空写了个小例子希望大家提出宝贵意见。 采用DBUnit测试 mysql数据库脚本: 新建test数据库,初始化脚本 create table tt(id int primary key,name ...
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,怎么使用注解生成实体类,怎么使用Hibernate4的二级缓存Ehcache,完整的增删查改功能,附带扁平化列表的效果图和里面关键配置的学习文档,让...
例子: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-...
springmvc,mybatis,mybatis-plus,spring,beetl,hibernate-validator ehcache,controller层,map+warpper返回方式介绍 map+warpper方式即为把controller层的返回结果使用BeanKit工具类把原有bean转化为Map的的...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...