首先,在Hibernate的配置文件中,我们要配置连接数据库的信息。Hibernate通过JDBC池连接数据库。对于Hibernate的配置,我们可以使用properties文件,xml文件或者直接在程序中进行配置。最常用的方式是采用xml文件进行配置。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="events/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这个xml文件的root element是hibernate-configuration,有一个子元素session-factory。一个session-factory对应一个数据库的配置。如果需要对多个数据库进行配置,那么可以生成多个配置文件,这样便于启动Hibernate。
前4个property定义了JDBC连接所需要的信息:
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
dialect(方言)属性告诉Hibernate怎样和数据库“交谈”(实际上就是告诉Hibernate你使用的是什么数据库,这样Hibernate才能够生成相应的SQL语句)。
show_sql属性和format_sql属性告诉Hibernate是否需要在后台输出生产的SQL语句以及是否需要格式化这些语句。这在调试的时候是很有用的,但是会影响性能,因此在实际运行的时候,通常将这两个选项设置成false。
mapping元素定义了一个mapping file,resource属性指定了mapping file的路径。resource中指定的路径是相对于程序的classpath的路径。mapping元素可以有多个,应当将所有的mapping file都列在这里。
connection.pool_size属性是用来定义connection pool的属性,关于connection pool的配置,我们在以后会专门学习。
附:
Hibernate所支持的dialect
|
RDBMS |
Dialect |
|
DB2 |
org.hibernate.dialect.DB2Dialect |
|
DB2 AS/400 |
org.hibernate.dialect.DB2400Dialect |
|
DB2 OS390 |
org.hibernate.dialect.DB2390Dialect |
|
PostgreSQL |
org.hibernate.dialect.PostgreSQLDialect |
|
MySQL |
org.hibernate.dialect.MySQLDialect |
|
MySQL with InnoDB |
org.hibernate.dialect.MySQLInnoDBDialect |
|
MySQL with MyISAM |
org.hibernate.dialect.MySQLMyISAMDialect |
|
Oracle (any version) |
org.hibernate.dialect.OracleDialect |
|
Oracle 9i/10g |
org.hibernate.dialect.Oracle9Dialect |
|
Sybase |
org.hibernate.dialect.SybaseDialect |
|
Sybase Anywhere |
org.hibernate.dialect.SybaseAnywhereDialect |
|
Microsoft SQL Server |
org.hibernate.dialect.SQLServerDialect |
|
SAP DB |
org.hibernate.dialect.SAPDBDialect |
|
Informix |
org.hibernate.dialect.InformixDialect |
|
HypersonicSQL |
org.hibernate.dialect.HSQLDialect |
|
Ingres |
org.hibernate.dialect.IngresDialect |
|
Progress |
org.hibernate.dialect.ProgressDialect |
|
Mckoi SQL |
org.hibernate.dialect.MckoiDialect |
|
Interbase |
org.hibernate.dialect.InterbaseDialect |
|
Pointbase |
org.hibernate.dialect.PointbaseDialect |
|
FrontBase |
org.hibernate.dialect.FrontbaseDialect |
|
Firebird |
org.hibernate.dialect.FirebirdDialect |
本文介绍了如何在Hibernate中配置数据库连接信息,包括使用XML文件配置JDBC连接参数、方言设置及SQL输出选项等。同时提供了支持多种数据库类型的dialect列表。
606

被折叠的 条评论
为什么被折叠?



