Hibernate的核心:java
類:mysql
SessionFactorysql
Session數據庫
配置文件:緩存
classpath:hibernate.cfg.xmlapp
classpath:hibernate.properties - 可選的框架
緩存:dom
一級緩存 Sessioneclipse
二級緩存 SessionFactory 必需要使用ehcache框架 - ehcache.xmlmaven
查詢
QBC/HQL/Native-SQL
對象狀態:新建,瞬時,脫管
如下是具體操做
1:如何建立一個Hiberante的項目
方式:
1:添加hiberante\lib\required中的全部包
2:maven
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
2:如何使用Hibernate.cfg.xml文件鏈接數據庫
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:///hib?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">e23456</property>
3:如何使用c3p0鏈接數據庫
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
在cfg.xml中添加c3p0的配置:
<property name="hibernate.c3p0.max_size">3</property>
<property name="hibernate.c3p0.timeout">3000</property>
4:如何建立一個表的單表的影射
1:先建立表 - 再建立Bean - 再建立hbm.xml - 添加到cfg.xml中。
2:先建立Bean - >再建立hbm.xml - >添加到cfg.xml中去。配置自動的生成表
<property name="hbm2ddl.auto">update</property>
3:使用反向工程
1:先使用eclipse鏈接數據庫
第二步:使用Hiberante鏈接數據
<!-- 添加影射 -->
<mapping resource="cn/ql/domain/Studs.hbm.xml" />
5:更多影射
組件影射 ?
一對多
多對多
一對一
6:如何生成主鍵
7:如何進行CRUD
Session.save/update/delete/
Criteria/Query/SQLQuery
8:什麼是一級緩存特色
9:如何配置二級緩存
添加ehcache的包
Maven:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.1.0.Final</version>
</dependency>
添加ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
</ehcache>
<!-- 打開二級緩存 -->
<!-- 打開二級緩存 -->
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
配置對哪個類進行二級緩存
<!-- 配置對哪個類進行二級緩存 -->
<class-cache usage="read-only" class="cn.ql.domain.Studs"/>
測試及統計是否命中了二級緩存:
Session s1 = HibernateUtils.openSession();
Studs studs1 = s1.get(Studs.class, "S002");
s1.close();
Session s2 = HibernateUtils.openSession();
Studs studs2 = s2.get(Studs.class, "S002");
s2.close();
測試後臺只有一個SQL語句:
Session.delete/update
<!-- 配置對哪個類進行二級緩存 -->
<class-cache usage="read-write" class="cn.ql.domain.Studs" />
Session s1 = HibernateUtils.openSession();
Studs studs1 = s1.get(Studs.class, "S001");//select..
s1.close();
Session s3 = HibernateUtils.openSession();
Studs studs3 = s3.get(Studs.class, "S001");
s3.beginTransaction();
//studs3.setStudName("張三");
//s3.update(studs3);//update.. - 沒有刪除二級緩存中的數據,對二級緩存中的數據進行了更新操做.
s3.delete(studs3);
s3.getTransaction().commit();
s3.close();
Session s2 = HibernateUtils.openSession();
Studs studs2 = s2.get(Studs.class, "S001");//仍是命中二級緩存
s2.close();
Criteria/Query/SQLQuery -默認不會訪問二級緩存。
配置打開查詢的二緩存:
<!-- 打開查詢緩存 -->
<property name="hibernate.cache.use_query_cache">true</property>
在代碼上也設置當前的QBC/HQL能夠訪問二級緩存:
Session s1 = HibernateUtils.openSession();
Studs studs1 = (Studs) s1.createCriteria(Studs.class)//
.add(Restrictions.idEq("S001"))//
.setCacheable(true)//能夠訪問二級緩存
.uniqueResult();
s1.close();
Session s3 = HibernateUtils.openSession();
s3.beginTransaction();
s3.createQuery("delete from Studs where id='S001'")
.setCacheable(true)//不管是否setCacheable都會刪除放到二級緩存中的對象
.executeUpdate();
s3.getTransaction().commit();
s3.close();
Session s2 = HibernateUtils.openSession();
Studs studs2 = (Studs) s2.createCriteria(Studs.class)//
.add(Restrictions.idEq("S001"))//
.setCacheable(true)//能夠訪問二級緩存
.uniqueResult();
System.err.println("studs2 is:"+studs2);
s2.close();
<!-- 配置對集合進行緩存 -->
<collection-cache usage="read-only" collection="cn.ql.domain.Persons.cars"/>
<class-cache usage="read-write" class="cn.ql.domain.Cars"/> 對類進行緩存
<!-- 配置對集合進行緩存 -->
<collection-cache usage="read-only" collection="cn.ql.domain.Persons.carses"/> 只對集合中的對象的id進行緩存
1:cfg.xml中同上
建議.
2:在*.hbm.xml
<class name="cn.ql.domain.Cars" table="cars">
<cache usage="read-only"/>
3:註解
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Persons implements java.io.Serializable {
private String personId;
private String personName;
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
<class-cache usage="read-write" class="cn.ql.domain.Studs" />
<class-cache usage="read-write" class="cn.ql.domain.Persons"/>
<class-cache usage="read-write" class="cn.ql.domain.Cars"/>
<!-- 配置對集合進行緩存 -->
<collection-cache usage="read-only" collection="cn.ql.domain.Persons.carses"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
應該對每個類都配置緩存
<!-- 對某個一類進行緩存策略的配置 -->
<cache name="cn.ql.domain.Persons"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
是否在hiberante中全部的類,都要示配置二級緩存:
不行。
應該只對哪些常常查詢,但不多修改刪除的對象進行二級緩存。
Session s = HibernateUtils.openSession();
s.beginTransaction();
Studs studs = s.get(Studs.class, "S001");
studs.setStudName("Mary");
s.getTransaction().commit();
s.close();
[Can't write to a readonly object]
限制不了HQL:
public void test1() {
Session s = HibernateUtils.openSession();
s.beginTransaction();
s.createQuery("update Studs set studName='Simth' where studId='S001'")
.executeUpdate();
s.getTransaction().commit();
s.close();
}
將一個對象放到二級緩存中,可是會添加一個鎖。
若是一個Session沒有結束,其餘的Session獲取不到這個二級緩存中的對象:
public void test1() {
Session s = HibernateUtils.openSession();
Session s2 = HibernateUtils.openSession();
s.beginTransaction();
s2.beginTransaction();
Studs studs1 = s.get(Studs.class,"S001");
boolean boo =
HibernateUtils.getSessionFactory()//
.getCache().containsEntity(Studs.class, "S001");
System.err.println("是否有這個對象:"+boo);//true
Studs studs2 = s2.get(Studs.class,"S001");
s2.getTransaction().commit();
s2.close();
s.getTransaction().commit();
s.close();
}
放到二級緩存中,不添加鎖
(同上代碼)
對於Hiberante的分頁:
<!-- 指定方言 -->
<property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>
Mysql:
<!-- 指定方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
Session s = HibernateUtils.openSession();
List list = s.createCriteria(Studs.class)//
.setFirstResult(10)//limit start
.setMaxResults(10)//,end - pageSize
.list();
s.close();