---
title: "Hibernate加载其数据库配置"
author: "netxdiy"
date: "2011-03-23T21:58:54Z"
tags: ["Hibernate", "JDBC", "Oracle", "Sybase", "配置管理"]
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2011-03-23T21:58:54Z"
id: 973126
url: "https://netxdiy.iteye.com/blog/973126"
---

Hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml,常用xml文件比较直观，方便管理， Hibernate 是一个流行的开源对象关系映射工具，方便连接不同数据库，更换数据库，只要修改Hibernate配置文件，大度减少项目的维护。  
     完整的配置如下:

&lt;?xml version='1.0' encoding='utf-8'?&gt;  
&lt;!DOCTYPE hibernate-configuration PUBLIC  
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;

&lt;hibernate-configuration&gt;

    &lt;session-factory&gt;

        &lt;!-- Database connection settings --&gt;  
        &lt;property name="connection.driver\_class"&gt;org.hsqldb.jdbcDriver&lt;/property&gt;  
        &lt;property name="connection.url"&gt;jdbc:hsqldb:hsql://localhost/test&lt;/property&gt;  
        &lt;property name="connection.username"&gt;sa&lt;/property&gt;  
        &lt;property name="connection.password"&gt;&lt;/property&gt;

        &lt;!-- JDBC connection pool (use the built-in) --&gt;  
        &lt;property name="connection.pool\_size"&gt;1&lt;/property&gt;

        &lt;!-- SQL dialect --&gt;  
        &lt;property name="dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/property&gt;

        &lt;!-- Enable Hibernate's automatic session context management --&gt;  
        &lt;property name="current\_session\_context\_class"&gt;thread&lt;/property&gt;

        &lt;!-- Disable the second-level cache  --&gt;  
        &lt;property name="cache.provider\_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;

        &lt;!-- Echo all executed SQL to stdout --&gt;  
        &lt;property name="show\_sql"&gt;true&lt;/property&gt;

        &lt;!-- Drop and re-create the database schema on startup --&gt;  
        &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;

        &lt;mapping resource="../\*.hbm.xml"/&gt;

    &lt;/session-factory&gt;

&lt;/hibernate-configuration&gt;

     常用数据库连接  
     MySql 3/4/5:

   &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/test&lt;/property&gt;      
   &lt;property name="connection.username"&gt;root&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;

    Microsoft SQLServer (via jTDS):

   &lt;property name="dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;net.sourceforge.jtds.jdbc.Driver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:jtds:sqlserver://localhost:1433/test;useCursors=true&lt;/property&gt;      
   &lt;property name="connection.username"&gt;sa&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;  

    IBM DB2:

   &lt;property name="dialect"&gt;org.hibernate.dialect.DB2Dialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;com.ibm.db2.jcc.DB2Driver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:db2://localhost:50000/test&lt;/property&gt;      
   &lt;property name="connection.username"&gt;db2inst1&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt; 

    Oracle:

   &lt;property name="dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:oracle:thin:@localhost:1521:test&lt;/property&gt;      
   &lt;property name="connection.username"&gt;ora&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;

    Informix:

   &lt;property name="dialect"&gt;org.hibernate.dialect.InformixDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;com.informix.jdbc.IfxDriver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:informix-sqli://localhost:1526/test:informixserver=server1&lt;/property&gt;      
   &lt;property name="connection.username"&gt;root&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;    
   &lt;property name="connection.encoding"&gt;true&lt;/property&gt;

    Sybase (via jTDS):

   &lt;property name="dialect"&gt;org.hibernate.dialect.SybaseDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;net.sourceforge.jtds.jdbc.Driver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:jtds:sybase://localhost:7100/test;useCursors=true&lt;/property&gt;      
   &lt;property name="connection.username"&gt;root&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;

    PostgreSQL:

   &lt;property name="dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;org.postgresql.Driver&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:postgresql://localhost:5432/test&lt;/property&gt;      
   &lt;property name="connection.username"&gt;root&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;

    MaxDB(SAPDB):

   &lt;property name="dialect"&gt;org.hibernate.dialect.SAPDBDialect&lt;/property&gt;  
     
   &lt;property name="connection.driver\_class"&gt;com.sap.dbtech.jdbc.DriverSapDB&lt;/property&gt;  
   &lt;property name="connection.url"&gt;jdbc:sapdb://localhost/test&lt;/property&gt;      
   &lt;property name="connection.username"&gt;root&lt;/property&gt;  
   &lt;property name="connection.password"&gt;&lt;/property&gt;