Hibernate -- hibernate.cfg.xml 核心配置文件

<?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>
        <!-- property 元素用於配置Hibernate中的屬性
            鍵:值 
          -->
          <!-- hibernate.connection.driver_class : 鏈接數據庫的驅動  -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>


          <!-- hibernate.connection.username : 鏈接數據庫的用戶名 -->
        <property name="hibernate.connection.username">root</property>


          <!-- hibernate.connection.password : 鏈接數據庫的密碼 -->
        <property name="hibernate.connection.password">123</property>


          <!-- hibernate.connection.url : 鏈接數據庫的地址,路徑 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedem</property>


        <!-------------------------------------------------------------------------------->


        <!-- show_sql: 操做數據庫時,會 向控制檯打印sql語句 -->
        <property name="show_sql">true</property>


        <!-------------------------------------------------------------------------------->


        <!-- format_sql: 打印sql語句前,會將sql語句先格式化  -->
        <property name="format_sql">true</property>


        <!-------------------------------------------------------------------------------->


        <!-- hbm2ddl.auto: 生成表結構的策略配置
             update(最經常使用的取值): 若是當前數據庫中不存在表結構,那麼自動建立表結構. 
                     若是存在表結構,而且表結構與實體一致,那麼不作修改
                     若是存在表結構,而且表結構與實體不一致,那麼會修改表結構.會保留原有列.
             create(不多):不管是否存在表結構.每次啓動Hibernate都會從新建立表結構.(數據會丟失)
             create-drop(極少): 不管是否存在表結構.每次啓動Hibernate都會從新建立表結構.每次Hibernate運行結束時,刪除表結構.
             validate(不多):不會自動建立表結構.也不會自動維護表結構.Hibernate只校驗表結構. 若是表結構不一致將會拋出異常.
          -->
        <property name="hbm2ddl.auto">update</property>


        <!-------------------------------------------------------------------------------->


        <!-- 數據庫方言配置 
         org.hibernate.dialect.MySQLDialect (選擇最短的)
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-------------------------------------------------------------------------------->


        <!-- hibernate.connection.autocommit: 事務自動提交  -->
        <property name="hibernate.connection.autocommit">true</property>


        <!-------------------------------------------------------------------------------->


        <!-- 將Session與線程綁定=> 只有配置了該配置,才能使用getCurrentSession -->
        <property name="hibernate.current_session_context_class">thread</property>


        <!-------------------------------------------------------------------------------->


        <!-- 引入ORM 映射文件 
            填寫src以後的路徑
         -->
        <mapping resource="com/itheima/a_hello/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
相關文章
相關標籤/搜索