ITeye博客

hibernate 配置文件之hibernate.cfg.xml说明

danlley · 2007-07-17 16:22 · J2EE · Markdown

以下是一个简单的配置文件清单:

xml 代码

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 以下是Oracle配置 -->
  8. <!-- Oracle驱动程序位置:D:\oracle\ora92\jdbc\lib(前提是你安装了Oracle) -->
  9. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  10. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:rosy</property>
  11. <property name="connection.username">HIBERLAB</property>
  12. <property name="connection.password">TEST</property>
  13. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  14. <!-- MySQL配置,其驱动程序可以到官方网站下载 -->
  15. <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
  16. <property name="connection.url">jdbc:mysql://localhost:3306/user</property>
  17. <property name="connection.username">root</property>
  18. <property name="connection.password">111111</property>
  19. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  20. <!-- sql server 第三方驱动插件下载地址 -->
  21. <!-- "http://sourceforge.net/project/showfiles.php?group_id=33291&package_id=25350&release_id=369359" -->
  22. <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
  23. <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/j2eeone</property>
  24. <property name="connection.username">sa</property>
  25. <property name="connection.password">TEST</property>
  26. <!-- JDBC connection pool (use the built-in) -->
  27. <property name="connection.pool_size">1</property>
  28. <!-- Enable Hibernate's automatic session context management -->
  29. <property name="current_session_context_class">thread</property>
  30. <!-- Disable the second-level cache  -->
  31. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  32. <!-- Echo all executed SQL to stdout -->
  33. <property name="show_sql">true</property>
  34. <!-- Drop and re-create the database schema on startup -->
  35. <property name="hbm2ddl.auto">create</property>
  36. <mapping resource="setting/Event.hbm.xml" />
  37. <mapping resource="setting/Person.hbm.xml" />
  38. </session-factory>
  39. </hibernate-configuration>

connection.driver_class :连接数据库所要用到的驱动程序。

 

 

connection.url:数据库连接地址。

 

                     

 

connection.username :数据库用户名

 

 

 

connection.password :数据库密码

 

 

connection.pool_size :链接池大小

 

 

 

说明:关于数据库的配置说明可以参考我的下面这篇博客


http://blog.chinaunix.net/u/22374/showart.php?id=151754


dialect :sql的本地方言,根据链接的数据库确定

 

 

current_session_context_class :确定以何种方式产生Session,如thread或jta

 

 

cache.provider_class :hibernate对缓存的支持

 

 

show_sql :是否在后台打印sql,默认为不打印,如果需要打印则设置为true,如果是在程序的调试阶段,可以将其设置为true,但是如果程序已经到了产品化阶段,为了提高程序的效率建议还是设置为false。

 

 

hbm2ddl.auto :每次运行时是否需要重新建立数据库,默认不需要,如果需要每次重新生成数据库,则可以设置为true,如果你是第一次运行程序,通过设置该参数可以自动在本地生成一个工程数据库,但是如果你的数据库中已经存在而且有部分重要数据的情况下,建议不要把这个参数设置为true,否则,hibernate首先会将本地的数据和表结构全部清除,然后再重新建立最新库。(这个一定要切记的!^^)

 

 

mapping resource :对象配置所在地址

hibernate的表结构以及表之间的关系都是通过mapping resource所指向的配置文件生成的。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。