`
Wind_ZhongGang
  • 浏览: 259741 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Hibernate的三种配置方式

阅读更多

 

  对于Hibernate的配置方式,有三种,一种是通过外部的属性文件进行配置,一种是通过外部的xml文件进行配置,还有一种就是通过编程的方式进行配置,但对于编程式配置来说,不便于参数的集中管理与修改,所以这里主要介绍两种外部文件的配置方式。

 

  一。属性文件配置方式

 

 

hibernate.connection.url=jdbc:mysql://localhost:3306/couple?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8

hibernate.connection.driver_class=com.mysql.jdbc.Driver

hibernate.connection.username=root

hibernate.connection.password=root

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.current_session_context_class=thread

hibernate.cache.use_second_level_cache=false

hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider

hibernate.jdbc.batch_size=100

hibernate.jdbc.fetch_size=10

hibernate.show_sql=true

hibernate.hbm2ddl.auto=update

hibernate.format_sql=true

hibernate.use_sql_comments=true

 

  二。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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/couple?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!--<property name="connection.autocommit">true</property>-->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.use_second_level_cache">false</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <property name="jdbc.batch_size">100</property>

        <property name="jdbc.fetch_size">10</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Format all executed SQL to stdout -->
        <property name="format_sql">true</property>

        <property name="use_sql_comments">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="hibernate/mappings/Person.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

 

  对于第一种外部配置文件方式,在创建sessionFactory时,以这样的方式调用new Configuration().buildSessionFactory(),它会寻找项目中名为hibernate.properties的文件,研究源码就可以发现在Environment类中有这样的一行代码InputStream stream = ConfigHelper.getResourceAsStream("/hibernate.properties");,因为这是写死在Hibernate中的,所以这样的文件名是不可改动的。

 

  对于第二种外部配置文件方式,在创建sessionFactory时,以这样的方式调用new Configuration().configure().buildSessionFactory(),默认在没有设置xml配置文件的位置的情况下,会去查找名为hibernate.cfg.xml的配置文件,研究源码就可以发现在Configuration类中有这样的一段代码

  public Configuration configure() throws HibernateException {

configure( "/hibernate.cfg.xml" );

return this;

}

 

  最后附上一个由Hibernate配置的mysql数据库项目模板实例,以供参考。

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics