Java 我在 spring/hibernate 中使用什么 mysql 驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3278148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What mysql driver do I use with spring/hibernate?
提问by Blankman
Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?
有点困惑,'driverclassname' 和'hibernate.dialect' 都是指 mysql 驱动程序吗?
What should I be using? Is the connectorJ the one I should use?
我应该使用什么?连接器J是我应该使用的吗?
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:mysql://localhost/blah"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
I'm using Maven so if I can get the driver from maven that would be ideal.
我正在使用 Maven,所以如果我可以从 Maven 获取驱动程序,那将是理想的。
Running my app in tomcat I get the error:
在 tomcat 中运行我的应用程序时出现错误:
Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL
采纳答案by Pascal Thivent
Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?
有点困惑,'driverclassname' 和'hibernate.dialect' 都是指 mysql 驱动程序吗?
No, they are not. The driverclassname
is referring to, well, the driver class namewhich is the class from a given JDBC driver that implements java.sql.Driver
. The driver class name is driver specific.
不,他们不是。driverclassname
指的是驱动程序类名称,它是来自实现java.sql.Driver
. 驱动程序类名称是特定于驱动程序的。
When using MySQL's JDBC driver aka MySQL Connector/J, this class is com.mysql.jdbc.Driver
as explained in the MySQL Connector/J documentation:
当使用 MySQL 的 JDBC 驱动程序又名 MySQL Connector/J 时,此类com.mysql.jdbc.Driver
在 MySQL Connector/J 文档中解释:
20.3.4.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J
The name of the class that implements
java.sql.Driver
in MySQL Connector/J iscom.mysql.jdbc.Driver
. (...)
20.3.4.1. Connector/J 的驱动程序/数据源类名称、URL 语法和配置属性
java.sql.Driver
在 MySQL Connector/J中实现的类的名称 是com.mysql.jdbc.Driver
. (……)
And actually, they even provide instructions to use their driver with Spring. See the section 20.3.5.2.4. Using Connector/J with Spring.
实际上,他们甚至提供了在 Spring 中使用他们的驱动程序的说明。请参阅第20.3.5.2.4节。将 Connector/J 与 Spring 一起使用。
The hibernate.dialect
is different, this configuration property is used to define the classname of a Hibernate org.hibernate.dialect.Dialect
which allows Hibernate to generate SQL optimized for a particular relational database. Again this is explained in the Hibernate documentation:
hibernate.dialect
不同的是,此配置属性用于定义Hibernate 的类名,org.hibernate.dialect.Dialect
它允许 Hibernate 生成针对特定关系数据库优化的 SQL。再次在 Hibernate 文档中对此进行了解释:
3.4. Optional configuration properties
(...) The classname of a Hibernate
org.hibernate.dialect.Dialect
which allows Hibernate to generate SQL optimized for a particular relational database.e.g.
full.classname.of.Dialect
In most cases Hibernate will actually be able to choose the correct
org.hibernate.dialect.Dialect
implementation based on the JDBC metadata returned by the JDBC driver.
3.4. 可选配置属性
(...) Hibernate 的类名,
org.hibernate.dialect.Dialect
它允许 Hibernate 生成针对特定关系数据库优化的 SQL。例如
full.classname.of.Dialect
在大多数情况下,Hibernate 实际上能够
org.hibernate.dialect.Dialect
根据 JDBC 驱动程序返回的 JDBC 元数据选择正确的 实现。
For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect
if you are using InnoDB tables (this would be my recommendation) or org.hibernate.dialect.MySQL5Dialect
if you're not. See the section 3.4.1. SQL Dialectsfor a (non exhaustive) list.
对于 MySQL 5.x,org.hibernate.dialect.MySQL5InnoDBDialect
如果您正在使用 InnoDB 表(这将是我的建议)或org.hibernate.dialect.MySQL5Dialect
如果您不使用,您应该使用。请参阅第3.4.1节。用于(非详尽)列表的SQL 方言。
Last point, the Maven part that you didn't even mention in your question... The MySQL JDBC driver is available in the Maven central repository and you should use a repository search engine(as I already suggested). For example, the following query:
最后一点,您在问题中甚至没有提到的 Maven 部分...... MySQL JDBC 驱动程序在 Maven 中央存储库中可用,您应该使用存储库搜索引擎(正如我已经建议的那样)。例如,以下查询:
http://www.jarvana.com/jarvana/search?search_type=project&project=mysql
http://www.jarvana.com/jarvana/search?search_type=project&project=mysql
allows to find the maven coordinates of the ultimate version in two clicks:
允许通过两次点击找到终极版本的 maven 坐标:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
PS: I don't mean to be rude and I'm glad to help but you should really try to leverage the documentation of the products or frameworks you're using. What you're asking in this question is well documented (as I showed) and can be found easily. Learning to find basic information by yourself is a fundamental skill for a software developer in my opinion.
PS:我无意粗鲁,我很乐意提供帮助,但您确实应该尝试利用您正在使用的产品或框架的文档。您在这个问题中提出的问题有据可查(如我所示)并且很容易找到。在我看来,学会自己查找基本信息是软件开发人员的一项基本技能。
回答by BalusC
The driverClassName
should be referring to the class name of the JDBC driver you'd like to load (as you usually would do using Class#forName()
in "plain" JDBC). The one you're currently specifying is correct for the MySQL JDBC driver (Update: you quickly edited it into a HSQLDB JDBC driver one, this is only correct for a Hypersonic DB, not for a MySQL DB).
本driverClassName
应该是指你想加载JDBC驱动程序的类名(因为你通常会做使用Class#forName()
的“普通” JDBC)。您当前指定的一个适用于 MySQL JDBC 驱动程序(更新:您快速将其编辑为 HSQLDB JDBC 驱动程序之一,这仅适用于 Hypersonic DB,不适用于 MySQL DB)。
The hibernate.dialect
should be referring to the class name of the Hibernate Dialectimplementation you'd like to use for the particular database, so that Hibernate knows what the DB in question understands so that it can autogenerate the suitable SQL statements. The one you're currently specifying is correct for the MySQL database.
该hibernate.dialect
应指的类名Hibernate的话,你想使用的特定数据库实现,这样的话Hibernate知道有问题的DB了解到,以便它可以自动生成合适的SQL语句。您当前指定的那个对于 MySQL 数据库是正确的。
That said, it sounds like that you're having problems with it. Probably you haven't installed the MySQL JDBC driver? Are you getting a ClassNotFoundException
on it? Just downloading Connector/J, extracting the zip and placing the JAR file in the runtime classpath ought to be sufficient.
也就是说,听起来您遇到了问题。可能您还没有安装 MySQL JDBC 驱动程序?你得到ClassNotFoundException
它吗?只需下载Connector/J,解压缩 zip 并将 JAR 文件放在运行时类路径中就足够了。
回答by tim_wonil
regarding maven mysql definition, here's one that seems to work.
关于 Maven mysql 定义,这里有一个似乎有效的定义。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
</dependency>