43

这是 MySQL 的配置文件:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">zgy01</property>
    <property name="hibernate.connection.pool_size">100</property>
    <property name="show_sql">false</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Mapping files -->
    <mapping resource="model.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

为 SQL Server 2005 指定什么?我是这样做的:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
    <property name="hibernate.connection.url">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">lal</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <property name="hibernate.connection.pool_size">100</property>        
    <property name="show_sql">false</property>

    <!-- Mapping files -->
    <mapping resource="model.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

我的问题更确切地说是如何指定我必须连接到的数据库?

在 MySQL 中,我曾经这样做过:

<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> 
4

6 回答 6

99

特定于数据库的属性是:

  • hibernate.connection.driver_class: JDBC驱动类
  • hibernate.connection.url: JDBC URL
  • hibernate.connection.username: 数据库用户
  • hibernate.connection.password: 数据库密码
  • hibernate.dialect: Hibernate 的类名,org.hibernate.dialect.Dialect它允许 Hibernate 生成针对特定关系数据库优化的 SQL。

要更改数据库,您必须:

  1. 为类路径上的数据库提供适当的 JDBC 驱动程序,
  2. 更改 JDBC 属性(驱动程序、url、用户、密码
  3. 更改DialectHibernate 用来与数据库通信的方式

有两个驱动程序可以连接到 SQL Server;开源jTDS和 Microsoft 之一。驱动程序类和 JDBC URL 取决于您使用哪一个。

使用 jTDS 驱动程序

驱动程序类名称是net.sourceforge.jtds.jdbc.Driver.

sqlserver 的 URL 格式为:

 jdbc:jtds:sqlserver://<server>[:<port>][/<database>][;<property>=<value>[;...]]

所以 Hibernate 配置看起来像(注意你可以跳过hibernate.属性中的前缀):

<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
    <property name="connection.url">jdbc:jtds:sqlserver://<server>[:<port>][/<database>]</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">lal</property>

    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    ...
  </session-factory>
</hibernate-configuration>

使用 Microsoft SQL Server JDBC 3.0:

驱动程序类名称是com.microsoft.sqlserver.jdbc.SQLServerDriver.

网址格式为:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

所以 Hibernate 配置看起来像:

<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName></property>
    <property name="connection.username">sa</property>
    <property name="connection.password">lal</property>

    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    ...
  </session-factory>
</hibernate-configuration>

参考

于 2010-08-27T22:55:56.617 回答
7

SQL Server 的连接 URL 应如下所示:

jdbc:sqlserver://serverName[\instanceName][:port][;databaseName=your_db_name]

例子:

jdbc:sqlserver://localhost
jdbc:sqlserver://127.0.0.1\INGESQL:1433;databaseName=datatest
...
于 2010-08-27T15:18:37.020 回答
5

我们还需要提及 SQSERVER 的默认模式:dbo

<property name="hibernate.default_schema">dbo</property>

用休眠4测试

于 2015-01-04T08:03:32.840 回答
4

不要忘记在 SQL SERVER 配置工具中启用 tcp/ip 连接

于 2013-06-02T22:11:55.407 回答
2

最后这是针对Hibernate 5in Tomcat

编译了上面的所有答案,并添加了我的提示,这对Hibernate 5 and SQL Server 2014.

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
   org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.driver_class">
   com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">  
jdbc:sqlserver://localhost\ServerInstanceOrServerName:1433;databaseName=DATABASE_NAME 
</property>
<property name="hibernate.default_schema">theSchemaNameUsuallydbo</property>
<property name="hibernate.connection.username">
   YourUsername
</property>
<property name="hibernate.connection.password">
   YourPasswordForMSSQL
</property>
于 2016-01-27T20:01:01.703 回答
-1

将 jar 文件保存在 web-inf lib 下,以防您包含 jar 并且无法识别 .

它适用于我的情况,一切正常但无法加载驱动程序类。

于 2018-10-06T08:30:52.900 回答