Hibernate 需要提前知道去哪里查找java类与表之间的映射信息,它同样需要知道数据库的配置信息以及其它一些相关的初始化信息。这些信息都填写在Hibernate.properties和Hibernate.cfg.xml里面。
那Hibernate.properties和Hibernate.cfg.xml怎么用呢?按官方推荐的写法都是在classpath下。Hibernate.properties的参数都有默认值。
一般hibernate.properties修改的配置项
hibernate.dialect
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.connection.pool_size
hibernate.connection.autocommit
Hibernate.cfg.xml一般长这样
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
dbusername
</property>
<property name="hibernate.connection.password">
dbuserpasswd
</property>
<!-- List of XML mapping files -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
有了上面两个文件,hibernate已经有足够的信息启动,并且准备为你的应用服务了。
稍提一下:hibernate.dialect ,hibernate的方言。之前我提到过,数据库作为一个独立的学科,门派也非常多。Mysql有自己的特性,Oracle也有自己的特性,SQL Server 也如此。hibernate它既然敢站在应用前面,说它深谙数据库之道,一切数据库相关的事务都交给它,必定是有与各数据库打交道的能力。hibernate通过学会各数据库方言,从而顺利的“沟通”。
DB2 | org.hibernate.dialect.DB2Dialect |
HSQLDB | org.hibernate.dialect.HSQLDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Informix | org.hibernate.dialect.InformixDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Microsoft SQL Server 2000 | org.hibernate.dialect.SQLServerDialect |
Microsoft SQL Server 2005 | org.hibernate.dialect.SQLServer2005Dialect |
Microsoft SQL Server 2008 | org.hibernate.dialect.SQLServer2008Dialect |
MySQL | org.hibernate.dialect.MySQLDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 11g | org.hibernate.dialect.Oracle10gDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
Progress | org.hibernate.dialect.ProgressDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
真是十八般武艺,样样精通啊!
就是这么神奇!一切犹如买个手机、插张卡、更新一下网络那么简单。