Hibernate 主清單文件配制的詳細(xì)介紹
Hibernate 主清單文件配制的詳細(xì)介紹
1 Hiernate 清單配制文件
方式一 在工程src目錄下創(chuàng)建 hibernate.cfg.xml 文件
Hiernate 開始加載時(shí),會(huì)默認(rèn)的方式去工程src目錄下掃描 hibernate.cfg.xml文件,然后加載配制
public class H3Utils { private static SessionFactory factory = new Configuration().configure().buildSessionFactory(); /** * 獲得線程綁定的session * @return */ public static Session getCurrentSession(){ return factory.getCurrentSession(); } }
方式二 在工程中的任何目錄下創(chuàng)建 hibernate.cfg.xml 文件
這種方式的時(shí)候,需要在使用的時(shí)候 手動(dòng)指定配制文件的路徑
public class HBUtils { //提供一個(gè)工廠 (鏈?zhǔn)讲僮? private static SessionFactory factory = new Configuration() .configure("android/longs/study/config/hibernate.cfg.xml") .buildSessionFactory(); /** * 獲得新的會(huì)話 * @return */ public static Session openSession(){ return factory.openSession() ; } /** * 獲得當(dāng)前線程中綁定的session * @return */ public static Session getCurrentSession(){ return factory.getCurrentSession(); } }
2 Hiernate 清單配制文件 詳情
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1 基本4項(xiàng) --> <!-- 1.1 加載驅(qū)動(dòng)配制 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 1.2 數(shù)據(jù)庫地址 --> <!-- 如 jdbc:mysql://192.168.1.1:3306/test_java_study?useUnicode=true&characterEncoding=UTF-8--> <property name="hibernate.connection.url">url</property> <!-- 1.3 登錄數(shù)據(jù)庫用戶名 --> <property name="hibernate.connection.username">root</property> <!-- 1.3 登錄數(shù)據(jù)庫用戶名密碼 --> <property name="hibernate.connection.password">123456</property> <!-- 2 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 3 開發(fā)時(shí),優(yōu)化設(shè)置 --> <!-- 3.1 顯示生產(chǎn)sql語句 --> <property name="hibernate.show_sql">true</property> <!-- 3.2 格式化方式顯示sql --> <property name="hibernate.format_sql">true</property> <!-- 4 表的創(chuàng)建 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 5 取消bean校驗(yàn) --> <property name="javax.persistence.validation.mode">none</property> <!-- 6 將session綁定當(dāng)本地線程中 * hibernate session 管理 : 只將使用。 * 當(dāng)在cfg.xml 配置 thread,SessionFactory提供 getCurrentSession() 將可以使用。 * hibernate底層使用 ThreadLocal 線程局部變量,可以在一個(gè)線程中共享數(shù)據(jù)。 *** get() ##map.get(Thread) *** set(value) ##map.put(Thread,value) *** remove() ##map.remove(Thread) --> <property name="hibernate.current_session_context_class">thread</property> <!-- 整合c3p0 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- 對(duì)象類的 映射文件 --> <mapping resource="android/longs/study/home/servlet/model/MobleHomeModel.hbm.xml" /> </session-factory> </hibernate-configuration>
關(guān)于 第四項(xiàng) 表的創(chuàng)建中
取值可為 create : 每一次都將創(chuàng)建表,如果表已經(jīng)存在將刪除。(測試)程序結(jié)束之后,表存在的。 create-drop:每一次都將創(chuàng)建表,如果表已經(jīng)存在將刪除。(測試)程序結(jié)束之后,將刪除表。 注意:必須執(zhí)行 factory.close() 否則與“create”相同 update : 如果表不存在,將創(chuàng)建。如果存在,將維護(hù)對(duì)應(yīng)關(guān)系(映射文件 - 表)【】 注意:只負(fù)責(zé)添加,但不進(jìn)行刪除。 validate : 運(yùn)行時(shí),將校驗(yàn) 映射文件 和 表 對(duì)應(yīng)關(guān)系,如果一一對(duì)應(yīng)程序正常運(yùn)行,如果不對(duì)應(yīng)拋異常。
二級(jí)緩存配制
<!-- 配置隔離級(jí)別 --> <property name="hibernate.connection.isolation">4</property> <!-- 開啟二級(jí)緩存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 提供商 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 開啟查詢緩存 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- 二級(jí)緩存監(jiān)測 --> <property name="hibernate.generate_statistics">true</property> <!-- 類緩存 --> <!-- com包下的Customer類 --> <class-cache usage="read-write" class="com.Customer"/> <!-- com包下的Order包 --> <class-cache usage="read-write" class="com.Order"/> <!-- 集合緩存 --> <!-- com包下的Customer類中的orderSet集合 --> <collection-cache usage="read-write" collection="com.Customer.orderSet"/>
注意
一級(jí)緩存緩存的是對(duì)象
二級(jí)緩存緩存的是數(shù)據(jù)
二級(jí)緩存中集合緩存中的對(duì)象未進(jìn)行類緩存的話,將會(huì)執(zhí)行OID查詢
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法
這篇文章主要介紹了tomcat共享多個(gè)web應(yīng)用會(huì)話的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10jsp利用POI生成Excel并在頁面中導(dǎo)出的示例
本篇文章主要是介紹jsp利用POI生成Excel并在頁面中導(dǎo)出的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10JSP靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入使用詳解
這篇文章主要介紹了JSP靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入使用詳解,文章通過include指令的使用格式,詳細(xì)介紹了靜態(tài)與動(dòng)態(tài)注入的使用方法,對(duì)大家的學(xué)習(xí)或經(jīng)驗(yàn)積累有一定的參考價(jià)值,需要的朋友可以參考下2020-08-08