如何为SQL Server配置Hibernate配置文件
-
01-10-2019 - |
题
这是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>
解决方案
属性是 特定于数据库 是:
hibernate.connection.driver_class
: :JDBC驱动程序类hibernate.connection.url
: :JDBC URLhibernate.connection.username
: :数据库用户hibernate.connection.password
: :数据库密码hibernate.dialect
: :冬眠的班级名称org.hibernate.dialect.Dialect
这使Hibernate可以生成针对特定关系数据库优化的SQL。
要更改数据库,您必须:
- 为类路径上的数据库提供适当的JDBC驱动程序,
- 更改JDBC属性(驱动程序,URL,用户,密码)
- 更改
Dialect
Hibernate用于与数据库交谈
有两个驱动程序可以连接到SQL Server;开源 JTD 和Microsoft One。驱动程序类和JDBC URL取决于您使用哪个。
与JTDS驱动程序
驱动程序类名称是 net.sourceforge.jtds.jdbc.Driver
.
SQLServer的URL格式是:
jdbc:jtds:sqlserver://<server>[:<port>][/<database>][;<property>=<value>[;...]]
因此,冬眠配置看起来像(请注意,您可以跳过 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
.
URL格式是:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
因此,冬眠配置看起来像:
<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>
参考
其他提示
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
...
我们还需要提及SQServer的默认模式:DBO
<property name="hibernate.default_schema">dbo</property>
用冬眠测试4
不要忘记在SQL Server配置工具中启用TCP/IP连接
最后是 Hibernate 5
在 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>
将JAR文件保留在Web-Inf lib Incase下,您可以识别jar,它无法识别。
在我的情况下,一切正常,但无法加载驱动程序类。
不隶属于 StackOverflow