Hibernate 主清單文件配制的詳細(xì)介紹
1 Hiernate 清單配制文件
方式一 在工程src目錄下創(chuàng)建 hibernate.cfg.xml 文件
Hiernate 開始加載時,會默認(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 文件
這種方式的時候,需要在使用的時候 手動指定配制文件的路徑
public class HBUtils { //提供一個工廠 (鏈?zhǔn)讲僮? private static SessionFactory factory = new Configuration() .configure("android/longs/study/config/hibernate.cfg.xml") .buildSessionFactory(); /** * 獲得新的會話 * @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項 --> <!-- 1.1 加載驅(qū)動配制 --> <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ā)時,優(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校驗 --> <property name="javax.persistence.validation.mode">none</property> <!-- 6 將session綁定當(dāng)本地線程中 * hibernate session 管理 : 只將使用。 * 當(dāng)在cfg.xml 配置 thread,SessionFactory提供 getCurrentSession() 將可以使用。 * hibernate底層使用 ThreadLocal 線程局部變量,可以在一個線程中共享數(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> <!-- 對象類的 映射文件 --> <mapping resource="android/longs/study/home/servlet/model/MobleHomeModel.hbm.xml" /> </session-factory></hibernate-configuration>
關(guān)于 第四項 表的創(chuàng)建中
取值可為 create : 每一次都將創(chuàng)建表,如果表已經(jīng)存在將刪除。(測試)程序結(jié)束之后,表存在的。 create-drop:每一次都將創(chuàng)建表,如果表已經(jīng)存在將刪除。(測試)程序結(jié)束之后,將刪除表。 注意:必須執(zhí)行 factory.close() 否則與“create”相同 update : 如果表不存在,將創(chuàng)建。如果存在,將維護(hù)對應(yīng)關(guān)系(映射文件 - 表)【】 注意:只負(fù)責(zé)添加,但不進(jìn)行刪除。 validate : 運行時,將校驗 映射文件 和 表 對應(yīng)關(guān)系,如果一一對應(yīng)程序正常運行,如果不對應(yīng)拋異常。
二級緩存配制
<!-- 配置隔離級別 --> <property name="hibernate.connection.isolation">4</property> <!-- 開啟二級緩存 --> <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> <!-- 二級緩存監(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"/>
注意
一級緩存緩存的是對象
二級緩存緩存的是數(shù)據(jù)
二級緩存中集合緩存中的對象未進(jìn)行類緩存的話,將會執(zhí)行OID查詢
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答