国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Hibernate 主清單文件配制的詳細(xì)介紹

2020-07-27 21:19:57
字體:
供稿:網(wǎng)友

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&amp;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ì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚州市| 聂荣县| 常山县| 山东省| 香港 | 麻栗坡县| 布拖县| 灵石县| 黄大仙区| 高雄县| 延寿县| 新丰县| 新河县| 长葛市| 津市市| 灌阳县| 永川市| 金塔县| 安塞县| 怀柔区| 无为县| 同德县| 博白县| 牟定县| 鸡西市| 温宿县| 余姚市| 桓仁| 彩票| 安泽县| 竹山县| 竹北市| 尚义县| 高雄县| 昭苏县| 佛学| 建宁县| 宁晋县| 民县| 新蔡县| 根河市|